Skip to content

Commit

Permalink
Refactor Commands Structure
Browse files Browse the repository at this point in the history
* first step to refactor commands

* commands moved to new structure

* CS & Psalm

* symfony/console version

* removes hidden composer dependencies

* removes hidden composer dependencies in unit tests
  • Loading branch information
overclokk authored May 29, 2024
1 parent e01f001 commit 2a3632d
Show file tree
Hide file tree
Showing 40 changed files with 236 additions and 582 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ codeception.yml
*.phar
*.bak
infection.json.dist
c3.php
c3.php

# Theme JSON Schema
theme.schema.json
2 changes: 0 additions & 2 deletions bin/theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

namespace ItalyStrap\ThemeJsonGenerator;

use ItalyStrap\ThemeJsonGenerator\Infrastructure\Cli\Composer\Bootstrap;

/** @psalm-suppress UnresolvableInclude */
require $_composer_autoload_path ?? __DIR__ . '/../vendor/autoload.php';

Expand Down
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
"require": {
"php" : ">=7.4",
"ext-json": "*",
"composer/composer": "^2.1",
"composer-plugin-api": "^2.0",

"italystrap/config": "^2.4",
"italystrap/empress": "^2.0",
Expand All @@ -34,6 +32,7 @@
"brick/varexporter": "^0.3.8",
"webimpress/safe-writer": "^2.2",

"symfony/console": "^v5.4",
"symfony/polyfill-php80": "^1.22",
"symfony/event-dispatcher": "^5.4",
"webmozart/assert": "^1.11"
Expand Down Expand Up @@ -64,8 +63,7 @@
"rector/rector": "^0.19.0",
"symplify/easy-coding-standard": "^12.0",

"italystrap/debug": "dev-master",
"wp-cli/wp-cli": "^2.7"
"italystrap/debug": "dev-master"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 0 additions & 4 deletions functions/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@
declare(strict_types=1);

namespace ItalyStrap\ThemeJsonGenerator;

use ItalyStrap\ThemeJsonGenerator\Infrastructure\Cli\WPCLI\Bootstrap;

(new Bootstrap())();
1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<directory name="functions" />
<directory name="src" />
<ignoreFiles>
<directory name="src/Application/Commands/WPCLI" />
<directory name="vendor" />
<file name="src/**/*Experimental*" />
</ignoreFiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

declare(strict_types=1);

namespace ItalyStrap\ThemeJsonGenerator\Application\Commands\Composer;
namespace ItalyStrap\ThemeJsonGenerator\Application\Commands;

use Composer\Command\BaseCommand;
use ItalyStrap\Config\ConfigInterface;
use ItalyStrap\ThemeJsonGenerator\Application\Commands\DumpMessage;
use ItalyStrap\ThemeJsonGenerator\Application\Commands\Utils\RootFolderTrait;
use ItalyStrap\ThemeJsonGenerator\Application\DumpMessage;
use ItalyStrap\ThemeJsonGenerator\Domain\Output\Dump;
use ItalyStrap\ThemeJsonGenerator\Domain\Output\Events\GeneratedFile;
use ItalyStrap\ThemeJsonGenerator\Domain\Output\Events\GeneratingFile;
use ItalyStrap\ThemeJsonGenerator\Domain\Output\Events\NoFileFound;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -20,7 +19,7 @@
/**
* @psalm-api
*/
final class DumpCommand extends BaseCommand
final class DumpCommand extends Command
{
use RootFolderTrait;

Expand All @@ -44,20 +43,21 @@ final class DumpCommand extends BaseCommand
*/
public const PATH_FOR_THEME_SASS = 'path-for-theme-sass';

private Dump $dump;
/**
* @var string
*/
public const FILE = 'file';

private ConfigInterface $config;
private Dump $dump;

private \Symfony\Component\EventDispatcher\EventDispatcher $subscriber;

public function __construct(
\Symfony\Component\EventDispatcher\EventDispatcher $subscriber,
Dump $dump,
ConfigInterface $config
Dump $dump
) {
$this->subscriber = $subscriber;
$this->dump = $dump;
$this->config = $config;
parent::__construct();
}

Expand Down Expand Up @@ -87,6 +87,26 @@ protected function configure(): void
)
);

$this->addOption(
'path',
'p',
InputOption::VALUE_OPTIONAL,
\sprintf(
'If set, %s will generate the json file in the specified path.',
self::NAME
)
);

$this->addOption(
self::FILE,
null,
InputOption::VALUE_OPTIONAL,
\sprintf(
'If set, %s will generate only the specified file.',
self::NAME
)
);

/**
* @todo other options:
* --no-pretty-print
Expand All @@ -99,18 +119,6 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$composer = $this->requireComposer();
$rootFolder = $this->rootFolder();

$package = $composer->getPackage();
/** @psalm-suppress MixedArgument */
$this->config->merge($package->getExtra()[self::COMPOSER_EXTRA_THEME_JSON_KEY] ?? []);

/**
* @todo The callback is temporary until the new files generation are in place.
* @psalm-suppress MixedAssignment
*/
$callback = $this->config->get(self::CALLBACK);

$this->subscriber->addListener(
GeneratingFile::class,
Expand All @@ -133,23 +141,23 @@ static function (GeneratedFile $event) use ($output): void {
}
);

$this->subscriber->addListener(
NoFileFound::class,
/** @psalm-suppress UnusedClosureParam */
static function (NoFileFound $event) use ($output): void {
$output->writeln(NoFileFound::M_NO_FILE_FOUND);
}
);

