-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from netglue/feature/identify-document
Add commands for dumping info from a configured repository
- Loading branch information
Showing
11 changed files
with
439 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Primo\Cli\Console\InfoCommand; | ||
use Primo\Cli\Console\ListCommand; | ||
use Prismic\Api; | ||
use Symfony\Component\Console\Application; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$api = Api::get('https://primo.prismic.io/api/v2'); | ||
|
||
$application = new Application('Repository Info Commands Example'); | ||
$application->add(new InfoCommand($api)); | ||
$application->add(new ListCommand($api)); | ||
|
||
return $application->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Primo\Cli; | ||
|
||
final class ApiToolsConfigProvider | ||
{ | ||
/** @return mixed */ | ||
public function __invoke(): array | ||
{ | ||
return [ | ||
'dependencies' => $this->dependencies(), | ||
'console' => [ | ||
'commands' => $this->commands(), | ||
], | ||
'laminas-cli' => [ | ||
'commands' => $this->commands(), | ||
], | ||
]; | ||
} | ||
|
||
/** @return mixed[] */ | ||
private function dependencies(): array | ||
{ | ||
return [ | ||
'factories' => [ | ||
'PrimoCliApiInstance' => Container\PrismicApiClientFactory::class, | ||
Console\InfoCommand::class => Console\Container\InfoCommandFactory::class, | ||
Console\ListCommand::class => Console\Container\ListCommandFactory::class, | ||
], | ||
]; | ||
} | ||
|
||
/** @return string[] */ | ||
private function commands(): array | ||
{ | ||
return [ | ||
Console\InfoCommand::DEFAULT_NAME => Console\InfoCommand::class, | ||
Console\ListCommand::DEFAULT_NAME => Console\ListCommand::class, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Primo\Cli\Console\Container; | ||
|
||
use Primo\Cli\Console\InfoCommand; | ||
use Psr\Container\ContainerInterface; | ||
|
||
final class InfoCommandFactory | ||
{ | ||
public function __invoke(ContainerInterface $container): InfoCommand | ||
{ | ||
return new InfoCommand($container->get('PrimoCliApiInstance')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Primo\Cli\Console\Container; | ||
|
||
use Primo\Cli\Console\ListCommand; | ||
use Psr\Container\ContainerInterface; | ||
|
||
final class ListCommandFactory | ||
{ | ||
public function __invoke(ContainerInterface $container): ListCommand | ||
{ | ||
return new ListCommand($container->get('PrimoCliApiInstance')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Primo\Cli\Console; | ||
|
||
use Prismic\ApiClient; | ||
use Prismic\Document; | ||
use Prismic\Value\Bookmark; | ||
use Prismic\Value\Language; | ||
use Prismic\Value\Type; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
use function array_map; | ||
use function implode; | ||
use function sprintf; | ||
|
||
use const PHP_EOL; | ||
|
||
final class InfoCommand extends Command | ||
{ | ||
public const DEFAULT_NAME = 'primo:info'; | ||
|
||
/** @var ApiClient */ | ||
private $apiClient; | ||
|
||
public function __construct(ApiClient $apiClient, string $name = self::DEFAULT_NAME) | ||
{ | ||
$this->apiClient = $apiClient; | ||
parent::__construct($name); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->setDescription( | ||
'This command shows information about a specific document when given its unique identifier, ' | ||
. 'or, with no arguments provides information about the repository.' | ||
); | ||
$this->addArgument( | ||
'id', | ||
InputArgument::OPTIONAL, | ||
'The unique id of the document' | ||
); | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$style = new SymfonyStyle($input, $output); | ||
$id = $input->getArgument('id'); | ||
if (empty($id)) { | ||
$this->showApiInfo($style); | ||
|
||
return 0; | ||
} | ||
|
||
$document = $this->apiClient->findById($id); | ||
if (! $document) { | ||
$style->warning(sprintf( | ||
'A document could not be found with the id "%s" in the repository "%s"', | ||
$id, | ||
$this->apiClient->host() | ||
)); | ||
|
||
return 0; | ||
} | ||
|
||
$this->showDocumentInfo($style, $document); | ||
|
||
return 0; | ||
} | ||
|
||
private function showDocumentInfo(SymfonyStyle $style, Document $document): void | ||
{ | ||
$style->title(sprintf('Document Information: %s', $document->id())); | ||
|
||
$style->table([], [ | ||
['ID:', $document->id()], | ||
['UID:', $document->uid() ?: '<none>'], | ||
['Type:', $document->type()], | ||
['Created At:', $document->firstPublished()->format('l jS F Y H:i:s')], | ||
['Modified At:', $document->lastPublished()->format('l jS F Y H:i:s')], | ||
['Language:', $document->lang()], | ||
['Tags:', $document->tags() === [] ? '<none>' : implode(', ', $document->tags())], | ||
]); | ||
} | ||
|
||
private function showApiInfo(SymfonyStyle $style): void | ||
{ | ||
$style->title(sprintf('Repository Information: %s', $this->apiClient->host())); | ||
$style->table([], [ | ||
['Repository Host:', $this->apiClient->host()], | ||
['Master Ref:', $this->apiClient->ref()->ref()], | ||
[ | ||
'Document Types:', | ||
implode(PHP_EOL, array_map(static function (Type $type): string { | ||
return sprintf('%s ("%s")', $type->id(), $type->name()); | ||
}, $this->apiClient->data()->types())), | ||
], | ||
[ | ||
'Tags:', | ||
implode(', ', $this->apiClient->data()->tags()), | ||
], | ||
[ | ||
'Languages:', | ||
implode(PHP_EOL, array_map(static function (Language $language): string { | ||
return $language->name(); | ||
}, $this->apiClient->data()->languages())), | ||
], | ||
[ | ||
'Bookmarks:', | ||
implode(PHP_EOL, array_map(static function (Bookmark $bookmark): string { | ||
return sprintf('%s (%s)', $bookmark->name(), $bookmark->documentId()); | ||
}, $this->apiClient->data()->bookmarks())), | ||
], | ||
]); | ||
} | ||
} |
Oops, something went wrong.