Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added an autoloader for proxies (see doctrine/DoctrineMongoDBBundle#85)

  • Loading branch information...
commit 3131c2df3634a197069bb5d576080c650644154a 1 parent 4530070
@lsmith77 lsmith77 authored
Showing with 53 additions and 0 deletions.
  1. +53 −0 DoctrinePHPCRBundle.php
View
53 DoctrinePHPCRBundle.php
@@ -27,6 +27,7 @@
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPass;
+use Doctrine\Common\Util\ClassUtils;
use Doctrine\Bundle\PHPCRBundle\OptionalCommand\InitDoctrineDbalCommand;
use Doctrine\Bundle\PHPCRBundle\OptionalCommand\JackrabbitCommand;
@@ -53,4 +54,56 @@ public function registerCommands(Application $application)
$application->add(new InitDoctrineDbalCommand());
}
}
+
+ public function boot()
+ {
+ // Register an autoloader for proxies to avoid issues when unserializing them when the ODM is used.
+ if ($this->container->hasParameter('doctrine_phpcr.odm.proxy_namespace')) {
+ $namespace = $this->container->getParameter('doctrine_phpcr.odm.proxy_namespace');
+ $dir = $this->container->getParameter('doctrine_phpcr.odm.proxy_dir');
+ // See https://github.com/symfony/symfony/pull/3419 for usage of references
+ $container =& $this->container;
+
+ $this->autoloader = function($class) use ($namespace, $dir, &$container) {
+ if (0 === strpos($class, $namespace)) {
+ $fileName = str_replace('\\', '', substr($class, strlen($namespace) +1));
+ $file = $dir.DIRECTORY_SEPARATOR.$fileName.'.php';
+
+ if (!is_file($file) && $container->getParameter('kernel.debug')) {
+ $originalClassName = ClassUtils::getRealClass($class);
+ $registry = $container->get('doctrine_phpcr');
+
+ // Tries to auto-generate the proxy file
+ foreach ($registry->getManagers() as $dm) {
+
+ if ($dm->getConfiguration()->getAutoGenerateProxyClasses()) {
+ $classes = $dm->getMetadataFactory()->getAllMetadata();
+
+ foreach ($classes as $classMetadata) {
+ if ($classMetadata->name == $originalClassName) {
+ $dm->getProxyFactory()->generateProxyClasses(array($classMetadata));
+ }
+ }
+ }
+ }
+
+ clearstatcache($file);
+ }
+
+ if (is_file($file)) {
+ require $file;
+ }
+ }
+ };
+ spl_autoload_register($this->autoloader);
+ }
+ }
+
+ public function shutdown()
+ {
+ if (null !== $this->autoloader) {
+ spl_autoload_unregister($this->autoloader);
+ $this->autoloader = null;
+ }
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.