$rootFolder = $this->rootFolder((string)$input->getOption('path'));

$message = new DumpMessage(
$rootFolder,
(string)$this->config->get(self::PATH_FOR_THEME_SASS, ''),
(bool)$input->getOption('dry-run')
'',
(bool)$input->getOption('dry-run'),
(string)$input->getOption(self::FILE)
);

try {
if (\is_callable($callback)) {
$output->writeln('<info>Generating theme.json file</info>');
$this->dump->processBlueprint($message, 'theme', $callback);
$output->writeln('<info>Generated theme.json file</info>');
$output->writeln('========================');
}
} catch (\Exception $exception) {
$output->writeln($exception->getMessage());
}

$this->dump->handle($message);

if ($input->getOption(ValidateCommand::NAME)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

declare(strict_types=1);

namespace ItalyStrap\ThemeJsonGenerator\Application\Commands\Composer;
namespace ItalyStrap\ThemeJsonGenerator\Application\Commands;

use Composer\Command\BaseCommand;
use ItalyStrap\ThemeJsonGenerator\Application\Commands\Utils\RootFolderTrait;
use ItalyStrap\ThemeJsonGenerator\Application\InfoMessage;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @psalm-api
*/
class InfoCommand extends BaseCommand
class InfoCommand extends Command
{
use RootFolderTrait;

Expand All @@ -38,7 +38,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$rootFolder = $this->rootFolder();

$message = new \ItalyStrap\ThemeJsonGenerator\Application\Commands\InfoMessage($rootFolder);
$message = new InfoMessage($rootFolder);

try {
return (int)$this->bus->handle($message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

declare(strict_types=1);

namespace ItalyStrap\ThemeJsonGenerator\Application\Commands\Composer;
namespace ItalyStrap\ThemeJsonGenerator\Application\Commands;

use Composer\Command\BaseCommand;
use ItalyStrap\ThemeJsonGenerator\Application\Commands\InitMessage;
use ItalyStrap\ThemeJsonGenerator\Application\Commands\Utils\DataFromJsonTrait;
use ItalyStrap\ThemeJsonGenerator\Application\Commands\Utils\RootFolderTrait;
use ItalyStrap\ThemeJsonGenerator\Application\InitMessage;
use ItalyStrap\ThemeJsonGenerator\Domain\Output\Events\EntryPointCanNotBeCreated;
use ItalyStrap\ThemeJsonGenerator\Domain\Output\Events\EntryPointCreated;
use ItalyStrap\ThemeJsonGenerator\Domain\Output\Events\EntryPointDoesNotExist;
Expand All @@ -19,7 +18,7 @@
/**
* @psalm-api
*/
class InitCommand extends BaseCommand
class InitCommand extends Command
{
use RootFolderTrait;
use DataFromJsonTrait;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
namespace ItalyStrap\ThemeJsonGenerator\Application\Commands\Middleware;

use ItalyStrap\Bus\HandlerInterface;
use ItalyStrap\ThemeJsonGenerator\Application\Commands\ValidateMessage;
use ItalyStrap\ThemeJsonGenerator\Domain\Output\Validate;
use ItalyStrap\ThemeJsonGenerator\Application\ValidateMessage;

class DeleteSchemaJsonMiddleware implements \ItalyStrap\Bus\MiddlewareInterface
{
Expand Down
3 changes: 1 addition & 2 deletions src/Application/Commands/Middleware/SchemaJsonMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace ItalyStrap\ThemeJsonGenerator\Application\Commands\Middleware;

use ItalyStrap\ThemeJsonGenerator\Application\Commands\ValidateMessage;
use ItalyStrap\ThemeJsonGenerator\Domain\Output\Validate;
use ItalyStrap\ThemeJsonGenerator\Application\ValidateMessage;

class SchemaJsonMiddleware implements \ItalyStrap\Bus\MiddlewareInterface
{
Expand Down
8 changes: 2 additions & 6 deletions src/Application/Commands/Utils/RootFolderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@

trait RootFolderTrait
{
private function rootFolder(): string
private function rootFolder(string $path = ''): string
{
$composer = $this->requireComposer();

/** @var string $vendorPath */
$vendorPath = $composer->getConfig()->get('vendor-dir');
return \dirname($vendorPath);
return $path !== '' ? $path : (string)\getcwd();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

declare(strict_types=1);

namespace ItalyStrap\ThemeJsonGenerator\Application\Commands\Composer;
namespace ItalyStrap\ThemeJsonGenerator\Application\Commands;

use Composer\Command\BaseCommand;
use ItalyStrap\ThemeJsonGenerator\Application\Commands\Utils\DataFromJsonTrait;
use ItalyStrap\ThemeJsonGenerator\Application\Commands\Utils\RootFolderTrait;
use ItalyStrap\ThemeJsonGenerator\Application\Commands\ValidateMessage;
use ItalyStrap\ThemeJsonGenerator\Application\ValidateMessage;
use ItalyStrap\ThemeJsonGenerator\Domain\Output\Events\ValidatedFails;
use ItalyStrap\ThemeJsonGenerator\Domain\Output\Events\ValidatingFile;
use ItalyStrap\ThemeJsonGenerator\Domain\Output\Events\ValidFile;
Expand All @@ -19,7 +18,7 @@
/**
* @psalm-api
*/
class ValidateCommand extends BaseCommand
class ValidateCommand extends Command
{
use RootFolderTrait;
use DataFromJsonTrait;
Expand Down
Loading

0 comments on commit 2a3632d

Please sign in to comment.