Skip to content

Commit

Permalink
CLI command: site:overrides:list
Browse files Browse the repository at this point in the history
gh-153

Signed-off-by: Nicholas K. Dionysopoulos <[email protected]>
  • Loading branch information
nikosdion committed Dec 28, 2023
1 parent 13bd170 commit e3f1e26
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 9 deletions.
100 changes: 100 additions & 0 deletions src/CliCommand/SiteOverridesList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* @package panopticon
* @copyright Copyright (c)2023-2023 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license https://www.gnu.org/licenses/agpl-3.0.txt GNU Affero General Public License, version 3 or later
*/

namespace Akeeba\Panopticon\CliCommand;

defined('AKEEBA') || die;

use Akeeba\Panopticon\CliCommand\Attribute\ConfigAssertion;
use Akeeba\Panopticon\CliCommand\Trait\PrintFormattedArrayTrait;
use Akeeba\Panopticon\Factory;
use Akeeba\Panopticon\Model\Overrides;
use Akeeba\Panopticon\Model\Sites;
use RuntimeException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
name: 'site:overrides:list',
description: 'List overrides to check for a given site',
hidden: false,
)]
#[ConfigAssertion(true)]
class SiteOverridesList extends AbstractCommand
{
use PrintFormattedArrayTrait;

protected function execute(InputInterface $input, OutputInterface $output): int
{
$container = Factory::getContainer();
/**
* @var Sites $site
* @var Overrides $model
*/
$site = $container->mvcFactory->makeTempModel('Sites');
$model = $container->mvcFactory->makeTempModel('Overrides');

$siteId = $input->getArgument('site_id');

try
{
$site->findOrFail($siteId);
}
catch (RuntimeException)
{
$this->ioStyle->error('No such site');

return Command::FAILURE;
}

$model->setSite($site);
$model->setState('limitstart', 0);
$model->setState('limit', 10000000);
$overrides = $model
->get(true)
->map(
fn(object $x) => [
'id' => $x->id,
'template' => $x->template,
'file' => base64_decode($x->hash_id),
'action' => $x->action,
'client' => match ($x->client_id)
{
0 => 'Site',
1 => 'Administrator',
2 => 'Installation',
3 => 'API',
4 => 'CLI',
default => '(Unknown)'
},
'created_date' => $x->created_date
]
)
->toArray();

$format = $input->getOption('format');

$this->printFormattedArray($overrides, $format);

return Command::SUCCESS;
}

protected function configure(): void
{
$this
->addArgument(
'site_id', InputOption::VALUE_REQUIRED, 'Numeric ID of the site to list template overrides for'
)
->addOption(
'format', 'f', InputOption::VALUE_OPTIONAL, 'Output format (table, json, yaml, csv, count)', 'table'
);
}

}
4 changes: 3 additions & 1 deletion src/CliCommand/Trait/PrintFormattedArrayTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ private function printFormattedArray(?array $data, string $format): void
case 'table':
if ($isNumericKeys)
{
$this->ioStyle->table(array_keys(reset($data)), $data);
$firstRow = reset($data);
$firstRow = is_array($firstRow) ? $firstRow : (array)$firstRow;
$this->ioStyle->table(array_keys($firstRow), $data);

return;
}
Expand Down
38 changes: 30 additions & 8 deletions src/Model/Overrides.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,38 @@ public function getPagination(): Pagination
return new Pagination($this->count(), $limitStart, $limit, 10, $this->getContainer());
}

public function get(): Collection
public function get(bool $overrideLimits = false): Collection
{
if (empty($this->site))
{
return new Collection();
}

$limitStart = $this->getUserStateFromRequest('limitstart', 'limitstart', 0, 'int') ?: 0;
$limit = $this->getUserStateFromRequest('limit', 'limit', 20, 'int') ?: 20;
$client = $this->getUserStateFromRequest('client', 'client', 0, 'int') ?: 0;
if ($overrideLimits)
{
$limitStart = 0;
$limit = 100000000;
$client = null;
}
else
{
$limitStart = $this->getUserStateFromRequest('limitstart', 'limitstart', 0, 'int') ?: 0;
$limit = $this->getUserStateFromRequest('limit', 'limit', 20, 'int') ?: 20;
$client = $this->getUserStateFromRequest('client', 'client', 0, 'int') ?: 0;
}


[$url, $options] = $this->getRequestOptions($this->site, '/index.php/v1/panopticon/template/overrides/changed');

$uri = new Uri($url);
$uri->setVar('client', $client);
$uri->setVar('page[limit]', $limit);
$uri->setVar('page[offset]', $limitStart);

if ($client)
{
$uri->setVar('client', $client);
}

/** @var \Akeeba\Panopticon\Container $container */
$container = $this->container;
$httpClient = $container->httpFactory->makeClient(cache: true, cacheTTL: 10);
Expand All @@ -86,7 +100,10 @@ public function get(): Collection
$rawData = null;
}

if (empty($rawData) || !is_object($rawData) || !is_array($rawData?->data ?? null) || empty($rawData?->data ?? null))
if (empty($rawData) || !is_object($rawData) || !is_array($rawData?->data ?? null)
|| empty(
$rawData?->data ?? null
))
{
return new Collection();
}
Expand All @@ -95,7 +112,10 @@ public function get(): Collection
array_filter(
array_map(
function ($item): ?object {
if (!is_object($item) || empty($item->attributes ?? null) || !is_object($item->attributes ?? null))
if (!is_object($item) || empty($item->attributes ?? null)
|| !is_object(
$item->attributes ?? null
))
{
return null;
}
Expand Down Expand Up @@ -177,7 +197,9 @@ public function getItem(): ?object
'wrapperClasses' => ['diff-wrapper'],
];

$data->diff = DiffHelper::calculate($data->coreSource, $data->overrideSource, 'SideBySide', $diffOptions, $rendererOptions);
$data->diff = DiffHelper::calculate(
$data->coreSource, $data->overrideSource, 'SideBySide', $diffOptions, $rendererOptions
);

return $data;
}
Expand Down

0 comments on commit e3f1e26

Please sign in to comment.