Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F120838309
ObjectListCommand.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
3 KB
Referenced Files
None
Subscribers
None
ObjectListCommand.php
View Options
<?php
namespace
App\Console
;
/**
* This abstract class provides a means to treat objects in our model using CRUD, with the exception that
* this particular abstract class lists objects.
*/
abstract
class
ObjectListCommand
extends
ObjectCommand
{
public
function
__construct
()
{
$this
->
description
=
"List all {$this->objectName} objects"
;
$this
->
signature
=
$this
->
commandPrefix
?
$this
->
commandPrefix
.
":"
:
""
;
if
(!
empty
(
$this
->
objectNamePlural
))
{
$this
->
signature
.=
"{$this->objectNamePlural}"
;
}
else
{
$this
->
signature
.=
"{$this->objectName}s"
;
}
if
(
in_array
(
\App\Traits\BelongsToTenantTrait
::
class
,
class_uses
(
$this
->
objectClass
)))
{
$this
->
signature
.=
" {--tenant= : Limit results to the specified tenant}"
;
}
if
(
\App\Utils
::
isSoftDeletable
(
$this
->
objectClass
))
{
$this
->
signature
.=
" {--with-deleted : Include deleted {$this->objectName}s}"
;
}
$this
->
signature
.=
" {--attr=* : Attributes other than the primary unique key to include}"
.
"{--filter=* : Additional filter(s) or a raw SQL WHERE clause}"
;
parent
::
__construct
();
}
/**
* Execute the console command.
*
* @return mixed
*/
public
function
handle
()
{
// @phpstan-ignore-next-line
if
(
\App\Utils
::
isSoftDeletable
(
$this
->
objectClass
)
&&
$this
->
option
(
'with-deleted'
))
{
$objects
=
$this
->
objectClass
::
withTrashed
();
}
else
{
$objects
=
new
$this
->
objectClass
();
}
// @phpstan-ignore-next-line
if
(
$this
->
hasOption
(
'tenant'
)
&&
(
$tenant
=
intval
(
$this
->
option
(
'tenant'
))))
{
$this
->
tenantId
=
$tenant
;
$objects
=
$this
->
applyTenant
(
$objects
);
}
foreach
(
$this
->
option
(
'filter'
)
as
$filter
)
{
$objects
=
$this
->
applyFilter
(
$objects
,
$filter
);
}
foreach
(
$objects
->
cursor
()
as
$object
)
{
if
(
$object
->
deleted_at
)
{
$this
->
info
(
"{$this->toString($object)} (deleted at {$object->deleted_at}"
);
}
else
{
$this
->
info
(
"{$this->toString($object)}"
);
}
}
}
/**
* Apply pre-configured filter or raw WHERE clause to the main query.
*
* @param object $query Query builder
* @param string $filter Pre-defined filter identifier or raw SQL WHERE clause
*
* @return object Query builder
*/
public
function
applyFilter
(
$query
,
string
$filter
)
{
// Get objects marked as deleted, i.e. --filter=TRASHED
// Note: For use with --with-deleted option
if
(
strtolower
(
$filter
)
===
'trashed'
)
{
return
$query
->
whereNotNull
(
'deleted_at'
);
}
// Get objects with specified status, e.g. --filter=STATUS:SUSPENDED
if
(
preg_match
(
'/^status:([a-z]+)$/i'
,
$filter
,
$matches
))
{
$status
=
strtoupper
(
$matches
[
1
]);
$const
=
"{$this->objectClass}::STATUS_{$status}"
;
if
(
defined
(
$const
))
{
return
$query
->
where
(
'status'
,
'&'
,
constant
(
$const
));
}
throw
new
\Exception
(
"Unknown status in --filter={$filter}"
);
}
// Get objects older/younger than specified time, e.g. --filter=MIN-AGE:1Y
if
(
preg_match
(
'/^(min|max)-age:([0-9]+)([mdy])$/i'
,
$filter
,
$matches
))
{
$operator
=
strtolower
(
$matches
[
1
])
==
'min'
?
'<='
:
'>='
;
$count
=
(
int
)
$matches
[
2
];
$period
=
strtolower
(
$matches
[
3
]);
$date
=
\Carbon\Carbon
::
now
();
if
(
$period
==
'y'
)
{
$date
->
subYearsWithoutOverflow
(
$count
);
}
elseif
(
$period
==
'm'
)
{
$date
->
subMonthsWithoutOverflow
(
$count
);
}
else
{
$date
->
subDays
(
$count
);
}
return
$query
->
where
(
'created_at'
,
$operator
,
$date
);
}
return
$query
->
whereRaw
(
$filter
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Fri, Apr 24, 1:57 PM (6 d, 24 m ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18842504
Default Alt Text
ObjectListCommand.php (3 KB)
Attached To
Mode
rK kolab
Attached
Detach File
Event Timeline