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
Merged
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
14 changes: 13 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
*/
class Configuration implements ConfigurationInterface
{
/**
* NEXT_MAJOR: make constant protected/private.
*/
const DB_DRIVERS = ['doctrine_orm', 'doctrine_mongodb', 'doctrine_phpcr', 'no_driver'];

/**
* {@inheritdoc}
*/
Expand All @@ -32,7 +37,14 @@ 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"')

->info('Choose persistence mechanism driver from the following list: "doctrine_orm", "doctrine_mongodb", "doctrine_phpcr"')
->validate()
->ifNotInArray(self::DB_DRIVERS)
->thenInvalid('SonataMediaBundle - Invalid db driver %s.')
->end()
->end()
->scalarNode('default_context')->isRequired()->end()
->scalarNode('category_manager')
->defaultValue('sonata.media.manager.category.default')
Expand Down
4 changes: 0 additions & 4 deletions src/DependencyInjection/SonataMediaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ 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'])) {
throw new \InvalidArgumentException(sprintf('SonataMediaBundle - Invalid db driver "%s".', $config['db_driver']));
}

$bundles = $container->getParameter('kernel.bundles');

if (isset($bundles['FOSRestBundle'], $bundles['NelmioApiDocBundle'])) {
Expand Down
27 changes: 27 additions & 0 deletions src/Exception/NoDriverException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\MediaBundle\Exception;

/**
* @author Andrey F. Mindubaev <[email protected]>
*/
final class NoDriverException extends \RuntimeException
{
public function __construct($message = null, $code = 0, \Exception $previous = null)
{
parent::__construct(
null === $message ? 'The child node "db_driver" at path "sonata_media" must be configured.' : $message,
$code,
$previous
);
}
}
28 changes: 28 additions & 0 deletions src/Generator/NoDriverGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\MediaBundle\Generator;

use Sonata\MediaBundle\Exception\NoDriverException;
use Sonata\MediaBundle\Model\MediaInterface;

/**
* @internal
*
* @author Andrey F. Mindubaev <[email protected]>
*/
final class NoDriverGenerator implements GeneratorInterface
{
public function generatePath(MediaInterface $media)
{
throw new NoDriverException();
}
}
88 changes: 88 additions & 0 deletions src/Model/NoDriverManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\MediaBundle\Model;

use Sonata\CoreBundle\Model\ManagerInterface;
use Sonata\MediaBundle\Exception\NoDriverException;

/**
* @internal
*
* @author Andrey F. Mindubaev <[email protected]>
*/
final class NoDriverManager implements ManagerInterface
{
public function getClass()
{
throw new NoDriverException();
}

public function findAll()
{
throw new NoDriverException();
}

/**
* @param int|null $limit
* @param int|null $offset
*/
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
{
throw new NoDriverException();
}

public function findOneBy(array $criteria, array $orderBy = null)
{
throw new NoDriverException();
}

/**
* @param mixed $id
*/
public function find($id)
{
throw new NoDriverException();
}

public function create()
{
throw new NoDriverException();
}

/**
* @param object $entity
* @param bool $andFlush
*/
public function save($entity, $andFlush = true)
{
throw new NoDriverException();
}

/**
* @param object $entity
* @param bool $andFlush
*/
public function delete($entity, $andFlush = true)
{
throw new NoDriverException();
}

public function getTableName()
{
throw new NoDriverException();
}

public function getConnection()
{
throw new NoDriverException();
}
}
8 changes: 8 additions & 0 deletions src/Resources/config/no_driver.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?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">
<services>
<service id="sonata.media.manager.media" class="Sonata\MediaBundle\Model\NoDriverManager" public="true"/>
<service id="sonata.media.manager.gallery" class="Sonata\MediaBundle\Model\NoDriverManager" public="true"/>
<service id="sonata.media.generator.default" class="Sonata\MediaBundle\Generator\NoDriverGenerator"/>
</services>
</container>
7 changes: 7 additions & 0 deletions src/Resources/config/no_driver_admin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?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 its default value, not corresponding
to real persistence mechanism.
-->
</container>
28 changes: 28 additions & 0 deletions tests/Generator/NoDriverGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\MediaBundle\Tests\Generator;

use PHPUnit\Framework\TestCase;
use Sonata\MediaBundle\Exception\NoDriverException;
use Sonata\MediaBundle\Generator\NoDriverGenerator;
use Sonata\MediaBundle\Model\MediaInterface;

class NoDriverGeneratorTest extends TestCase
{
public function testException()
{
$this->expectException(NoDriverException::class);

$manager = new NoDriverGenerator();
$manager->generatePath($this->createMock(MediaInterface::class));
}
}
45 changes: 45 additions & 0 deletions tests/Model/NoDriverManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\MediaBundle\Tests\Model;

use PHPUnit\Framework\TestCase;
use Sonata\MediaBundle\Exception\NoDriverException;
use Sonata\MediaBundle\Model\NoDriverManager;

class NoDriverManagerTest extends TestCase
{
/**
* @dataProvider providerMethods
*/
public function testException($method, array $arguments)
{
$this->expectException(NoDriverException::class);

call_user_func_array([new NoDriverManager(), $method], $arguments);
}

public function providerMethods()
{
return [
['getClass', []],
['findAll', []],
['findBy', [[]]],
['findOneBy', [[]]],
['find', [1]],
['create', []],
['save', [null]],
['delete', [null]],
['getTableName', []],
['getConnection', []],
];
}
}