Hi,
following the docs I wanted to customize the ProductFactory. I did everything like in the docs and got an error that parameter 1 of constructor was TranslableFactory not one implementing the ProductFactoryInterface. So I changed my service definition to:
services:
app.factory.product:
class: SyliusExtensionBundle\Factory\ProductFactory
decorates: sylius.custom_factory.product
arguments: ['@app.factory.product.inner']
public: false
I decorated sylius.custom_factory.product not thesylius.factory.product itself. This workarounds works fine, but as I check in configuration sylius.custom_factory.product is a decorator of sylius.factory.product. I read in symfony docs, there can be multiple decorators "stacked" each other like here:
foo:
class: Foo
bar:
class: Bar
public: false
decorates: foo
decoration_priority: 5
arguments: ['@bar.inner']
baz:
class: Baz
public: false
decorates: foo
decoration_priority: 1
arguments: ['@baz.inner']
And the decorator should be private and referenced throw the base one (foo in our example) - following this pattern I shouldn't decorates sylius.custom_factory.product. Could you tell me if I do right or there is some bug (in my code or sylius) why I can decorate sylius.factoy.product?
Hi @starspire While doing everything like in the docs, where do you get the error? I mean, what are you doing?
Because I can't reproduce :<
Hi @TheMadeleine,
I'll try to help you reproduce that - and maybe we will find a mistake in my understanding.
I get the following error:
Type error: Argument 1 passed to SyliusExtensionBundle\Factory\ProductFactory::__construct() must implement interface Sylius\Component\Product\Factory\ProductFactoryInterface, instance of Sylius\Component\Resource\Factory\TranslatableFactory given
When my services.yml is looking like yours:
services:
app.factory.product:
class: SyliusExtensionBundle\Factory\ProductFactory
decorates: sylius.factory.product
arguments: ['@app.factory.product.inner']
public: false
I get this error even on homepage. When I change sylius.factory.product to sylius.custom_factory.product everything goes fine:
services:
app.factory.product:
class: SyliusExtensionBundle\Factory\ProductFactory
decorates: sylius.custom_factory.product
arguments: ['@app.factory.product.inner']
public: false
My product factory:
<?php
namespace SyliusExtensionBundle\Factory;
use Sluggable\Fixture\TranslatableArticle;
use Sylius\Component\Product\Factory\ProductFactoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Factory\TranslatableFactoryInterface;
class ProductFactory implements ProductFactoryInterface {
/**
* @var FactoryInterface
*/
private $decoratedFactory;
/**
* @param ProductFactoryInterface $factory
*/
public function __construct(ProductFactoryInterface $factory)
{
$this->decoratedFactory = $factory;
}
/**
* {@inheritdoc}
*/
public function createNew()
{
return $this->decoratedFactory->createNew();
}
/**
* {@inheritdoc}
*/
public function createWithVariant()
{
return $this->decoratedFactory->createWithVariant();
}
public function createWithArchetype($archetype){
$product = $this->decoratedFactory->createNew();
$product->setArchetype($archetype);
return $product;
}
public function createWithArchetypeAndVariant($archetype)
{
$product = $this->decoratedFactory->createWithVariant();
$product->setArchetype($archetype);
return $product;
}
}
And the used version of Sylius is...? :)
@TheMadeleine, I'm using dev-master.
Well... So our situation should be exactly the same. And I do not get the error you are describing. I'll update to today's master and see what happens.
EDIT: nothing. I'm on current dev-master with your changes in my class exactly... and no errors 馃槙
One more question (not sure if it actually matters), but where do you define the factory service?
Lastly I'm doing that in services.yml, which is imported as last in config.yml. Then it should be able to override other service configurations
In fact... I have this issue also. :D
@pamil nailed it now :) It's a bug in Sylius. Prioritization problem.
@TheMadeleine At last :D It's not nice sylius has got a bug, but at least we reproduce this :+1:
@starspire let me know if it works as expected now :)
Now, it's working as expected. Good work, @pamil :+1:
Most helpful comment
Now, it's working as expected. Good work, @pamil :+1: