Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
"rector/rector": "^0.13.5",
"symfony/messenger": "^5.4 || ^6.0",
"symfony/serializer": "^5.4 || ^6.0",
"symfony/security-bundle": "^5.4 || ^6.0"
"symfony/security-bundle": "^5.4 || ^6.0",
"symfony/maker-bundle": "^1.48"
},
"suggest": {
"doctrine/orm": "^2.5",
Expand All @@ -107,7 +108,8 @@
"psr-4": {
"Sylius\\Bundle\\ResourceBundle\\spec\\": "src/Bundle/spec/",
"Sylius\\Component\\Resource\\spec\\": "src/Component/spec/",
"App\\": "tests/Application/src/"
"App\\": "tests/Application/src/",
"App\\Tests\\Tmp\\": "tests/Application/tmp/"
}
},
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ parameters:
- %currentWorkingDirectory%/src/Bundle/spec/*
- %currentWorkingDirectory%/src/Bundle/Tests/*
- %currentWorkingDirectory%/src/Component/Reflection/ClassReflection.php
- %currentWorkingDirectory%/src/Component/Symfony/Maker/Resources/skeleton/*
- %currentWorkingDirectory%/src/Component/spec/*
- %currentWorkingDirectory%/src/Component/vendor/*

Expand Down Expand Up @@ -80,6 +81,7 @@ parameters:
- '/Method Sylius\\Component\\Resource\\Model\\TimestampableInterface::set(Created|Updated)At\(\) has no return type specified./'
- '/Parameter #1 \$objectOrArray of method Symfony\\Component\\PropertyAccess\\PropertyAccessor::getValue\(\) expects array\|object, mixed given\./'
- '/Parameter #1 \$objectOrArray of method Symfony\\Component\\PropertyAccess\\PropertyAccessorInterface::getValue\(\) expects array\|object, mixed given\./'
- '/Parameter #1 \$help of method Symfony\\Component\\Console\\Command\\Command\:\:setHelp\(\) expects string, string\|false given\./'
- '/Parameter #4 \.\.\.\$values of function sprintf expects bool\|float\|int\|string\|null, mixed given\./'
- '/Parameter #1 \$submittedData of method Symfony\\Component\\Form\\FormInterface::submit\(\) expects array\|string\|null, mixed given\./'
- '/Parameter #2 \$callback of function preg_replace_callback expects callable\(array<int\|string, string>\): string, Closure\(array\): mixed given\./'
Expand Down
9 changes: 9 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<directory name="src/Bundle/Tests" />
<directory name="src/Component/spec" />
<directory name="src/Component/vendor" />
<directory name="src/Component/Symfony/Maker/Resources/skeleton" />
<file name="src/Bundle/Controller/ControllerTrait.php" />
<file name="src/Bundle/DependencyInjection/Driver/Doctrine/DoctrineODMDriver.php" />
<file name="src/Bundle/DependencyInjection/Driver/Doctrine/DoctrinePHPCRDriver.php" />
Expand Down Expand Up @@ -51,8 +52,15 @@
</errorLevel>
</DuplicateClass>

<InternalClass>
<errorLevel type="suppress">
<file name="src/Component/Symfony/Maker/MakeResource.php" />
</errorLevel>
</InternalClass>

<InternalMethod>
<errorLevel type="suppress">
<file name="src/Component/Symfony/Maker/MakeResource.php" />
<file name="src/Bundle/Controller/ParametersParser.php" />
<file name="src/Bundle/Controller/RequestConfiguration.php" />
<file name="src/Bundle/Controller/ResourceController.php" />
Expand Down Expand Up @@ -161,6 +169,7 @@
<file name="src/Component/Tests/Reflection/ClassInfoTraitTest.php" />
<file name="src/Component/Tests/Reflection/ClassReflectionTest.php" />
<file name="src/Component/Tests/Symfony/ExpressionLanguage/ArgumentParserTest.php" />
<directory name="src/Component/Tests/Symfony/Maker" />
<file name="src/Bundle/Validator/Constraints/UniqueWithinCollectionConstraint.php" />
<file name="src/Bundle/Validator/Constraints/Enabled.php" />
<file name="src/Bundle/Validator/Constraints/Disabled.php" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* 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\Bundle\ResourceBundle\DependencyInjection\Compiler;

use Sylius\Component\Resource\Symfony\Command\StubMakeResource;
use Sylius\Component\Resource\Symfony\Maker\MakeResource;
use Symfony\Bundle\MakerBundle\MakerBundle;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class RegisterStubCommandsPass implements CompilerPassInterface
{
/**
* @inheritdoc
*/
public function process(ContainerBuilder $container): void
{
if (!$this->isMakerEnabled($container)) {
$container->register(StubMakeResource::class)->setClass(StubMakeResource::class)->addTag('console.command');
$container->removeDefinition('sylius.maker.resource');
$container->removeAlias(MakeResource::class);
}
}

private function isMakerEnabled(ContainerBuilder $container): bool
{
if (!class_exists(MakerBundle::class)) {
return false;
}

/** @var array $bundles */
$bundles = $container->getParameter('kernel.bundles');

return in_array(MakerBundle::class, $bundles, true);
Comment on lines +38 to +45
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic skips case when the maker bundle is installed, but it turned off in the given env

}
}
1 change: 1 addition & 0 deletions src/Bundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<import resource="services/form.xml" />
<import resource="services/helper.xml" />
<import resource="services/listener.xml" />
<import resource="services/maker.xml" />
<import resource="services/metadata.xml" />
<import resource="services/routing.xml" />
<import resource="services/state.xml" />
Expand Down
40 changes: 40 additions & 0 deletions src/Bundle/Resources/config/services/maker.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--

