Skip to content

Commit

Permalink
Add make state provider and processor
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Apr 20, 2023
1 parent 403bd4c commit 9528dfd
Show file tree
Hide file tree
Showing 16 changed files with 380 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/Bundle/Resources/config/services/maker.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,13 @@
<argument type="service" id="maker.file_manager" />
<tag name="maker.command" />
</service>

<service id="sylius.maker.state_provider" class="Sylius\Component\Resource\Symfony\Maker\MakeStateProvider">
<tag name="maker.command" />
</service>

<service id="sylius.maker.state_processor" class="Sylius\Component\Resource\Symfony\Maker\MakeStateProcessor">
<tag name="maker.command" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function generateRepositoryClass(ClassNameDetails $repositoryClassDetails
{
return $this->generator->generateClass(
$repositoryClassDetails->getFullName(),
dirname(__DIR__, 2) . '/Bundle/Resources/config/skeleton/Repository.tpl.php',
dirname(__DIR__) . '/Resources/skeleton/Repository.tpl.php',
[
'entity_class_name' => $resourceClassDetails->getShortName(),
'entity_namespace' => Str::getNamespace($resourceClassDetails->getFullName()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function generateResourceClass(

return $this->generator->generateClass(
$resourceClassDetails->getFullName(),
dirname(__DIR__, 2) . '/Bundle/Resources/config/skeleton/' . $skeletonName,
dirname(__DIR__) . '/Resources/skeleton/' . $skeletonName,
[
'class_name_without_suffix' => $shortName,
'show_template_dir' => \strtolower($shortName),
Expand Down
19 changes: 19 additions & 0 deletions src/Component/Symfony/Maker/MakeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,45 @@ public function __construct(
) {
}

/**
* {@inheritdoc}
*/
public static function getCommandName(): string
{
return 'make:resource';
}

/**
* {@inheritdoc}
*/
public static function getCommandDescription(): string
{
return 'Creates a Resource class';
}

/**
* {@inheritdoc}
*/
public function configureCommand(Command $command, InputConfiguration $inputConfig): void
{
$command
->setDescription(self::getCommandDescription())
->addArgument('name', InputArgument::OPTIONAL, 'Class name of the resource to create')
->addOption('is-entity', null, InputOption::VALUE_NONE, 'Do you want to store resource data in the database (via Doctrine)?')
->setHelp(file_get_contents(__DIR__.'/Resources/help/MakeResource.txt'))
;
}

/**
* {@inheritdoc}
*/
public function configureDependencies(DependencyBuilder $dependencies): void
{
}

/**
* {@inheritdoc}
*/
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
{
$resourceIsEntity = $io->confirm(
Expand All @@ -79,6 +95,9 @@ class_exists(DoctrineBundle::class),
}
}

/**
* {@inheritdoc}
*/
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
/** @var string $name */
Expand Down
82 changes: 82 additions & 0 deletions src/Component/Symfony/Maker/MakeStateProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Component\Resource\Symfony\Maker;

use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;

final class MakeStateProcessor extends AbstractMaker
{
/**
* {@inheritdoc}
*/
public static function getCommandName(): string
{
return 'make:sylius-state-processor';
}

/**
* {@inheritdoc}
*/
public static function getCommandDescription(): string
{
return 'Creates a Sylius state processor';
}

/**
* {@inheritdoc}
*/
public function configureCommand(Command $command, InputConfiguration $inputConfig): void
{
$command
->addArgument('name', InputArgument::REQUIRED, 'Choose a class name for your state processor (e.g. <fg=yellow>AwesomeStateProcessor</>)')
->setHelp(file_get_contents(__DIR__.'/Resources/help/MakeStateProcessor.txt'))
;
}

/**
* {@inheritdoc}
*/
public function configureDependencies(DependencyBuilder $dependencies): void
{
}

/**
* {@inheritdoc}
*/
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
$stateProcessorClassNameDetails = $generator->createClassNameDetails(
$input->getArgument('name'),
'State\\'
);

$generator->generateClass(
$stateProcessorClassNameDetails->getFullName(),
__DIR__.'/Resources/skeleton/StateProcessor.tpl.php'
);
$generator->writeChanges();

$this->writeSuccessMessage($io);
$io->text([
'Next: Open your new state processor class and start customizing it.',
]);
}
}
82 changes: 82 additions & 0 deletions src/Component/Symfony/Maker/MakeStateProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Component\Resource\Symfony\Maker;

use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;

final class MakeStateProvider extends AbstractMaker
{
/**
* {@inheritdoc}
*/
public static function getCommandName(): string
{
return 'make:sylius-state-provider';
}

/**
* {@inheritdoc}
*/
public static function getCommandDescription(): string
{
return 'Creates a Sylius state provider';
}

/**
* {@inheritdoc}
*/
public function configureCommand(Command $command, InputConfiguration $inputConfig): void
{
$command
->addArgument('name', InputArgument::REQUIRED, 'Choose a class name for your state provider (e.g. <fg=yellow>AwesomeStateProvider</>)')
->setHelp(file_get_contents(__DIR__.'/Resources/help/MakeStateProvider.txt'))
;
}

/**
* {@inheritdoc}
*/
public function configureDependencies(DependencyBuilder $dependencies): void
{
}

/**
* {@inheritdoc}
*/
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
$stateProviderClassNameDetails = $generator->createClassNameDetails(
$input->getArgument('name'),
'State\\'
);

$generator->generateClass(
$stateProviderClassNameDetails->getFullName(),
__DIR__.'/Resources/skeleton/StateProvider.tpl.php'
);
$generator->writeChanges();

$this->writeSuccessMessage($io);
$io->text([
'Next: Open your new state provider class and start customizing it.',
]);
}
}
5 changes: 5 additions & 0 deletions src/Component/Symfony/Maker/Resources/help/MakeResource.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The <info>%command.name%</info> command generates a new Sylius resource class.

<info>php %command.full_name% Book</info>

If the argument is missing, the command will ask for the class name interactively.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The <info>%command.name%</info> command generates a new Sylius state processor class.

<info>php %command.full_name% AwesomeStateProcessor</info>

If the argument is missing, the command will ask for the class name interactively.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The <info>%command.name%</info> command generates a new Sylius state provider class.

<info>php %command.full_name% AwesomeStateProvider</info>

If the argument is missing, the command will ask for the class name interactively.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);
echo "<?php\n"; ?>

namespace <?php echo $namespace; ?>;

use Sylius\Component\Resource\Context\Context;
use Sylius\Component\Resource\Metadata\Operation;
use Sylius\Component\Resource\State\ProcessorInterface;

final class <?php echo $class_name; ?> implements ProcessorInterface
{
public function process(mixed $data, Operation $operation, Context $context): mixed
{
// Handle the state

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);
echo "<?php\n"; ?>

namespace <?php echo $namespace; ?>;

use Sylius\Component\Resource\Context\Context;
use Sylius\Component\Resource\Metadata\Operation;
use Sylius\Component\Resource\State\ProviderInterface;

final class <?php echo $class_name; ?> implements ProviderInterface
{
public function provide(Operation $operation, Context $context): object|iterable|null
{
// Retrieve the state from somewhere

return null;
}
}
68 changes: 68 additions & 0 deletions src/Component/Tests/Symfony/Maker/MakeStateProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Component\Resource\Tests\Symfony\Maker;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

final class MakeStateProcessorTest extends MakerTestCase
{
private const CREATE_BOOK_PROCESSOR_PATH = 'tmp/Sylius/State/CreateBookProcessor.php';

/** @test */
public function it_can_create_state_processors(): void
{
$tester = new CommandTester((new Application(self::bootKernel()))->find('make:sylius-state-processor'));

$this->assertFileDoesNotExist(self::file(self::CREATE_BOOK_PROCESSOR_PATH));

$tester->execute(['name' => '\\App\\Tests\\Tmp\\Sylius\\State\\CreateBookProcessor']);

$this->assertFileExists(self::file(self::CREATE_BOOK_PROCESSOR_PATH));
$this->assertSame(self::getCreateBookProcessorExpectedContent(), \file_get_contents(self::file(self::CREATE_BOOK_PROCESSOR_PATH)));
}

private static function getCreateBookProcessorExpectedContent(): string
{
return <<<EOF
<?php
namespace App\Tests\Tmp\Sylius\State;
use Sylius\Component\Resource\Context\Context;
use Sylius\Component\Resource\Metadata\Operation;
use Sylius\Component\Resource\State\ProcessorInterface;
final class CreateBookProcessor implements ProcessorInterface
{
public function process(mixed \$data, Operation \$operation, Context \$context): mixed
{
// Handle the state
return null;
}
}
EOF
;
}

/**
* @before
*/
protected function removeGeneratedFiles(): void
{
$this->removeFile(self::file(self::CREATE_BOOK_PROCESSOR_PATH));
}
}
Loading

0 comments on commit 9528dfd

Please sign in to comment.