Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added filter support #40

Open
wants to merge 1 commit into from

1 participant

@leonmyburgh

Added support for Doctrine filters. I have tested it with the Gedmo SoftDeleteable Filter (which is the one I needed to use).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 3, 2012
  1. @leonmyburgh

    Added filter support

    leonmyburgh authored
This page is out of date. Refresh to see the latest.
Showing with 67 additions and 45 deletions.
  1. +4 −0 README.markdown
  2. +63 −45 library/Bisna/Doctrine/Container.php
View
4 README.markdown
@@ -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
View
108 library/Bisna/Doctrine/Container.php
@@ -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.
@@ -14,7 +14,7 @@
class Container
{
/**
- * @var string Default DBAL Connection name.
+ * @var string Default DBAL Connection name.
*/
public $defaultConnection = 'default';
@@ -48,7 +48,11 @@ class Container
*/
private $entityManagers = array();
-
+ /**
+ * @var array List of configured filters
+ */
+ private $filters = array();
+
/**
* Constructor.
*
@@ -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);
@@ -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;
@@ -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' => '',
@@ -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);
}
/**
@@ -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);
}
/**
@@ -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.
*
@@ -440,7 +449,7 @@ private function startDBALConfiguration(array $config = array())
$sqlLoggerClass = $config['sqlLoggerClass'];
$configuration->setSQLLogger(new $sqlLoggerClass());
}
-
+
//DBAL Types configuration
$types = $config['types'];
@@ -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());
}
}
@@ -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;
}
@@ -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;
@@ -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');
@@ -730,14 +748,14 @@ 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'])
);
@@ -745,7 +763,7 @@ private function startORMMetadata(array $config = array())
} else {
$nestedDriver = $reflClass->newInstance($driver['mappingDirs']);
}
-
+
$metadataDriver->addDriver($nestedDriver, $driver['mappingNamespace']);
}
@@ -753,13 +771,13 @@ private function startORMMetadata(array $config = array())
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)
@@ -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']
);
}
-
- }
+
+ }
}
}
Something went wrong with that request. Please try again.