简体   繁体   English

如果路径结构不是默认值,则无法使用symfony2类加载器

[英]symfony2 classloader cannot be used if path structure is not default

looks like symfony2 classloader does not allow different paths for namespaces, anyone has any idea?(except changing the classloader). 看起来symfony2类加载器不允许名称空间使用不同的路径,有人有什么主意吗(更改类加载器除外)。

$loader = new \Symfony\Component\ClassLoader\UniversalClassLoader();
$loader->registerNamespaces(
  array(
    'Symfony\\Component' => \realpath('/myapp/path' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'Symfony2Components')
  )
);
$loader->register(true);


// in other file

use Symfony\Component\HttpFoundation\Request;

$request = Request::createFromGlobals(); // <-- resolves to : '/myapp/path/vendor/Symfony2Components/Symfony/Component/HttpFoundation/Request.php'
// expected '/myapp/path/vendor/Symfony2Components/HttpFoundation/Request.php'

It's not a bug. 这不是错误。 Symfony2 ClassLoader just conforms to the PSR-0 : Symfony2 ClassLoader仅符合PSR-0

Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system. 从文件系统加载时,每个名称空间分隔符都会转换为DIRECTORY_SEPARATOR。

You just define the root path, but all the namespace parts will still be used in the path. 您仅定义了根路径,但是所有名称空间部分仍将在该路径中使用。

@Maerlyn I also thought about that, but its not, good notice thou. @Maerlyn我也考虑过这个问题,但是请注意。

https://github.com/nimmen/ClassLoader/commit/0881a02d3210fd6376abd3b62f0c665642d310bb https://github.com/nimmen/ClassLoader/commit/0881a02d3210fd6376abd3b62f0c665642d310bb
made small changes, now it works like(imo as it should): 进行了一些小的更改,现在它的工作方式就像(应该的imo):

register namespaces example: 注册名称空间示例:

name\\space => /dir/ectory 名称\\空间=> / dir / ectory
another\\name => /another/dir 另一个\\名称=> / another / dir

\\name\\space\\classname resolves to: \\ name \\ space \\ classname解析为:

/dir/ectory/classname.php /dir/ectory/classname.php

\\another\\name\\space\\class resolves to: \\ another \\ name \\ space \\ class解析为:

/another/dir/space/class.php /another/dir/space/class.php

hope this will help someone. 希望这会帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM