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

Make a db_driver configuration parameter optional with no_driver default value #1420

Merged
merged 4 commits into from
Apr 26, 2018

Conversation

covex-nn
Copy link
Contributor

@covex-nn covex-nn commented Apr 20, 2018

I am targeting this branch, because this is BC.

Closes #1419

Changelog

### Changed
- A db_driver configuration parameter is optional now with 'no_driver' default value

To do

  • Add tests

Subject

To create a recipe for Symfony Flex, it should be possible to create a default configuration, that allows to run cache:clear without errors (see sonata-project/dev-kit#409) A default value for db_driver configuration parameter will allow this.

If this PR will be accepted, a default configuration, i.e. a recipe for sonata-media/media-bundle could be like this (please fix me, it there something wrong here; may be providers or formats sections should be changed to more commonly used?):

sonata_media:
    default_context: default
    contexts:
        default:
            providers:
                - sonata.media.provider.dailymotion
                - sonata.media.provider.youtube
                - sonata.media.provider.image
                - sonata.media.provider.file
                - sonata.media.provider.vimeo

            formats:
                small: { width: 100 , quality: 70}
                big:   { width: 500 , quality: 70}

    cdn:
        server:
            path: /upload/media

    filesystem:
        local:
            directory: "%kernel.project_dir%/public/upload/media"
            create: false

Additional configuration, i.e. a recipe for a new package sonata-project/media-orm-pack could be like this (i created a gist with entity classes):

sonata_media:
    class:
        media: App\Entity\SonataMediaMedia
        gallery: App\Entity\SonataMediaGallery
        gallery_has_media: App\Entity\SonataMediaGalleryHasMedia
    db_driver: doctrine_orm


use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\SonataMediaBundle;

Copy link
Contributor

Choose a reason for hiding this comment

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

@author is missing

/**
* @internal
*/
class NoDriverManager implements ManagerInterface
Copy link
Contributor

Choose a reason for hiding this comment

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

@author

SonataMediaBundle::noDriverRuntimeException();
}

public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
Copy link
Contributor

Choose a reason for hiding this comment

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

please add comments about types of vars

SonataMediaBundle::noDriverRuntimeException();
}

public function find($id)
Copy link
Contributor

Choose a reason for hiding this comment

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

comment about type

SonataMediaBundle::noDriverRuntimeException();
}

public function save($entity, $andFlush = true)
Copy link
Contributor

Choose a reason for hiding this comment

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

comment about type

SonataMediaBundle::noDriverRuntimeException();
}

public function delete($entity, $andFlush = true)
Copy link
Contributor

Choose a reason for hiding this comment

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

comment about type

@@ -99,7 +99,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('serializer.xml');
$loader->load('command.xml');

if (!in_array(strtolower($config['db_driver']), ['doctrine_orm', 'doctrine_mongodb', 'doctrine_phpcr'])) {
if (!in_array(strtolower($config['db_driver']), ['doctrine_orm', 'doctrine_mongodb', 'doctrine_phpcr', 'no_driver'])) {
Copy link
Contributor

@kunicmarko20 kunicmarko20 Apr 20, 2018

Choose a reason for hiding this comment

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

can we extract this array into a const maybe, what do @sonata-project/contributors think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here is how it is done in FOSUserBundle, we can move this check to Sonata\NotificationBundle\DependencyInjection\Configuration and add constant there

Copy link
Contributor

Choose a reason for hiding this comment

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

Ye, I would like that more, let's see what others say.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds better, if we don't produce any BC breaks I would prefer that way too

Copy link
Contributor

Choose a reason for hiding this comment

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

ah, forgot about this @covex-nn can you move this without doing a BC break?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! i'll do it a little bit later (i must go right now)

Copy link
Contributor

Choose a reason for hiding this comment

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

Blocked merge, thank you for helping!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kunicmarko20 all done, please review )


public function findAll()
{
SonataMediaBundle::noDriverRuntimeException();
Copy link
Member

Choose a reason for hiding this comment

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

Why don't we generate a new Exception class for this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, sounds better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This error will be shown to developer in a browser, if a pack with additional configuration was not installed. Here is a screenshot how it is decorated by symfony/debug in dev environment:

Error message

That message could be updated! We could add instructions how to install a pack =)
But, i agree, that method should be removed. Can i use Sonata\CoreBundle\Exception\InvalidParameterException?

Copy link
Contributor

Choose a reason for hiding this comment

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

I thought more about new exception class, NoDriverException?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added new NoDriverException

{
$this->expectException('Sonata\MediaBundle\Exception\NoDriverException');

$manager = new NoDriverManager();
Copy link
Contributor

Choose a reason for hiding this comment

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

this var is not needed, can you just move new NoDriverManager() to method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed =)

*/
public function testException($method, array $arguments)
{
$this->expectException('Sonata\MediaBundle\Exception\NoDriverException');
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you use NoDriverException::class

{
public function testException()
{
$this->expectException('Sonata\MediaBundle\Exception\NoDriverException');
Copy link
Contributor

Choose a reason for hiding this comment

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

same here, use ::class

Copy link
Contributor

@kunicmarko20 kunicmarko20 left a comment

Choose a reason for hiding this comment

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

blocked to avoid merge

->defaultValue('no_driver')
->validate()
->ifNotInArray(self::DB_DRIVERS)
->thenInvalid('SonataMediaBundle - Invalid db driver %s.')
Copy link
Contributor

Choose a reason for hiding this comment

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

will it throw the same exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before my changes there was \InvalidArgumentException, and now it will throw a same exception, see https://github.com/symfony/config/blob/master/Definition/Builder/ExprBuilder.php#L187, but before container compilation started

Copy link
Contributor

Choose a reason for hiding this comment

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

good

kunicmarko20
kunicmarko20 previously approved these changes Apr 20, 2018
Copy link
Contributor

@kunicmarko20 kunicmarko20 left a comment

Choose a reason for hiding this comment

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

Great work!

@kunicmarko20 kunicmarko20 requested a review from a team April 20, 2018 13:49
/**
* @author Andrey F. Mindubaev <[email protected]>
*/
class NoDriverException extends \RuntimeException
Copy link
Member

Choose a reason for hiding this comment

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

final?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

*
* @author Andrey F. Mindubaev <[email protected]>
*/
class NoDriverGenerator implements GeneratorInterface
Copy link
Member

Choose a reason for hiding this comment

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

final?

*
* @author Andrey F. Mindubaev <[email protected]>
*/
class NoDriverManager implements ManagerInterface
Copy link
Member

Choose a reason for hiding this comment

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

final?

kunicmarko20
kunicmarko20 previously approved these changes Apr 23, 2018
@kunicmarko20 kunicmarko20 requested review from jordisala1991 and core23 and removed request for core23 April 23, 2018 11:27
jordisala1991
jordisala1991 previously approved these changes Apr 23, 2018
@covex-nn
Copy link
Contributor Author

I pushed a commit with recipe for MediaBundle; after new version will be released i will create a PR

@kunicmarko20
Copy link
Contributor

I think we need to mention that uploads folder should be writable

@@ -32,7 +37,13 @@ public function getConfigTreeBuilder()

$node
->children()
->scalarNode('db_driver')->isRequired()->end()
->scalarNode('db_driver')
->defaultValue('no_driver')
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a call to info()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added ->info('Choose persistence mechanism driver from the following list: "doctrine_orm", "doctrine_mongodb", "doctrine_phpcr"')

{
const DEFAULT_MESSAGE = 'The child node "db_driver" at path "sonata_media" must be configured.';

public function __construct($message = self::DEFAULT_MESSAGE, $code = 0, \Throwable $previous = null)
Copy link
Contributor

Choose a reason for hiding this comment

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

\Throwable is not available with php 5

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

*/
final class NoDriverException extends \RuntimeException
{
const DEFAULT_MESSAGE = 'The child node "db_driver" at path "sonata_media" must be configured.';
Copy link
Contributor

Choose a reason for hiding this comment

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

Why put this in a constant? It's public now, and it's only ever used once.

Copy link
Contributor Author

@covex-nn covex-nn Apr 25, 2018

Choose a reason for hiding this comment

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

This is a default value for message. I set it once and use it each time when i throw this exception just with new NoDriverException(); without setting first argument.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah but why not do this: https://3v4l.org/WmvS8 or this: https://3v4l.org/ROnrq?

Copy link
Contributor Author

@covex-nn covex-nn Apr 25, 2018

Choose a reason for hiding this comment

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

Just i did not think, that it is important. Next time i will try not to create new not necessary entities. Fixed

<services>
<service id="sonata.media.manager.media" class="Sonata\MediaBundle\Model\NoDriverManager"/>
<service id="sonata.media.manager.gallery" class="Sonata\MediaBundle\Model\NoDriverManager"/>
<service id="sonata.media.generator.default" class="Sonata\MediaBundle\Generator\NoDriverGenerator"/>
Copy link
Contributor

Choose a reason for hiding this comment

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

Please mark service than can be private as such

Copy link
Contributor Author

@covex-nn covex-nn Apr 25, 2018

Choose a reason for hiding this comment

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

@greg0ire only sonata.media.generator.default can be private, two others must be public. I added public="true" there

@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
</container>
Copy link
Contributor

Choose a reason for hiding this comment

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

?

Copy link
Contributor Author

@covex-nn covex-nn Apr 25, 2018

Choose a reason for hiding this comment

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

This file is loaded from SonataMediaExtension.php when SonataAdminBundle is enabled. So, i created an empty file for that case. <container /> tag can be empty. See http://symfony.com/schema/dic/services/services-1.0.xsd

Copy link
Contributor

@greg0ire greg0ire Apr 25, 2018

Choose a reason for hiding this comment

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

Please add an XML comment to explain this, hopefully xmllint will not remove it.

@@ -1,3 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!--
This file will be loaded when SonataAdminBundle is enabled and db_driver has it's default value, not corresponding to real persistence mechanism.
Copy link
Contributor

Choose a reason for hiding this comment

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

  • it's => its
  • linebreak that line 4

and we're good

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

greg0ire
greg0ire previously approved these changes Apr 25, 2018
@greg0ire greg0ire merged commit 4e9be09 into sonata-project:3.x Apr 26, 2018
@greg0ire
Copy link
Contributor

Thanks @covex-nn !

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

Successfully merging this pull request may close these issues.

[Proposal] A recipe for Symfony Flex
6 participants