php - Workaround for doctrine generator in PSR-4 codebase -


with symfony 2 & doctrine on windows machine i'm trying

  1. generate entities existing schema:

    php app/console doctrine:mapping:import --force corebundle annotation

  2. generate getters/setters on them:

    php app/console doctrine:generate:entities --path=/path/to/codebase/src/myproject/corebundle/entities corebundle

  3. generate rest crud controllers on them using voryx:

    php app/console voryx:generate:rest --entity="corebundle:user"

the first steps works fine , can find entities in corebundle/entity folder correct namespace:

myvendor\myproject\corebundle\entity 

good far. however, running other 2 commands fail:

[runtimeexception] can't find base path "corebundle" (path:  "\path\to\codebase\src\myproject\corebundle", destination:  "/path/to/codebase/src/myproject/corebundle").   

the autoload in composer.json looks this:

"autoload": {     "psr-4": {         "myvendor\\": "src/"     } }, 

i found out doctrine can't deal psr-4 namespaces, that's makes fail.

i entities live in psr-4 corebundle though - there workaround it?

i tried this, doesn't work, either:

"autoload": {     "psr-0": {         "myvendor\\myproject\\corebundle\\entity": "src/myproject/corebundle/entity/"     },     "psr-4": {         "myvendor\\": "src/"     } }, 

thank you.

user janvennemann on github fixed doctrine psr-4. can find patch on gist, or linked here below

step fix it

  1. mkdir -p app/vendoroverride;
  2. cp vendor/doctrine/doctrine-bundle/mapping/disconnectedmetadatafactory.php app/vendoroverride/disconnectedmetadatafactory.php;
  3. apply disconnectedmetadatafactory patch;
  4. add app/vendoroverride classmap section in composer.json;
  5. run composer dump-autoload.

then scaffolding command works.

disconnectedmetadatafactory psr-4 patch

/**  * base path class  *  * @param string $name      class name  * @param string $namespace class namespace  * @param string $path      class path  *  * @return string  * @throws \runtimeexception when base path not found  */ private function getbasepathforclass($name, $namespace, $path) {     $composerclassloader = $this->getcomposerclassloader();     if ($composerclassloader !== null) {         $psr4paths = $this->findpathsbypsr4prefix($namespace, $composerclassloader);         if ($psr4paths !== array()) {             // use first path             return $psr4paths[0];         }     }      $namespace = str_replace('\\', '/', $namespace);     $search = str_replace('\\', '/', $path);     $destination = str_replace('/'.$namespace, '', $search, $c);      if ($c != 1) {         throw new \runtimeexception(sprintf('can\'t find base path "%s" (path: "%s", destination: "%s").', $name, $path, $destination));     }      return $destination; }  /**  * gets composer class loader list of registered autoloaders  *  * @return \composer\autoload\classloader  */ private function getcomposerclassloader() {     $activeautloaders = spl_autoload_functions();     foreach($activeautloaders $autoloaderfunction) {         if (!is_array($autoloaderfunction)) {             continue;         }          $classloader = $autoloaderfunction[0];         if ($classloader instanceof \symfony\component\debug\debugclassloader) {             $classloader = $classloader->getclassloader()[0];         }          if (!is_object($classloader)) {             continue;         }          if ($classloader instanceof \composer\autoload\classloader) {             return $classloader;         }     }      return null; }  /**  * matches namespace against registered psr4 prefixes ,  * returns mapped paths if found  *  * @param string $namespace full namespace search  * @param \composer\autoload\classloader $composerclassloader composer class loader instance list of psr4 preixes  * @return array found paths namespace or empty array if none matched  */ private function findpathsbypsr4prefix($namespace, $composerclassloader) {     foreach ($composerclassloader->getprefixespsr4() $prefix => $paths) {         if (strpos($namespace, $prefix) === 0) {             return $paths;         }     }      return array(); } 

Comments