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

1.0 #66

Open
wants to merge 6 commits into
base: 1.0
Choose a base branch
from
Open

1.0 #66

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
156 changes: 99 additions & 57 deletions src/Dflydev/Pimple/Provider/DoctrineOrm/DoctrineOrmServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@
use Doctrine\ORM\Mapping\DefaultEntityListenerResolver;
use Doctrine\ORM\Mapping\DefaultNamingStrategy;
use Doctrine\ORM\Mapping\DefaultQuoteStrategy;
use Doctrine\ORM\Mapping\Driver\Driver;
use Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver;
use Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver;
use Doctrine\ORM\Mapping\Driver\XmlDriver;
use Doctrine\ORM\Mapping\Driver\YamlDriver;
use Doctrine\ORM\Mapping\Driver\StaticPHPDriver;
use Doctrine\ORM\Repository\DefaultRepositoryFactory;

/**
Expand All @@ -51,9 +45,16 @@ public function register(\Pimple $app)
}

$app['orm.em.default_options'] = array(
'connection' => 'default',
'mappings' => array(),
'types' => array()
'connection' => 'default',
'mappings' => array(),
'types' => array(),
'class.configuration' => 'Doctrine\ORM\Configuration',
'class.entityManager' => 'Doctrine\ORM\EntityManager',
'class.driver.yml' => 'Doctrine\ORM\Mapping\Driver\YamlDriver',
'class.driver.simple_yml' => 'Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver',
'class.driver.xml' => 'Doctrine\ORM\Mapping\Driver\XmlDriver',
'class.driver.simple_xml' => 'Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver',
'class.driver.php' => 'Doctrine\Common\Persistence\Mapping\Driver\StaticPHPDriver',
);

$app['orm.ems.options.initializer'] = $app->protect(function () use ($app) {
Expand Down Expand Up @@ -90,7 +91,7 @@ public function register(\Pimple $app)
return $app['orm.ems.default'];
});

$app['orm.ems'] = $app->share(function($app) {
$app['orm.ems'] = $app->share(function(\Pimple $app) {
$app['orm.ems.options.initializer']();

$ems = new \Pimple();
Expand All @@ -102,8 +103,12 @@ public function register(\Pimple $app)
$config = $app['orm.ems.config'][$name];
}

$ems[$name] = $app->share(function ($ems) use ($app, $options, $config) {
return EntityManager::create(
$ems[$name] = $app->share(function () use ($app, $options, $config) {
/**
* @var $entityManagerClassName EntityManager
*/
$entityManagerClassName = $options['class.entityManager'];
return $entityManagerClassName::create(
$app['dbs'][$options['connection']],
$config,
$app['dbs.event_manager'][$options['connection']]
Expand All @@ -114,12 +119,16 @@ public function register(\Pimple $app)
return $ems;
});

$app['orm.ems.config'] = $app->share(function($app) {
$app['orm.ems.config'] = $app->share(function(\Pimple $app) {
$app['orm.ems.options.initializer']();

$configs = new \Pimple();
foreach ($app['orm.ems.options'] as $name => $options) {
$config = new Configuration;
/**
* @var $config Configuration
*/
$configurationClassName = $options['class.configuration'];
$config = new $configurationClassName;

$app['orm.cache.configurer']($name, $config, $options);

Expand All @@ -141,6 +150,9 @@ public function register(\Pimple $app)
$config->setNamingStrategy($app['orm.strategy.naming']);
$config->setQuoteStrategy($app['orm.strategy.quote']);

/**
* @var MappingDriverChain $chain
*/
$chain = $app['orm.mapping_driver_chain.locator']($name);
foreach ((array) $options['mappings'] as $entity) {
if (!is_array($entity)) {
Expand All @@ -150,46 +162,30 @@ public function register(\Pimple $app)
}

if (!empty($entity['resources_namespace'])) {
$entity['path'] = $app['psr0_resource_locator']->findFirstDirectory($entity['resources_namespace']);
if($app->offsetExists('psr0_resource_locator')) {
$entity['path'] = $app['psr0_resource_locator']->findFirstDirectory($entity['resources_namespace']);
} else {
throw new \InvalidArgumentException('Not exist psr0_resource_locator');
}
}

if (isset($entity['alias'])) {
$config->addEntityNamespace($entity['alias'], $entity['namespace']);
}

switch ($entity['type']) {
case 'annotation':
$useSimpleAnnotationReader =
isset($entity['use_simple_annotation_reader'])
? $entity['use_simple_annotation_reader']
: true;
$driver = $config->newDefaultAnnotationDriver((array) $entity['path'], $useSimpleAnnotationReader);
$chain->addDriver($driver, $entity['namespace']);
break;
case 'yml':
$driver = new YamlDriver($entity['path']);
$chain->addDriver($driver, $entity['namespace']);
break;
case 'simple_yml':
$driver = new SimplifiedYamlDriver(array($entity['path'] => $entity['namespace']));
$chain->addDriver($driver, $entity['namespace']);
break;
case 'xml':
$driver = new XmlDriver($entity['path']);
$chain->addDriver($driver, $entity['namespace']);
break;
case 'simple_xml':
$driver = new SimplifiedXmlDriver(array($entity['path'] => $entity['namespace']));
$chain->addDriver($driver, $entity['namespace']);
break;
case 'php':
$driver = new StaticPHPDriver($entity['path']);
$chain->addDriver($driver, $entity['namespace']);
break;
default:
if('annotation' === $entity['type']){
$useSimpleAnnotationReader = isset($entity['use_simple_annotation_reader'])
? $entity['use_simple_annotation_reader']
: true;
$driver = $config->newDefaultAnnotationDriver((array) $entity['path'], $useSimpleAnnotationReader);
} else {
if( isset($app['orm.driver.factory.'.$entity['type']]) ) {
$driver = $app['orm.driver.factory.'.$entity['type']]( $entity, $options );
} else {
throw new \InvalidArgumentException(sprintf('"%s" is not a recognized driver', $entity['type']));
break;
}
}
$chain->addDriver($driver, $entity['namespace']);
}
$config->setMetadataDriverImpl($chain);

Expand All @@ -207,6 +203,41 @@ public function register(\Pimple $app)
return $configs;
});

$app['orm.driver.factory.yml'] = $app->share(function() {
return function ( $entity, $options ) {
$className = $options['class.driver.yml'];
return new $className($entity['path']);
};
});

$app['orm.driver.factory.simple_yml'] = $app->share(function() {
return function ( $entity, $options ) {
$className = $options['class.driver.simple_yml'];
return new $className( array($entity['path'] => $entity['namespace']) );
};
});

$app['orm.driver.factory.xml'] = $app->share(function() {
return function ( $entity, $options ) {
$className = $options['class.driver.xml'];
return new $className($entity['path']);
};
});

$app['orm.driver.factory.simple_xml'] = $app->share(function() {
return function ( $entity, $options ) {
$className = $options['class.driver.simple_xml'];
return new $className( array($entity['path'] => $entity['namespace']) );
};
});

$app['orm.driver.factory.php'] = $app->share(function() {
return function ( $entity, $options ) {
$className = $options['class.driver.php'];
return new $className($entity['path']);
};
});

$app['orm.cache.configurer'] = $app->protect(function($name, Configuration $config, $options) use ($app) {
$config->setMetadataCacheImpl($app['orm.cache.locator']($name, 'metadata', $options));
$config->setQueryCacheImpl($app['orm.cache.locator']($name, 'query', $options));
Expand Down Expand Up @@ -256,6 +287,9 @@ public function register(\Pimple $app)
throw new \RuntimeException('Host and port options need to be specified for memcache cache');
}

/**
* @var $memcache \Memcache
*/
$memcache = $app['orm.cache.factory.backing_memcache']();
$memcache->connect($cacheOptions['host'], $cacheOptions['port']);

Expand All @@ -273,7 +307,9 @@ public function register(\Pimple $app)
if (empty($cacheOptions['host']) || empty($cacheOptions['port'])) {
throw new \RuntimeException('Host and port options need to be specified for memcached cache');
}

/**
* @var $memcached \Memcached
*/
$memcached = $app['orm.cache.factory.backing_memcached']();
$memcached->addServer($cacheOptions['host'], $cacheOptions['port']);

Expand All @@ -291,7 +327,9 @@ public function register(\Pimple $app)
if (empty($cacheOptions['host']) || empty($cacheOptions['port'])) {
throw new \RuntimeException('Host and port options need to be specified for redis cache');
}

/**
* @var $redis \Redis
*/
$redis = $app['orm.cache.factory.backing_redis']();
$redis->connect($cacheOptions['host'], $cacheOptions['port']);

Expand Down Expand Up @@ -365,7 +403,7 @@ public function register(\Pimple $app)
return $app[$cacheInstanceKey] = $app['orm.mapping_driver_chain.factory']($name);
});

$app['orm.mapping_driver_chain.factory'] = $app->protect(function($name) use ($app) {
$app['orm.mapping_driver_chain.factory'] = $app->protect(function() use ($app) {
return new MappingDriverChain;
});

Expand All @@ -375,15 +413,21 @@ public function register(\Pimple $app)
if (null === $name) {
$name = $app['orm.ems.default'];
}

/**
* @var MappingDriverChain $driverChain
*/
$driverChain = $app['orm.mapping_driver_chain.locator']($name);
$driverChain->addDriver($mappingDriver, $namespace);
});

$app['orm.generate_psr0_mapping'] = $app->protect(function($resourceMapping) use ($app) {
$mapping = array();
foreach ($resourceMapping as $resourceNamespace => $entityNamespace) {
$directory = $app['psr0_resource_locator']->findFirstDirectory($resourceNamespace);
if($app->offsetExists('psr0_resource_locator')) {
$directory = $app['psr0_resource_locator']->findFirstDirectory($resourceNamespace);
} else {
throw new \InvalidArgumentException('Not exist psr0_resource_locator');
}
if (!$directory) {
throw new \InvalidArgumentException("Resources for mapping '$entityNamespace' could not be located; Looked for mapping resources at '$resourceNamespace'");
}
Expand All @@ -393,19 +437,19 @@ public function register(\Pimple $app)
return $mapping;
});

$app['orm.strategy.naming'] = $app->share(function($app) {
$app['orm.strategy.naming'] = $app->share(function() {
return new DefaultNamingStrategy;
});

$app['orm.strategy.quote'] = $app->share(function($app) {
$app['orm.strategy.quote'] = $app->share(function() {
return new DefaultQuoteStrategy;
});

$app['orm.entity_listener_resolver'] = $app->share(function($app) {
$app['orm.entity_listener_resolver'] = $app->share(function() {
return new DefaultEntityListenerResolver;
});

$app['orm.repository_factory'] = $app->share(function($app) {
$app['orm.repository_factory'] = $app->share(function() {
return new DefaultRepositoryFactory;
});

Expand All @@ -425,8 +469,6 @@ public function register(\Pimple $app)
/**
* Get default ORM configuration settings.
*
* @param Application $app Application
*
* @return array
*/
protected function getOrmDefaults()
Expand Down