This file is part of the Sylius package.

(c) Paweł Jędrzejewski
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--- (c) Paweł Jędrzejewski
+++ (c) Sylius Sp. z o.o.


For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.

-->

<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="sylius.maker.generator.resource" class="Sylius\Component\Resource\Symfony\Maker\Generator\ResourceClassGenerator">
<argument type="service" id="maker.generator" />
</service>

<service id="sylius.maker.generator.repository" class="Sylius\Component\Resource\Symfony\Maker\Generator\RepositoryClassGenerator">
<argument type="service" id="maker.generator" />
</service>

<service id="sylius.maker.resource" class="Sylius\Component\Resource\Symfony\Maker\MakeResource">
<argument type="service" id="maker.generator" />
<argument type="service" id="sylius.maker.generator.resource" />
<argument type="service" id="sylius.maker.generator.repository" />
<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>
2 changes: 2 additions & 0 deletions src/Bundle/SyliusResourceBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterResourcesPass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterResourceStateMachinePass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterStateMachinePass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterStubCommandsPass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\TwigPass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\WinzouStateMachinePass;
use Sylius\Bundle\ResourceBundle\DependencyInjection\PagerfantaExtension;
Expand Down Expand Up @@ -53,6 +54,7 @@ public function build(ContainerBuilder $container): void
$container->addCompilerPass(new RegisterResourceRepositoryPass());
$container->addCompilerPass(new RegisterResourcesPass());
$container->addCompilerPass(new RegisterStateMachinePass());
$container->addCompilerPass(new RegisterStubCommandsPass());
$container->addCompilerPass(new RegisterResourceStateMachinePass());
$container->addCompilerPass(new TwigPass());
$container->addCompilerPass(new WinzouStateMachinePass());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace DependencyInjection\Compiler;

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\RegisterStubCommandsPass;
use Sylius\Component\Resource\Symfony\Command\StubMakeResource;
use Sylius\Component\Resource\Symfony\Maker\MakeResource;
use Symfony\Bundle\MakerBundle\MakerBundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class RegisterStubCommandsPassTest extends AbstractCompilerPassTestCase
{
/** @test */
public function it_registers_stub_commands_when_maker_is_not_registered(): void
{
$this->compile();

$this->assertContainerBuilderHasService(StubMakeResource::class, StubMakeResource::class);
}

/** @test */
public function it_does_not_register_stub_commands_when_maker_is_registered(): void
{
$this->setParameter('kernel.bundles', [MakerBundle::class]);

$this->compile();

$this->assertContainerBuilderNotHasService(StubMakeResource::class);
}

/** @test */
public function it_unregisters_definition_for_grid_maker_when_maker_is_not_registered(): void
{
$this->registerService('sylius.maker.resource', MakeResource::class);

$this->compile();

$this->assertContainerBuilderNotHasService(MakeResource::class);
}

/** @test */
public function it_does_not_unregister_definition_for_grid_maker_when_maker_is_registered(): void
{
$this->setParameter('kernel.bundles', [MakerBundle::class]);
$this->registerService('sylius.maker.resource', MakeResource::class);

$this->compile();

$this->assertContainerBuilderHasService('sylius.maker.resource', MakeResource::class);
}

protected function registerCompilerPass(ContainerBuilder $container): void
{
$this->setParameter('kernel.bundles', []);

$container->addCompilerPass(new RegisterStubCommandsPass());
}
}
33 changes: 33 additions & 0 deletions src/Component/Symfony/Command/StubCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* 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\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

abstract class StubCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output)
{
(new SymfonyStyle($input, $output))
->error(
\sprintf("To run \"%s\" you need the \"%s\" which is currently not installed.\n\nTry running \"composer require %s\".", static::$defaultName ?? '', 'MakerBundle', 'symfony/maker-bundle --dev'),
)
;

return 0;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exit codes consts are available since Symfony 5.4, so it could/should be used over here, but I'm unsure if it's within scope of this PR.

    public const SUCCESS = 0;
    public const FAILURE = 1;
    public const INVALID = 2;
--- return 0;
+++ return Command::SUCCESS;

}
}
19 changes: 19 additions & 0 deletions src/Component/Symfony/Command/StubMakeResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* 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\Command;

final class StubMakeResource extends StubCommand
{
protected static $defaultName = 'make:resource';
}
41 changes: 41 additions & 0 deletions src/Component/Symfony/Maker/Generator/RepositoryClassGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* 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\Generator;

use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;

/**
* @internal
*/
final class RepositoryClassGenerator
{
public function __construct(
private Generator $generator,
) {
}

public function generateRepositoryClass(ClassNameDetails $repositoryClassDetails, ClassNameDetails $resourceClassDetails): string
{
return $this->generator->generateClass(
$repositoryClassDetails->getFullName(),
dirname(__DIR__) . '/Resources/skeleton/Repository.tpl.php',
[
'entity_class_name' => $resourceClassDetails->getShortName(),
'entity_namespace' => Str::getNamespace($resourceClassDetails->getFullName()),
],
);
}
}
Loading