php*_*hil 3 php symfony doctrine-orm psr-0 symfony-console
在我正在尝试的Windows机器上使用Symfony 2和Doctrine
从现有模式生成实体:
php app/console doctrine:mapping:import --force CoreBundle annotation
在它们上生成getter/setter:
php app/console doctrine:generate:entities --path=/path/to/codebase/src/MyProject/CoreBundle/Entities CoreBundle
使用Voryx在它们上生成REST CRUD控制器:
php app/console voryx:generate:rest --entity="CoreBundle:User"
第一步工作正常,我可以在我的CoreBundle/Entity文件夹中找到具有正确命名空间的实体:
MyVendor\MyProject\CoreBundle\Entity
Run Code Online (Sandbox Code Playgroud)
目前很好.但是,运行其他2个命令将失败:
[RuntimeException]
Can't find base path for "CoreBundle" (path:
"\path\to\codebase\src\MyProject\CoreBundle", destination:
"/path/to/codebase/src/MyProject/CoreBundle").
Run Code Online (Sandbox Code Playgroud)
我的composer.json中的自动加载看起来像这样:
"autoload": {
"psr-4": {
"MyVendor\\": "src/"
}
},
Run Code Online (Sandbox Code Playgroud)
我发现Doctrine不能处理PSR-4命名空间,这可能是它失败的原因.
我真的希望这些实体能够存在于PSR-4 CoreBundle中 - 它有解决方法吗?
我试过这个,但它也不起作用:
"autoload": {
"psr-0": {
"MyVendor\\MyProject\\CoreBundle\\Entity": "src/MyProject/CoreBundle/Entity/"
},
"psr-4": {
"MyVendor\\": "src/"
}
},
Run Code Online (Sandbox Code Playgroud)
谢谢.
用户janvennemann在GitHub上修复了PSR-4的Doctrine.您可以在Gist上找到补丁,或在下面链接
mkdir -p app/VendorOverride
;cp vendor/doctrine/doctrine-bundle/Mapping/DisconnectedMetadataFactory.php app/VendorOverride/DisconnectedMetadataFactory.php
;app/VendorOverride
到该classmap
部分composer.json
;composer dump-autoload
.然后几乎所有脚手架命令都有效.
/**
* 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();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1586 次 |
最近记录: |