Skip to content

Instantly share code, notes, and snippets.

@janvennemann
Last active February 20, 2018 09:31
Show Gist options
  • Save janvennemann/46b2626eee2a4808ed75 to your computer and use it in GitHub Desktop.
Save janvennemann/46b2626eee2a4808ed75 to your computer and use it in GitHub Desktop.
Changes for DoctrineBundle PSR-4 support
<?php
class DisconnectedMetadataFactory
{
//...
/**
* 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();
}
//...
}
@giosh94mhz
Copy link

I was struggling with the same problem and you saved the day!

Have you already tried sending a PR for this? I know they don't like to support PSR-4 this much, but it it's worth a try.

@drzraf
Copy link

drzraf commented May 3, 2016

I'd advise you to make this a PR so that one could test and feed back easily.
I don't know what other modification are neededin order to get the command line orm:generate:entities to works with it.

@dennisverspuij
Copy link

Brilliant, workaround for Doctrine annoyance 83, thanks very much!

@photoup-godwinh
Copy link

Does this work on the 2.5 version of Doctrine?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment