-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add make state provider and processor
- Loading branch information
Showing
16 changed files
with
380 additions
and
2 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
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,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.', | ||
]); | ||
} | ||
} |
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,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.', | ||
]); | ||
} | ||
} |
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,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. |
5 changes: 5 additions & 0 deletions
5
src/Component/Symfony/Maker/Resources/help/MakeStateProcessor.txt
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,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. |
5 changes: 5 additions & 0 deletions
5
src/Component/Symfony/Maker/Resources/help/MakeStateProvider.txt
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,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. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions
18
src/Component/Symfony/Maker/Resources/skeleton/StateProcessor.tpl.php
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); | ||
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; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Component/Symfony/Maker/Resources/skeleton/StateProvider.tpl.php
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); | ||
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
68
src/Component/Tests/Symfony/Maker/MakeStateProcessorTest.php
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,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)); | ||
} | ||
} |
Oops, something went wrong.