Skip to content

Commit

Permalink
Moving DBAL related configuration to ORM module, fixing tests bootstr…
Browse files Browse the repository at this point in the history
…apper (still using Configuration instead of Config
  • Loading branch information
Ocramius committed Jul 22, 2012
1 parent cabab9a commit e5b98e9
Show file tree
Hide file tree
Showing 11 changed files with 627 additions and 24 deletions.
94 changes: 94 additions & 0 deletions docs/EXTRAS_ORM.md
@@ -0,0 +1,94 @@
# Extra goodies included with DoctrineModule
The items listed below are entirely optional and are intended to enhance integration between Zend Framework 2 and
Doctrine 2 .

## ObjectExists and NoObjectExists Validators
The ObjectExists and NoObjectExists are validators similar to Zend\Validator\Db validators. You can
pass a variety of options to determine validity. The most basic use case requires an entity manager (em),
an entity, and a field. You also have the option of specifying a query_builder Closure to use if you
want to fine tune the results.

```php
<?php
$validator = new \DoctrineModule\Validator\NoObjectExists(array(
// object repository to lookup
'object_repository' => $serviceLocator->get('Doctrine\ORM\EntityManager')->getRepository('My\Entity\User'),

// fields to match
'fields' => array('username'),
));

// following works also with simple values if the number of fields to be matched is 1
echo $validator->isValid(array('username' => 'test')) ? 'Valid' : 'Invalid! Duplicate found!';
```

## Authentication adapter for Zend\Authentication
The authentication adapter is intended to provide an adapter for `Zend\Authentication`. It works much
like the `DbTable` adapter in the core framework. You must provide the entity manager instance,
entity name, identity field, and credential field. You can optionally provide a callable method
to perform hashing on the password prior to checking for validation.

```php
<?php
$adapter = new \DoctrineModule\Authentication\Adapter\DoctrineObject(
$this->getLocator()->get('Doctrine\ORM\EntityManager'),
'Application\Test\Entity',
'username', // optional, default shown
'password', // optional, default shown,
function($identity, $credential) { // optional callable
return \My\Service\User::hashCredential(
$credential,
$identity->getSalt(),
$identity->getAlgorithm()
);
}
);

$adapter->setIdentityValue('admin');
$adapter->setCredentialValue('pa55w0rd');
$result = $adapter->authenticate();

echo $result->isValid() ? 'Authenticated!' : 'Could not authenticate';
```

## Custom DBAL Types
To register custom Doctrine DBAL types, simply add them to the `doctrine.configuration.my_dbal_default.types`
key in you configuration file:

```php
<?php
return array(
'doctrine' => array(
'configuration' => array(
'my_dbal_default' => array(
'types' => array(
// You can override a default type
'date' => 'My\DBAL\Types\DateType',

// And set new ones
'tinyint' => 'My\DBAL\Types\TinyIntType',
),
),
),
),
);
```

You are now able to use them, for example, in your ORM entities:

```php
<?php

class User
{
/**
* @ORM\Column(type="date")
*/
protected $birthdate;

/**
* @ORM\Column(type="tinyint")
*/
protected $houses;
}
```
22 changes: 12 additions & 10 deletions src/DoctrineORMModule/Module.php
Expand Up @@ -21,13 +21,15 @@


use Doctrine\Common\Annotations\AnnotationRegistry; use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\ORM\Tools\Console\ConsoleRunner; use Doctrine\ORM\Tools\Console\ConsoleRunner;
use DoctrineModule\Service as CommonService; use DoctrineModule\Service\DriverFactory;
use DoctrineORMModule\Service as ORMService; use DoctrineModule\Service\EventManagerFactory;
use DoctrineORMModule\Service\ConfigurationFactory as ORMConfigurationFactory;
use DoctrineORMModule\Service\EntityManagerFactory;
use DoctrineORMModule\Service\DBALConnectionFactory;


use Zend\ModuleManager\ModuleManagerInterface; use Zend\ModuleManager\ModuleManagerInterface;
use Zend\ModuleManager\Feature\ServiceProviderInterface; use Zend\ModuleManager\Feature\ServiceProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface; use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\ModuleEvent;
use Zend\ServiceManager\ServiceLocatorInterface; use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\EventManager\EventInterface; use Zend\EventManager\EventInterface;


Expand All @@ -41,8 +43,6 @@
use Doctrine\DBAL\Migrations\Tools\Console\Command\StatusCommand; use Doctrine\DBAL\Migrations\Tools\Console\Command\StatusCommand;
use Doctrine\DBAL\Migrations\Tools\Console\Command\VersionCommand; use Doctrine\DBAL\Migrations\Tools\Console\Command\VersionCommand;


use ReflectionClass;

/** /**
* Base module for Doctrine ORM. * Base module for Doctrine ORM.
* *
Expand Down Expand Up @@ -121,11 +121,13 @@ public function getServiceConfig()
$sl->get('doctrine.entitymanager.orm_default') $sl->get('doctrine.entitymanager.orm_default')
); );
}, },
'doctrine.connection.orm_default' => new CommonService\ConnectionFactory('orm_default'),
'doctrine.configuration.orm_default' => new ORMService\ConfigurationFactory('orm_default'), 'doctrine.connection.orm_default' => new DBALConnectionFactory('orm_default'),
'doctrine.driver.orm_default' => new CommonService\DriverFactory('orm_default'), 'doctrine.configuration.orm_default' => new ORMConfigurationFactory('orm_default'),
'doctrine.entitymanager.orm_default' => new ORMService\EntityManagerFactory('orm_default'), 'doctrine.entitymanager.orm_default' => new EntityManagerFactory('orm_default'),
'doctrine.eventmanager.orm_default' => new CommonService\EventManagerFactory('orm_default'),
'doctrine.driver.orm_default' => new DriverFactory('orm_default'),
'doctrine.eventmanager.orm_default' => new EventManagerFactory('orm_default'),
) )
); );
} }
Expand Down
14 changes: 11 additions & 3 deletions src/DoctrineORMModule/Options/Configuration.php
Expand Up @@ -2,9 +2,17 @@


namespace DoctrineORMModule\Options; namespace DoctrineORMModule\Options;


use DoctrineModule\Options\Configuration as DoctrineConfiguration; use DoctrineORMModule\Options\DBALConfiguration;


class Configuration extends DoctrineConfiguration /**
* Configuration options for an ORM Configuration
*
* @license MIT
* @link http://www.doctrine-project.org/
* @author Kyle Spraggs <theman@spiffyjr.me>
* @author Marco Pivetta <ocramius@gmail.com>
*/
class Configuration extends DBALConfiguration
{ {
/** /**
* Set the cache key for the metadata cache. Cache key * Set the cache key for the metadata cache. Cache key
Expand Down
104 changes: 104 additions & 0 deletions src/DoctrineORMModule/Options/DBALConfiguration.php
@@ -0,0 +1,104 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace DoctrineORMModule\Options;

use Zend\Stdlib\AbstractOptions;

/**
* Configuration options for a DBAL Connection
*
* @license MIT
* @link http://www.doctrine-project.org/
* @author Kyle Spraggs <theman@spiffyjr.me>
*/
class DBALConfiguration extends AbstractOptions
{
/**
* Set the cache key for the result cache. Cache key
* is assembled as "doctrine.cache.{key}" and pulled from
* service locator.
*
* @var string
*/
protected $resultCache = 'array';

/**
* Set the class name of the SQL Logger, or null, to disable.
*
* @var string
*/
protected $sqlLogger = null;

/**
* Keys must be the name of the type identifier and value is
* the class name of the Type
*
* @var array
*/
protected $types = array();

/**
* @param string $resultCache
*/
public function setResultCache($resultCache)
{
$this->resultCache = $resultCache;
}

/**
* @return string
*/
public function getResultCache()
{
return 'doctrine.cache.' . $this->resultCache;
}

/**
* @param string $sqlLogger
*/
public function setSqlLogger($sqlLogger)
{
$this->sqlLogger = $sqlLogger;
}

/**
* @return string
*/
public function getSqlLogger()
{
return $this->sqlLogger;
}

/**
* @param array $types
*/
public function setTypes(array $types)
{
$this->types = $types;
}

/**
* @return string
*/
public function getTypes()
{
return $this->types;
}
}

0 comments on commit e5b98e9

Please sign in to comment.