Skip to content

Commit

Permalink
refactor #777 Move some namespaces on Factory (loic425)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.11 branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Bug fix?        | no
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | 
| License         | MIT


Commits
-------

263f3c1 Move some namespaces on Factory
fccf05a Fix imports on doc
f713e8b Fix PHPStan & Psalm errors
8c79536 Use class string
64b49e1 bc layer for translatable factory interface
3f53fbc Fix coding standard
  • Loading branch information
lchrusciel authored Nov 13, 2023
2 parents 00c1f96 + 3f53fbc commit bbbdac9
Show file tree
Hide file tree
Showing 32 changed files with 313 additions and 109 deletions.
7 changes: 3 additions & 4 deletions docs/resource_factories.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ declare(strict_types=1);
namespace App\Factory;

use App\Entity\Book;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Resource\Factory\FactoryInterface;

final class BookFactory implements FactoryInterface
{
Expand Down Expand Up @@ -61,9 +61,8 @@ declare(strict_types=1);
namespace App\Factory;

use App\Entity\Book;
use Sylius\Resource\Factory\FactoryInterface;
use Symfony\Component\Security\Core\Security;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;

final class BookFactory implements FactoryInterface
{
Expand Down Expand Up @@ -130,8 +129,8 @@ declare(strict_types=1);
namespace App\Factory;

use App\Entity\Book;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Resource\Factory\FactoryInterface;

final class BookFactory implements FactoryInterface
{
Expand Down
1 change: 1 addition & 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/Annotation/*
- %currentWorkingDirectory%/src/Component/Factory/*
- %currentWorkingDirectory%/src/Component/Reflection/ClassReflection.php
- %currentWorkingDirectory%/src/Component/spec/*
- %currentWorkingDirectory%/src/Component/Storage/*
Expand Down
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
<TypeDoesNotContainType>
<errorLevel type="suppress">
<directory name="src/Component/Annotation" />
<directory name="src/Component/Factory" />
<directory name="src/Component/Storage" />
<file name="src/Bundle/Event/ResourceControllerEvent.php" />
</errorLevel>
Expand All @@ -209,6 +210,7 @@

<UnrecognizedStatement>
<errorLevel type="suppress">
<directory name="src/Component/Factory" />
<directory name="src/Component/Storage" />
</errorLevel>
</UnrecognizedStatement>
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/Controller/NewResourceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

namespace Sylius\Bundle\ResourceBundle\Controller;

use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Resource\Factory\FactoryInterface;

final class NewResourceFactory implements NewResourceFactoryInterface
{
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/Controller/NewResourceFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

namespace Sylius\Bundle\ResourceBundle\Controller;

use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Resource\Factory\FactoryInterface;

interface NewResourceFactoryInterface
{
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/Controller/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
use Sylius\Component\Resource\Exception\DeleteHandlingException;
use Sylius\Component\Resource\Exception\UpdateHandlingException;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Metadata\MetadataInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Resource\ResourceActions;
use Sylius\Resource\Factory\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\Bundle\ResourceBundle\Form\Type\DefaultResourceType;
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
use Sylius\Component\Resource\Factory\Factory;
use Sylius\Resource\Factory\Factory;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
Expand Down
4 changes: 2 additions & 2 deletions src/Bundle/DependencyInjection/Driver/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Driver;

use Sylius\Component\Resource\Factory\Factory;
use Sylius\Component\Resource\Factory\TranslatableFactoryInterface;
use Sylius\Component\Resource\Metadata\Metadata;
use Sylius\Component\Resource\Metadata\MetadataInterface;
use Sylius\Resource\Factory\Factory;
use Sylius\Resource\Factory\TranslatableFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
Expand Down
9 changes: 7 additions & 2 deletions src/Bundle/DependencyInjection/SyliusResourceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
use Sylius\Bundle\ResourceBundle\DependencyInjection\Driver\DriverProvider;
use Sylius\Bundle\ResourceBundle\Form\Type\DefaultResourceType;
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
use Sylius\Component\Resource\Factory\Factory;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface as LegacyFactoryInterface;
use Sylius\Component\Resource\Metadata\Metadata;
use Sylius\Component\Resource\Reflection\ClassReflection;
use Sylius\Resource\Factory\Factory;
use Sylius\Resource\Factory\FactoryInterface;
use Sylius\Resource\Metadata\AsResource;
use Sylius\Resource\Metadata\ResourceMetadata;
use Sylius\Resource\State\ProcessorInterface;
Expand Down Expand Up @@ -86,6 +87,10 @@ public function load(array $configs, ContainerBuilder $container): void
->addTag('sylius.resource_factory')
;

$container->registerForAutoconfiguration(LegacyFactoryInterface::class)
->addTag('sylius.resource_factory')
;

$container->registerForAutoconfiguration(ContextFactoryInterface::class)
->addTag('sylius.twig_context_factory')
;
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/spec/Controller/NewResourceFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ResourceBundle\Controller\NewResourceFactoryInterface;
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Resource\Factory\FactoryInterface;

final class NewResourceFactorySpec extends ObjectBehavior
{
Expand Down
2 changes: 1 addition & 1 deletion src/Bundle/spec/Controller/ResourceControllerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
use Sylius\Bundle\ResourceBundle\Controller\ViewHandlerInterface;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
use Sylius\Component\Resource\Exception\DeleteHandlingException;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Metadata\MetadataInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Resource\ResourceActions;
use Sylius\Resource\Factory\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\Form;
Expand Down
22 changes: 4 additions & 18 deletions src/Component/Factory/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,13 @@

namespace Sylius\Component\Resource\Factory;

/**
* Creates resources based on theirs FQCN.
*/
final class Factory implements FactoryInterface
{
/**
* @var string
* @psalm-var class-string
*/
private $className;
class_exists(\Sylius\Resource\Factory\Factory::class);

if (false) {
/**
* @psalm-param class-string $className
* @inheritDoc
*/
public function __construct(string $className)
{
$this->className = $className;
}

public function createNew()
final class Factory extends \Sylius\Resource\Factory\Factory
{
return new $this->className();
}
}
14 changes: 7 additions & 7 deletions src/Component/Factory/FactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

namespace Sylius\Component\Resource\Factory;

/**
* @template T of object
*/
interface FactoryInterface
{
interface_exists(\Sylius\Resource\Factory\FactoryInterface::class);

if (false) {
/**
* @return T
* @inheritDoc
*/
public function createNew();
interface FactoryInterface extends \Sylius\Resource\Factory\FactoryInterface
{
}
}
32 changes: 3 additions & 29 deletions src/Component/Factory/TranslatableFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,10 @@

namespace Sylius\Component\Resource\Factory;

use Sylius\Component\Resource\Exception\UnexpectedTypeException;
use Sylius\Component\Resource\Model\TranslatableInterface;
use Sylius\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
class_exists(\Sylius\Resource\Factory\TranslatableFactory::class);

final class TranslatableFactory implements TranslatableFactoryInterface
{
private FactoryInterface $factory;

private TranslationLocaleProviderInterface $localeProvider;

public function __construct(FactoryInterface $factory, TranslationLocaleProviderInterface $localeProvider)
{
$this->factory = $factory;
$this->localeProvider = $localeProvider;
}

/**
* @throws UnexpectedTypeException
*/
public function createNew()
if (false) {
final class TranslatableFactory extends \Sylius\Resource\Factory\TranslatableFactory
{
$resource = $this->factory->createNew();

if (!$resource instanceof TranslatableInterface) {
throw new UnexpectedTypeException($resource, TranslatableInterface::class);
}

$resource->setCurrentLocale($this->localeProvider->getDefaultLocaleCode());
$resource->setFallbackLocale($this->localeProvider->getDefaultLocaleCode());

return $resource;
}
}
18 changes: 11 additions & 7 deletions src/Component/Factory/TranslatableFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@

namespace Sylius\Component\Resource\Factory;

/**
* @template T of object
*/
interface TranslatableFactoryInterface extends FactoryInterface
{
interface_exists(\Sylius\Resource\Factory\TranslatableFactoryInterface::class);

if (false) {
/**
* @return T
* @inheritDoc
*/
public function createNew();
interface TranslatableFactoryInterface extends \Sylius\Resource\Factory\TranslatableFactoryInterface
{
/**
* @inheritDoc
*/
public function createNew();
}
}
14 changes: 13 additions & 1 deletion src/Component/spec/Factory/FactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
namespace spec\Sylius\Component\Resource\Factory;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface as LegacyFactoryInterface;
use Sylius\Resource\Factory\Factory as NewFactory;
use Sylius\Resource\Factory\FactoryInterface;

final class FactorySpec extends ObjectBehavior
{
Expand All @@ -28,6 +30,16 @@ function it_implements_factory_interface(): void
$this->shouldHaveType(FactoryInterface::class);
}

function it_implements_legacy_factory_interface(): void
{
$this->shouldHaveType(LegacyFactoryInterface::class);
}

function it_is_an_alias_of_the_factory(): void
{
$this->shouldBeAnInstanceOf(NewFactory::class);
}

function it_creates_a_new_instance_of_a_resource(): void
{
$this->createNew()->shouldHaveType(\stdClass::class);
Expand Down
33 changes: 9 additions & 24 deletions src/Component/spec/Factory/TranslatableFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
namespace spec\Sylius\Component\Resource\Factory;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Resource\Exception\UnexpectedTypeException;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Factory\TranslatableFactoryInterface;
use Sylius\Component\Resource\Model\TranslatableInterface;
use Sylius\Component\Resource\Factory\TranslatableFactoryInterface as LegacyTranslatableFactoryInterface;
use Sylius\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
use Sylius\Resource\Factory\FactoryInterface;
use Sylius\Resource\Factory\TranslatableFactory as NewTranslatableFactory;
use Sylius\Resource\Factory\TranslatableFactoryInterface;

final class TranslatableFactorySpec extends ObjectBehavior
{
Expand All @@ -32,28 +32,13 @@ function it_implements_translatable_factory_interface(): void
$this->shouldImplement(TranslatableFactoryInterface::class);
}

function it_throws_an_exception_if_resource_is_not_translatable(FactoryInterface $factory, \stdClass $resource): void
function it_implements_legacy_translatable_factory_interface(): void
{
$factory->createNew()->willReturn($resource);

$this
->shouldThrow(UnexpectedTypeException::class)
->during('createNew')
;
$this->shouldImplement(LegacyTranslatableFactoryInterface::class);
}

function it_creates_translatable_and_sets_locales(
FactoryInterface $factory,
TranslationLocaleProviderInterface $localeProvider,
TranslatableInterface $resource,
): void {
$localeProvider->getDefaultLocaleCode()->willReturn('pl_PL');

$factory->createNew()->willReturn($resource);

$resource->setCurrentLocale('pl_PL')->shouldBeCalled();
$resource->setFallbackLocale('pl_PL')->shouldBeCalled();

$this->createNew()->shouldReturn($resource);
function it_should_be_an_alias_of_translatable_factory_interface(): void
{
$this->shouldBeAnInstanceOf(NewTranslatableFactory::class);
}
}
38 changes: 38 additions & 0 deletions src/Component/src/Factory/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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\Resource\Factory;

/**
* Creates resources based on theirs FQCN.
*/
final class Factory implements FactoryInterface
{
/** @var class-string */
private $className;

/**
* @param class-string $className
*/
public function __construct(string $className)
{
$this->className = $className;
}

public function createNew()
{
return new $this->className();
}
}

class_alias(Factory::class, \Sylius\Component\Resource\Factory\Factory::class);
Loading

0 comments on commit bbbdac9

Please sign in to comment.