Workaround for doctrine generator in PSR-4 codebase

筅森魡賤 提交于 2019-12-09 19:10:04

问题


With Symfony 2 & Doctrine on a Windows machine I'm trying to

  1. generate entities from an 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 and I can find the entities in my CoreBundle/Entity folder with the correct namespace:

MyVendor\MyProject\CoreBundle\Entity

Good so far. However, running the other 2 commands will fail:

[RuntimeException]
Can't find base path for "CoreBundle" (path: 
"\path\to\codebase\src\MyProject\CoreBundle", destination: 
"/path/to/codebase/src/MyProject/CoreBundle").  

The autoload in my composer.json looks like this:

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

I found out that Doctrine can't deal with PSR-4 namespaces, that's probably what makes it fail.

I would really like the entities to live in the PSR-4 CoreBundle though - is there a workaround for it?

I tried this, but it doesn't work, either:

"autoload": {
    "psr-0": {
        "MyVendor\\MyProject\\CoreBundle\\Entity": "src/MyProject/CoreBundle/Entity/"
    },
    "psr-4": {
        "MyVendor\\": "src/"
    }
},

Thank you.


回答1:


User janvennemann on GitHub fixed Doctrine for PSR-4. You can find the patch on Gist, or linked here below

Step to fix it

  1. mkdir -p app/VendorOverride;
  2. cp vendor/doctrine/doctrine-bundle/Mapping/DisconnectedMetadataFactory.php app/VendorOverride/DisconnectedMetadataFactory.php;
  3. apply the DisconnectedMetadataFactory patch;
  4. add app/VendorOverride to the classmap section in composer.json;
  5. run composer dump-autoload.

Then almost all scaffolding command works.

DisconnectedMetadataFactory PSR-4 patch

/**
 * Get a base path for a 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()) {
            // We just use the first path for now
            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 for "%s" (path: "%s", destination: "%s").', $name, $path, $destination));
    }

    return $destination;
}

/**
 * Gets the composer class loader from the list of registered autoloaders
 *
 * @return \Composer\Autoload\ClassLoader
 */
private function getComposerClassLoader() {
    $activeAutloaders = spl_autoload_functions();
    foreach($activeAutloaders as $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 the namespace against all registered psr4 prefixes and
 * returns their mapped paths if found
 *
 * @param string $namespace The full namespace to search for
 * @param \Composer\Autoload\ClassLoader $composerClassLoader A composer class loader instance to get the list of psr4 preixes from
 * @return array The found paths for the namespace or an empty array if none matched
 */
private function findPathsByPsr4Prefix($namespace, $composerClassLoader) {
    foreach ($composerClassLoader->getPrefixesPsr4() as $prefix => $paths) {
        if (strpos($namespace, $prefix) === 0) {
            return $paths;
        }
    }

    return array();
}



回答2:


If somebody comes across this issue.. I got it finally working. I'm not quite sure what exactly fixed it, so here are all the steps I did:

  1. As I was running Symfony 2.3, first I upgraded to 2.7
  2. I re-generated the bundle from scratch... I changed the location of the bundle to MyProject\CoreBundle and renamed the bundle class to MyProjectCoreBundle.

I can now run all of these commands successfully:

php app/console doctrine:mapping:import --force MyProjectCoreBundle annotation
php app/console doctrine:generate:entities MyProjectCoreBundle
php app/console doctrine:generate:form MyProjectCoreBundle:User
php app/console voryx:generate:rest --entity=MyProjectCoreBundle:User       

(Note that the call of doctrine:generate:form was not in the OP.)

My best guess is that one step of the upgrade was to change the composer autoload - this, or the 2.7 autoloader, seems to have fixed it:

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


来源:https://stackoverflow.com/questions/31333129/workaround-for-doctrine-generator-in-psr-4-codebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!