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

Added an option to configure the ORM quote strategy #453

Merged
merged 3 commits into from
Jul 3, 2016
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
23 changes: 23 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,26 @@ return array(
),
);
```

### How to Use Quote Strategy

[Official documentation](http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/basic-mapping.html#quoting-reserved-words)

Zend Configuration
Copy link
Contributor

Choose a reason for hiding this comment

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

s/Zend/Zend Framework 2+


```php
return array(
'service_manager' => array(
'invokables' => array(
'Doctrine\ORM\Mapping\AnsiQuoteStrategy' => 'Doctrine\ORM\Mapping\AnsiQuoteStrategy',
),
),
'doctrine' => array(
'configuration' => array(
'orm_default' => array(
'quote_strategy' => 'Doctrine\ORM\Mapping\AnsiQuoteStrategy'
),
),
),
);
```
41 changes: 41 additions & 0 deletions src/DoctrineORMModule/Options/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\ORM\Mapping\EntityListenerResolver;
use Doctrine\ORM\Mapping\NamingStrategy;
use Doctrine\ORM\Mapping\QuoteStrategy;
use Doctrine\ORM\Repository\RepositoryFactory;
use Zend\Stdlib\Exception\InvalidArgumentException;

Expand Down Expand Up @@ -153,6 +154,13 @@ class Configuration extends DBALConfiguration
*/
protected $namingStrategy;

/**
* Quote strategy or name of the quote strategy service to be set in ORM
* configuration (if any)
*
* @var string|null|QuoteStrategy
*/
protected $quoteStrategy;

/**
* Default repository class
Expand Down Expand Up @@ -533,6 +541,39 @@ public function getNamingStrategy()
return $this->namingStrategy;
}

/**
* @param string|null|QuoteStrategy $quoteStrategy
* @return self
* @throws InvalidArgumentException when the provided quote strategy does not fit the expected type
*/
public function setQuoteStrategy($quoteStrategy)
{
if (null === $quoteStrategy
|| is_string($quoteStrategy)
|| $quoteStrategy instanceof QuoteStrategy
) {
$this->quoteStrategy = $quoteStrategy;

return $this;
}

throw new InvalidArgumentException(
sprintf(
'quoteStrategy must be either a string, a Doctrine\ORM\Mapping\QuoteStrategy '
. 'instance or null, %s given',
is_object($quoteStrategy) ? get_class($quoteStrategy) : gettype($quoteStrategy)
)
);
}

/**
* @return string|null|QuoteStrategy
*/
public function getQuoteStrategy()
{
return $this->quoteStrategy;
}

/**
* @param string|null|RepositoryFactory $repositoryFactory
* @return self
Expand Down
12 changes: 12 additions & 0 deletions src/DoctrineORMModule/Service/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ public function createService(ServiceLocatorInterface $serviceLocator)
}
}

if ($quoteStrategy = $options->getQuoteStrategy()) {
if (is_string($quoteStrategy)) {
if (!$serviceLocator->has($quoteStrategy)) {
throw new InvalidArgumentException(sprintf('Quote strategy "%s" not found', $quoteStrategy));
}

$config->setQuoteStrategy($serviceLocator->get($quoteStrategy));
} else {
$config->setQuoteStrategy($quoteStrategy);
}
}

if ($repositoryFactory = $options->getRepositoryFactory()) {
if (is_string($repositoryFactory)) {
if (!$serviceLocator->has($repositoryFactory)) {
Expand Down
17 changes: 17 additions & 0 deletions tests/DoctrineORMModuleTest/Options/ConfigurationOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ public function testSetGetNamingStrategy()
$options->setNamingStrategy(new \stdClass());
}

public function testSetGetQuoteStrategy()
{
$options = new Configuration();
$options->setQuoteStrategy(null);
$this->assertNull($options->getQuoteStrategy());

$options->setQuoteStrategy('test');
$this->assertSame('test', $options->getQuoteStrategy());

$quoteStrategy = $this->getMock('Doctrine\ORM\Mapping\QuoteStrategy');
$options->setQuoteStrategy($quoteStrategy);
$this->assertSame($quoteStrategy, $options->getQuoteStrategy());

$this->setExpectedException('Zend\Stdlib\Exception\InvalidArgumentException');
$options->setQuoteStrategy(new \stdClass());
}

public function testSetRepositoryFactory()
{
$options = new Configuration();
Expand Down
53 changes: 53 additions & 0 deletions tests/DoctrineORMModuleTest/Service/ConfigurationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,59 @@ public function testWillNotInstantiateConfigWithInvalidNamingStrategyReference()
$this->factory->createService($this->serviceManager);
}

public function testWillInstantiateConfigWithQuoteStrategyObject()
{
$quoteStrategy = $this->getMock('Doctrine\ORM\Mapping\QuoteStrategy');

$config = array(
'doctrine' => array(
'configuration' => array(
'test_default' => array(
'quote_strategy' => $quoteStrategy,
),
),
),
);
$this->serviceManager->setService('Config', $config);
$factory = new ConfigurationFactory('test_default');
$ormConfig = $factory->createService($this->serviceManager);
$this->assertSame($quoteStrategy, $ormConfig->getQuoteStrategy());
}

public function testWillInstantiateConfigWithQuoteStrategyReference()
{
$quoteStrategy = $this->getMock('Doctrine\ORM\Mapping\QuoteStrategy');
$config = array(
'doctrine' => array(
'configuration' => array(
'test_default' => array(
'quote_strategy' => 'test_quote_strategy',
),
),
),
);
$this->serviceManager->setService('Config', $config);
$this->serviceManager->setService('test_quote_strategy', $quoteStrategy);
$ormConfig = $this->factory->createService($this->serviceManager);
$this->assertSame($quoteStrategy, $ormConfig->getQuoteStrategy());
}

public function testWillNotInstantiateConfigWithInvalidQuoteStrategyReference()
{
$config = array(
'doctrine' => array(
'configuration' => array(
'test_default' => array(
'quote_strategy' => 'test_quote_strategy',
),
),
),
);
$this->serviceManager->setService('Config', $config);
$this->setExpectedException('Zend\ServiceManager\Exception\InvalidArgumentException');
$this->factory->createService($this->serviceManager);
}

public function testWillInstantiateConfigWithHydrationCacheSetting()
{
$config = array(
Expand Down