Bläddra i källkod

[PropelBundle] added autoloading for model classes

Fabien Potencier 15 år sedan
förälder
incheckning
448d1a31df

+ 26 - 0
src/Symfony/Framework/PropelBundle/Bundle.php

@@ -14,4 +14,30 @@ class Bundle extends BaseBundle
     {
         Loader::registerExtension(new PropelExtension());
     }
+
+    public function boot(ContainerInterface $container)
+    {
+        $kernel = $container->getKernelService();
+        if (!file_exists($autoload = $kernel->getCacheDir().'/propel_autoload.php')) {
+            $map = array();
+            foreach ($kernel->getBundles() as $bundle) {
+                if (!file_exists($file = $bundle->getPath().'/Resources/config/classmap.php')) {
+                    continue;
+                }
+
+                $local = include($file);
+                foreach ($local as $class => $path) {
+                    $map[$class] = $bundle->getPath().'/'.$path;
+                }
+            }
+
+            if ($map) {
+                file_put_contents($autoload, '<?php return '.var_export($map, true).';');
+            }
+        }
+
+        $autoloader = \PropelAutoloader::getInstance();
+        $autoloader->addClassPaths(include($autoload));
+        $autoloader->register();
+    }
 }

+ 35 - 1
src/Symfony/Framework/PropelBundle/Command/BuildCommand.php

@@ -51,7 +51,41 @@ class BuildCommand extends Command
      */
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        return $this->callPhing('om');
+        $this->callPhing('om');
+
+        foreach ($this->application->getKernel()->getBundles() as $bundle) {
+            $map = array();
+            if (is_dir($dir = $bundle->getPath().'/Model/map')) {
+                $finder = new Finder();
+                $files = $finder->files()->name('*TableMap.php')->followLinks()->in($dir);
+
+                foreach ($files as $file) {
+                    $class = substr($file->getBasename(), 0, -12);
+
+                    $map = array_merge($map, array(
+                        $class                => 'Model/'.$class.'.php',
+                        $class.'Peer'         => 'Model/'.$class.'Peer.php',
+                        $class.'Query'        => 'Model/'.$class.'Query.php',
+                        'Base'.$class         => 'Model/om/Base'.$class.'.php',
+                        'Base'.$class.'Peer'  => 'Model/om/Base'.$class.'Peer.php',
+                        'Base'.$class.'Query' => 'Model/om/Base'.$class.'Query.php',
+                        $class.'TableMap'     => 'Model/map/'.$class.'TableMap.php',
+                    ));
+                }
+            }
+
+            if (!is_dir($bundle->getPath().'/Resources/config'))
+            {
+                mkdir($bundle->getPath().'/Resources/config', null, true);
+            }
+
+            if ($map)
+            {
+                file_put_contents($bundle->getPath().'/Resources/config/classmap.php', '<?php return '.var_export($map, true).';');
+            }
+        }
+
+        unlink($this->application->getKernel()->getCacheDir().'/propel_autoload.php');
     }
 
     protected function callPhing($taskName, $properties = array())