Skip to content

Commit

Permalink
3197: Added options to load template command
Browse files Browse the repository at this point in the history
  • Loading branch information
tuj committed Jan 8, 2025
1 parent 3e8f002 commit c9babb3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

- [#230](https://github.com/os2display/display-api-service/pull/230)
- Adds options to set paths to component and admin files from path to the json config file.

## [2.1.3] - 2024-10-25

- [#220](https://github.com/os2display/display-api-service/pull/220)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
"prepare-code": [
"bin/console doctrine:schema:validate",
"vendor/bin/rector",
"vendor/bin/psalm",
"vendor/bin/psalm --no-cache",
"@coding-standards-apply",
"@test-setup",
"@test"
Expand Down
22 changes: 20 additions & 2 deletions src/Command/LoadTemplateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Uid\Ulid;
Expand All @@ -33,6 +34,8 @@ public function __construct(
protected function configure(): void
{
$this->addArgument('filename', InputArgument::REQUIRED, 'json file to load. Can be a local file or a URL');
$this->addOption('path-from-filename', 'p', InputOption::VALUE_NONE, 'Set path to component and admin from filename. Assumes that the config file loaded has the naming format: [templateName]-config[.*].json.', null);
$this->addOption('timestamp', 't', InputOption::VALUE_NONE, 'Add a timestamp to the component and admin urls: ?ts=. Only applies if path-from-filename option is active.', null);
}

final protected function execute(InputInterface $input, OutputInterface $output): int
Expand All @@ -41,7 +44,9 @@ final protected function execute(InputInterface $input, OutputInterface $output)
$successMessage = 'Template updated';

try {
/** @var string $filename */
$filename = $input->getArgument('filename');

$content = json_decode(file_get_contents($filename), false, 512, JSON_THROW_ON_ERROR);

// Validate template json.
Expand Down Expand Up @@ -87,8 +92,21 @@ final protected function execute(InputInterface $input, OutputInterface $output)
}

$template->setIcon($content->icon);
// @TODO: Resource should be an object.
$template->setResources(get_object_vars($content->resources));

$resources = get_object_vars($content->resources);

if ($input->getOption('path-from-filename')) {
// Set paths to component and admin from filename.
$resources['component'] = preg_replace("/-config.*\.json$/", '.js', $filename);
$resources['admin'] = preg_replace("/-config.*\.json$/", '-admin.json', $filename);

if ($input->getOption('timestamp')) {
$resources['component'] = $resources['component'].'?ts='.time();
$resources['admin'] = $resources['admin'].'?ts='.time();
}
}

$template->setResources($resources);
$template->setTitle($content->title);
$template->setDescription($content->description);

Expand Down

0 comments on commit c9babb3

Please sign in to comment.