Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GridBundle] Impossible to create new Grid Driver #61

Open
Roshyo opened this issue Jan 31, 2019 · 8 comments
Open

[GridBundle] Impossible to create new Grid Driver #61

Roshyo opened this issue Jan 31, 2019 · 8 comments

Comments

@Roshyo
Copy link
Contributor

Roshyo commented Jan 31, 2019

Sylius version affected: 1.x (I believe all of them, from the 1.0. 1.3 for sure)

Description
In order to fetch entities from an API and then display them in a grid, we have to create at least a new Driver and register it.
The thing is, when trying to register the new Driver in SyliusGrid, this exception is thrown :

The value "your/driver" is not allowed for path "sylius_resource.drivers.1". Permissible values: "doctrine/orm", "doctrine/mongodb-odm", "doctrine/phpcr-odm"

This is due to this in Sylius\Bundle\ResourceBundle\DependencyInjection\Configuration.php

...
    /**
     * @param ArrayNodeDefinition $node
     */
    private function addDriversSection(ArrayNodeDefinition $node): void
    {
        $node
            ->children()
                ->arrayNode('drivers')
                    ->defaultValue([SyliusResourceBundle::DRIVER_DOCTRINE_ORM])
                    ->enumPrototype()->values(SyliusResourceBundle::getAvailableDrivers())->end()
                ->end()
            ->end()
        ;
    }
...

So the first step is (do not reproduce at home) to override the availableDrivers and add our (all done in vendor...)

Then, in Sylius\Bundle\ResourceBundle\AbstractResourceBundle, there is a switch/case to also mpodify and add our new value.
Next step (nearly last one) we also have to override the switch/case in Sylius\Bundle\ResourceBundle\DependencyInjection\Driver\DriverProvider

And to finish, we are warned that the XML file of our provider does not exist. So we create a new one in
Sylius/resource-bundle/Resources/config/services/integration/your/driver.xml

Steps to reproduce
Install Sylius (GridBundle at least) and try to add a new Provider

Possible Solution
Be more permissive in this bundle configuration. It is just one step away from being able to fetch data in API and display them in a grid.

@Prometee
Copy link

Prometee commented Jan 31, 2019

If someone get here is possible to do it but not easily.

First into your class *Plugin extends Bundle implements ResourceBundleInterface
add a new SupportedDriver :

public const MY_API_DRIVER = 'my_vendor/my_api_driver';

public function getSupportedDrivers(): array
{
    return [
        self::MY_API_DRIVER,
        SyliusResourceBundle::DRIVER_DOCTRINE_ORM
    ];
}

Then an override have to be made into the DependencyInjection\*Extension extends AbstractResourceExtension to give your own logic of how to get the Entity, the Repository and the Manager, then make a new DependencyInjection\Driver to handle each of those points.
Build the doctrine like Driver with 3 classes :

  1. Driver\Driver handling a Registry/ManagerRegistry, and the Driver/DataSource
  2. Driver\DataSource (Transport of data between the API service and the view)
  3. Driver\ExpressionBuilder manage or and, etc.. to allow you to sort and filter the results into the Driver\DataSource

And finally the Custom Repository(ies) extending Sylius\Component\Resource\Repository\InMemoryRepository and using your API service to find, findBy, etc every Entity you need

After that you will be able to create your APIEntity (like a Doctrine one), your Form\Type and configure your grid (using driver: 'my_vendor/my_custom_driver')

I would like to be able to create a sylius-plugin or a library with an exemple to handle this specific thing, but it's not possible right now... Ping me if you want code exemples.

@pamil
Copy link
Contributor

pamil commented Feb 1, 2019

Would the similar mechanism to the one from ThemeBundle work for you: https://docs.sylius.com/en/1.3/components_and_bundles/bundles/SyliusThemeBundle/configuration_sources/creating_custom.html?

@Roshyo
Copy link
Contributor Author

Roshyo commented Feb 1, 2019

According to the doc, it requires a Bundle. Couldn't it be done without ? Symfony 4 advice to do everything in src without any bundle.
Except this point, I guess it can work.

@cornobils
Copy link
Contributor

@Roshyo You can register driver without a bundle with:

App\Mssql\Driver:
        tags:
            - { name: "sylius.grid_driver", alias: "mssql" }

@Roshyo
Copy link
Contributor Author

Roshyo commented Mar 22, 2019

I don't think so since the list of allowed drivers is fixed in the vendor files. As described above

@stale
Copy link

stale bot commented Jun 20, 2019

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in a week if no further activity occurs. Thank you for your contributions.

@stale stale bot closed this as completed Jun 27, 2019
@vvasiloi
Copy link
Contributor

This should be reopened and moved to grid repository.

@lchrusciel lchrusciel reopened this Jun 16, 2020
@lchrusciel lchrusciel transferred this issue from Sylius/Sylius Jun 16, 2020
@mbabker
Copy link
Contributor

mbabker commented Mar 14, 2022

#61 (comment) has it right, you just need a service with the right DI tag and you should be fine. With that said...

After a question in Slack this morning, I'm starting to wonder what the value is of the drivers config node in the first place.

Reverse engineering the code, the bundle only uses it to "smartly" decide which grid driver services to load. There are better ways to decide whether services should be loaded, and with Sylius moving toward a Symfony 5.4 minimum requirement, the ContainerBuilder::willBeAvailable() method was added during the Symfony 5 lifecycle for exactly this type of scenario (detecting at compile time if dependencies will be available).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants