Skip to content
Open
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
4 changes: 4 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ Doctrine DataBase Abstraction Layer follows the same idea of Cache drivers.

TBD

## Add Filters

;resources.doctrine.orm.entityManagers.default.filterCollection.soft-deleteable = "\Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter"

## Using

### Accessing Doctrine Container
Expand Down
108 changes: 63 additions & 45 deletions library/Bisna/Doctrine/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Bisna\Doctrine;

use Bisna\Exception,
Doctrine\DBAL\Types\Type,
Doctrine\Common\Annotations\AnnotationRegistry;
Doctrine\DBAL\Types\Type,
Doctrine\Common\Annotations\AnnotationRegistry;

/**
* Doctrine Container class.
Expand All @@ -14,7 +14,7 @@
class Container
{
/**
* @var string Default DBAL Connection name.
* @var string Default DBAL Connection name.
*/
public $defaultConnection = 'default';

Expand Down Expand Up @@ -48,7 +48,11 @@ class Container
*/
private $entityManagers = array();


/**
* @var array List of configured filters
*/
private $filters = array();

/**
* Constructor.
*
Expand All @@ -60,7 +64,7 @@ public function __construct(array $config = array())
if (isset($config['classLoader'])) {
$this->registerClassLoaders($config['classLoader']);
}

// Defining DBAL configuration
$dbalConfig = $this->prepareDBALConfiguration($config);

Expand Down Expand Up @@ -104,9 +108,9 @@ private function registerClassLoaders(array $config = array())
{
$classLoaderClass = $config['loaderClass'];
$classLoaderFile = $config['loaderFile'];

require_once $classLoaderFile;

foreach ($config['loaders'] as $loaderItem) {
if (! isset($loaderItem['includePath'])) {
$loaderItem['includePath'] = null;
Expand Down Expand Up @@ -184,7 +188,7 @@ private function prepareCacheInstanceConfiguration(array $config = array())
? $cacheConfig['defaultCacheInstance'] : $this->defaultCacheInstance;

unset($cacheConfig['defaultCacheInstance']);

$defaultCacheInstance = array(
'adapterClass' => 'Doctrine\Common\Cache\ArrayCache',
'namespace' => '',
Expand Down Expand Up @@ -298,18 +302,18 @@ public function getConnection($connName = null)

return $this->connections[$connName];
}

/**
* Retrieves a list of names for all Connections configured and/or loaded
*
*
* @return array
*/
public function getConnectionNames()
{
$configuredConnections = array_keys($this->configuration['dbal']);
$loadedConnections = array_keys($this->connections);
return array_merge($configuredConnections, $loadedConnections);
$configuredConnections = array_keys($this->configuration['dbal']);
$loadedConnections = array_keys($this->connections);

return array_merge($configuredConnections, $loadedConnections);
}

/**
Expand Down Expand Up @@ -344,15 +348,15 @@ public function getCacheInstance($cacheName = null)

/**
* Retrieves a list of names for all cache instances configured
*
*
* @return array
*/
public function getCacheInstanceNames()
{
$configuredInstances = array_keys($this->configuration['cache']);
$loadedInstances = array_keys($this->cacheInstances);
return array_merge($configuredInstances, $loadedInstances);
$configuredInstances = array_keys($this->configuration['cache']);
$loadedInstances = array_keys($this->cacheInstances);

return array_merge($configuredInstances, $loadedInstances);
}

/**
Expand Down Expand Up @@ -381,23 +385,28 @@ public function getEntityManager($emName = null)

unset($this->configuration['orm'][$emName]);
}


//add configured filters
foreach($this->filters as $filter) {
$this->entityManagers[$emName]->getFilters()->enable($filter);
}

return $this->entityManagers[$emName];
}

/**
* Retrieves a list of names for all Entity Managers configured and/or loaded
*
*
* @return array
*/
public function getEntityManagerNames()
{
$configuredEMs = array_keys($this->configuration['orm']);
$loadedEMs = array_keys($this->entityManagers);
return array_merge($configuredEMs, $loadedEMs);
$configuredEMs = array_keys($this->configuration['orm']);
$loadedEMs = array_keys($this->entityManagers);

return array_merge($configuredEMs, $loadedEMs);
}

/**
* Initialize the DBAL Connection.
*
Expand Down Expand Up @@ -440,7 +449,7 @@ private function startDBALConfiguration(array $config = array())
$sqlLoggerClass = $config['sqlLoggerClass'];
$configuration->setSQLLogger(new $sqlLoggerClass());
}

//DBAL Types configuration
$types = $config['types'];

Expand All @@ -466,7 +475,7 @@ private function startDBALEventManager(array $config = array())
// Event Subscribers configuration
foreach ($config['eventSubscribers'] as $subscriber) {
if ($subscriber) {
$eventManager->addEventSubscriber(new $subscriber());
$eventManager->addEventSubscriber(new $subscriber());
}
}

Expand Down Expand Up @@ -675,7 +684,16 @@ private function startORMConfiguration(array $config = array())
if (isset($config['defaultRepositoryClass'])) {
$configuration->setDefaultRepositoryClassName($config['defaultRepositoryClass']);
}


//enable configured filters
if(isset($config['filterCollection'])) {
foreach($config['filterCollection'] as $filter => $filterClass) {
//keep the filter names. We will have to enable them later
$this->filters[] = $filter;
$configuration->addFilter($filter, $filterClass);
}
}

return $configuration;
}

Expand All @@ -700,15 +718,15 @@ private function startORMMetadata(array $config = array())
'annotationReaderNamespaces' => array()
);


// Setup AnnotationRegistry
if (isset($config['annotationRegistry'])) {
$this->startAnnotationRegistry($config['annotationRegistry']);
}

foreach ($config['drivers'] as $driver) {
$driver = array_replace_recursive($defaultMetadataDriver, $driver);

$reflClass = new \ReflectionClass($driver['adapterClass']);
$nestedDriver = null;

Expand All @@ -718,7 +736,7 @@ private function startORMMetadata(array $config = array())
) {
$annotationReaderClass = $driver['annotationReaderClass'];
$annotationReader = new $annotationReaderClass();

// For Doctrine >= 2.2
if (method_exists($annotationReader, 'addNamespace')) {
$annotationReader->addNamespace('Doctrine\ORM\Mapping');
Expand All @@ -730,36 +748,36 @@ private function startORMMetadata(array $config = array())

if (method_exists($annotationReader, 'setAnnotationNamespaceAlias')) {
$driver['annotationReaderNamespaces']['ORM'] = 'Doctrine\ORM\Mapping\\';

foreach ($driver['annotationReaderNamespaces'] as $alias => $namespace) {
$annotationReader->setAnnotationNamespaceAlias($namespace, $alias);
}
}

$indexedReader = new \Doctrine\Common\Annotations\CachedReader(
new \Doctrine\Common\Annotations\IndexedReader($annotationReader),
new \Doctrine\Common\Annotations\IndexedReader($annotationReader),
$this->getCacheInstance($driver['annotationReaderCache'])
);

$nestedDriver = $reflClass->newInstance($indexedReader, $driver['mappingDirs']);
} else {
$nestedDriver = $reflClass->newInstance($driver['mappingDirs']);
}

$metadataDriver->addDriver($nestedDriver, $driver['mappingNamespace']);
}

if (($drivers = $metadataDriver->getDrivers()) && count($drivers) == 1) {
reset($drivers);
$metadataDriver = $drivers[key($drivers)];
}

return $metadataDriver;
}

/**
* Initialize ORM Metatada Annotation Registry driver
*
*
* @param array $config ORM Annotation Registry configuration.
*/
private function startAnnotationRegistry($config)
Expand All @@ -770,16 +788,16 @@ private function startAnnotationRegistry($config)
AnnotationRegistry::registerFile($file);
}
}

// Load annotation namespaces
if (isset($config['annotationNamespaces']) && is_array($config['annotationNamespaces'])) {
foreach($config['annotationNamespaces'] as $annotationNamespace) {
AnnotationRegistry::registerAutoloadNamespace(
$annotationNamespace['namespace'],
$annotationNamespace['includePath']
$annotationNamespace['namespace'],
$annotationNamespace['includePath']
);
}
}

}
}
}