Skip to content

Commit

Permalink
Merge pull request #42 from mishak87/feature_interface
Browse files Browse the repository at this point in the history
Feature interface WIP
  • Loading branch information
fprochazka committed Sep 5, 2013
2 parents 3d09e99 + 3c9c278 commit 32dd68e
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 10 deletions.
40 changes: 40 additions & 0 deletions src/Kdyby/Doctrine/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* This file is part of the Kdyby (http://www.kdyby.org)
*
* Copyright (c) 2008 Filip Procházka ([email protected])
*
* For the full copyright and license information, please view the file license.txt that was distributed with this source code.
*/

namespace Kdyby\Doctrine;

use Doctrine;
use Doctrine\ORM\Configuration as BaseConfiguration;
use Kdyby;
use Nette;



/**
* @author Michal Gebauer <[email protected]>
*/
class Configuration extends BaseConfiguration
{

public function setTargetEntityMap($targetEntityMap)
{
$this->_attributes['targetEntityMap'] = $targetEntityMap;
}



public function getTargetEntityClassName($className)
{
return isset($this->_attributes['targetEntityMap'], $this->_attributes['targetEntityMap'][$className])
? $this->_attributes['targetEntityMap'][$className]
: $className;
}

}
3 changes: 1 addition & 2 deletions src/Kdyby/Doctrine/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
namespace Kdyby\Doctrine;

use Doctrine;
use Doctrine\DBAL\Configuration;
use Doctrine\Common\EventManager;
use Kdyby;
use Nette;
Expand Down Expand Up @@ -167,7 +166,7 @@ public function prepare($statement)
* @param array $schemaTypes
* @return Connection
*/
public static function create(array $params, Configuration $config, EventManager $eventManager, array $dbalTypes = array(), array $schemaTypes = array())
public static function create(array $params, Doctrine\DBAL\Configuration $config, EventManager $eventManager, array $dbalTypes = array(), array $schemaTypes = array())
{
foreach ($dbalTypes as $name => $className) {
if (DbalType::hasType($name)) {
Expand Down
35 changes: 35 additions & 0 deletions src/Kdyby/Doctrine/DI/ITargetEntityProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* This file is part of the Kdyby (http://www.kdyby.org)
*
* Copyright (c) 2008 Filip Procházka ([email protected])
*
* For the full copyright and license information, please view the file license.txt that was distributed with this source code.
*/

namespace Kdyby\Doctrine\DI;

use Kdyby;
use Nette;



/**
* Mapping definition can be array of
* - originalEntity => targetEntity
* - originalEntity => [ targetEntity => <class>, ... additional mapping data ]
*
* @author Michal Gebauer <[email protected]>
*/
interface ITargetEntityProvider
{

/**
* Returns associative array of Interface => Class definition
*
* @return array
*/
function getTargetEntityMappings();

}
68 changes: 62 additions & 6 deletions src/Kdyby/Doctrine/DI/OrmExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class OrmExtension extends Nette\DI\CompilerExtension
'metadata' => array(),
'filters' => array(),
'namespaceAlias' => array(),
'targetEntityMappings' => array(),
);

/**
Expand Down Expand Up @@ -119,6 +120,10 @@ class OrmExtension extends Nette\DI\CompilerExtension
*/
private $proxyAutoLoaders = array();

/**
* @var array
*/
private $targetEntityMappings = array();


public function loadConfiguration()
Expand Down Expand Up @@ -183,6 +188,17 @@ public function loadConfiguration()
$builder->addDefinition($this->prefix('jitProxyWarmer'))
->setClass('Kdyby\Doctrine\Proxy\JitProxyWarmer')
->setInject(FALSE);

if ($this->targetEntityMappings) {
$listener = $builder->addDefinition($this->prefix('resolveTargetEntityListener'))
->setClass('Kdyby\Doctrine\Tools\ResolveTargetEntityListener')
->addTag(Kdyby\Events\DI\EventsExtension::SUBSCRIBER_TAG)
->setInject(FALSE);

foreach ($this->targetEntityMappings as $originalEntity => $mapping) {
$listener->addSetup('addResolveTargetEntity', array($originalEntity, $mapping['targetEntity'], $mapping));
}
}
}


Expand All @@ -203,14 +219,20 @@ protected function processEntityManager($name, array $defaults)
/** @var Nette\DI\ServiceDefinition $metadataDriver */

Validators::assertField($config, 'metadata', 'array');
Validators::assertField($config, 'targetEntityMappings', 'array');
$config['targetEntityMappings'] = $this->normalizeTargetEntityMappings($config['targetEntityMappings']);
foreach ($this->compiler->getExtensions() as $extension) {
if (!$extension instanceof IEntityProvider) {
continue;
if ($extension instanceof IEntityProvider) {
$metadata = $extension->getEntityMappings();
Validators::assert($metadata, 'array:1..');
$config['metadata'] = array_merge($config['metadata'], $metadata);
}

$metadata = $extension->getEntityMappings();
Validators::assert($metadata, 'array:1..');
$config['metadata'] = array_merge($config['metadata'], $metadata);
if ($extension instanceof ITargetEntityProvider) {
$targetEntities = $extension->getTargetEntityMappings();
Validators::assert($targetEntities, 'array:1..');
$config['targetEntityMappings'] = Nette\Utils\Arrays::mergeTree($config['targetEntityMappings'], $this->normalizeTargetEntityMappings($targetEntities));
}
}

foreach (self::natSortKeys($config['metadata']) as $namespace => $driver) {
Expand All @@ -232,7 +254,7 @@ protected function processEntityManager($name, array $defaults)
Validators::assertField($config['dql'], 'numeric', 'array');
Validators::assertField($config['dql'], 'datetime', 'array');
$configuration = $builder->addDefinition($this->prefix($name . '.ormConfiguration'))
->setClass('Doctrine\ORM\Configuration')
->setClass('Kdyby\Doctrine\Configuration')
->addSetup('setMetadataCacheImpl', array($this->processCache($config['metadataCache'], $name . '.metadata')))
->addSetup('setQueryCacheImpl', array($this->processCache($config['queryCache'], $name . '.query')))
->addSetup('setResultCacheImpl', array($this->processCache($config['resultCache'], $name . '.ormResult')))
Expand Down Expand Up @@ -261,6 +283,13 @@ protected function processEntityManager($name, array $defaults)
$configuration->addSetup('addFilter', array($filterName, $filterClass));
}

if ($config['targetEntityMappings']) {
$configuration->addSetup('setTargetEntityMap', array(array_map(function ($mapping) {
return $mapping['targetEntity'];
}, $config['targetEntityMappings'])));
$this->targetEntityMappings = Nette\Utils\Arrays::mergeTree($this->targetEntityMappings, $config['targetEntityMappings']);
}

// entity manager
$builder->addDefinition($this->prefix($name . '.entityManager'))
->setClass('Kdyby\Doctrine\EntityManager')
Expand Down Expand Up @@ -540,6 +569,33 @@ private function loadConfig($name)
}


/**
* @param array $targetEntityMappings
* @return array
*/
private function normalizeTargetEntityMappings(array $targetEntityMappings)
{
$normalized = array();
foreach ($targetEntityMappings as $originalEntity => $targetEntity) {
$originalEntity = ltrim($originalEntity, '\\');
Validators::assert($targetEntity, 'array|string');
if (is_array($targetEntity)) {
Validators::assertField($targetEntity, 'targetEntity', 'string');
$mapping = array_merge($targetEntity, array(
'targetEntity' => ltrim($targetEntity['targetEntity'], '\\')
));

} else {
$mapping = array(
'targetEntity' => ltrim($targetEntity, '\\'),
);
}
$normalized[$originalEntity] = $mapping;
}
return $normalized;
}



/**
* @param \Nette\Configurator $configurator
Expand Down
7 changes: 5 additions & 2 deletions src/Kdyby/Doctrine/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Doctrine;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\ORMException;
use Kdyby;
use Kdyby\Doctrine\Tools\NonLockingUniqueInserter;
Expand Down Expand Up @@ -129,6 +128,10 @@ public function getDao($entityName)
*/
public function getClassMetadata($className)
{
if ($this->getConfiguration() instanceof Kdyby\Doctrine\Configuration) {
$className = $this->getConfiguration()->getTargetEntityClassName($className);
}

if (!class_exists($className)) {
throw new MissingClassException("Metadata of class $className was not found, because the class is missing or cannot be autoloaded.");
}
Expand All @@ -149,7 +152,7 @@ public function getClassMetadata($className)
* @throws \Doctrine\ORM\ORMException
* @return EntityManager
*/
public static function create($conn, Configuration $config, EventManager $eventManager = NULL)
public static function create($conn, Doctrine\ORM\Configuration $config, EventManager $eventManager = NULL)
{
if (!$config->getMetadataDriverImpl()) {
throw ORMException::missingMappingDriverImpl();
Expand Down
34 changes: 34 additions & 0 deletions src/Kdyby/Doctrine/Tools/ResolveTargetEntityListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* This file is part of the Kdyby (http://www.kdyby.org)
*
* Copyright (c) 2008 Filip Procházka ([email protected])
*
* For the full copyright and license information, please view the file license.txt that was distributed with this source code.
*/

namespace Kdyby\Doctrine\Tools;

use Doctrine;
use Kdyby;



/**
* @author Michal Gebauer <[email protected]>
*/
class ResolveTargetEntityListener extends Doctrine\ORM\Tools\ResolveTargetEntityListener implements Kdyby\Events\Subscriber
{

/**
* Returns an array of events this subscriber wants to listen to.
*
* @return array
*/
public function getSubscribedEvents()
{
return array( Doctrine\ORM\Events::loadClassMetadata );
}

}

0 comments on commit 32dd68e

Please sign in to comment.