Symfony 2.6.4:命名空间问题?使用自定义验证器传递在命令行上引发异常,但在浏览器中运行良好


Symfony 2.6.4: Namespace Issue? Using custom validator pass throws exception on command line but works fine in browser

我尝试了Kevin Bond的解决方案。在浏览器中使用应用程序时,它运行良好,但在控制台命令上引发以下异常。我仔细检查了语法中的拼写错误。。。代码与上面链接的问题完全相同。我唯一更改的是捆绑包名称。

$ php app/console 
<?
// src/AppBundle/DependencyInjection/Compiler/ValidatorPass.php
namespace AppBundle'DependencyInjection'Compiler;
use Symfony'Component'Finder'Finder;
use Symfony'Component'DependencyInjection'ContainerBuilder;
use Symfony'Component'DependencyInjection'Compiler'CompilerPassInterface;
class ValidatorPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $validatorBuilder = $container->getDefinition('validator.builder');
        $validatorFiles = array();
        $finder = new Finder();
        foreach ($finder->files()->in(__DIR__ . '/../../Resources/config /validation') as $file) {
            $validatorFiles[] = $file->getRealPath();
        }
        $validatorBuilder->addMethodCall('addYamlMappings', array($validatorFiles));
    }
}

[RuntimeException]                                     
The autoloader expected class "AppBundle'DependencyInjection'Compiler'ValidatorPass"
to be defined in file "/home/mt/devel/netsite/phpprojekte/circle8/events/src/AppBundle/DependencyInjection/Compiler/ValidatorPass.php".
The file was found but the class was not in it, the class name or namespace probably has a typo.                                                                                                                                                     

异常跟踪:

() at /.../vendor/symfony/symfony/src/Symfony/Component/Debug/DebugClassLoader.php:186
Symfony'Component'Debug'DebugClassLoader->loadClass() at n/a:n/a
spl_autoload_call() at /.../src/AppBundle/AppBundle.php:17
AppBundle'AppBundle->build() at /.../app/bootstrap.php.cache:2632
Symfony'Component'HttpKernel'Kernel->prepareContainer() at /.../app/bootstrap.php.cache:2611
Symfony'Component'HttpKernel'Kernel->buildContainer() at /.../app/bootstrap.php.cache:2564
Symfony'Component'HttpKernel'Kernel->initializeContainer() at /home/.../app/bootstrap.php.cache:2344
Symfony'Component'HttpKernel'Kernel->boot() at /.../vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:70
Symfony'Bundle'FrameworkBundle'Console'Application->doRun() at /.../vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:126
Symfony'Component'Console'Application->run() at /.../app/console:27

我尝试了我能想到的任何调试方法。请帮忙。我现在唯一能做的就是在使用控制台时在AppBundle.php中注释掉调用,并在使用浏览器时将其注释回。

  • 用户和文件权限似乎并不重要
  • 清空缓存没有帮助

迄今为止尝试过的事情:

  • 修复类的权限

    $ sudo chmod -R 777 src/AppBundle/DependencyInjection/ $ sudo -u daemon php app/console cache:clear --env=dev=>同样的错误。

  • 删除缓存&尝试预热

    $ sudo rm -rf app/cache/* $ sudo chmod 777 app/cache $ sudo app/console cache:warmup=>同样的错误。

手动删除缓存(rm-rf样式),然后以root身份进行预热。修复权限,你就是GTG。

我真的很沮丧,不知道自己做错了什么。我用真的肮脏的方式修复了它。。。

我的AppBundle.php现在看起来是这样的:

<?php
namespace AppBundle;
use Symfony'Component'HttpKernel'Bundle'Bundle;
use Symfony'Component'DependencyInjection'ContainerBuilder;
use Symfony'Component'Finder'Finder;
use Symfony'Component'DependencyInjection'Compiler'CompilerPassInterface;
//use AppBundle'DependencyInjection'Compiler'ValidatorPass;
class AppBundle extends Bundle
{
    // ...
    public function build(ContainerBuilder $container)
    {
        parent::build($container);
        $container->addCompilerPass(new ValidatorPass());
    }
    // ...
}
class ValidatorPass implements CompilerPassInterface {
    public function process(ContainerBuilder $container)
    {
        $validatorBuilder = $container->getDefinition('validator.builder');
        $validatorFiles = array();
        $finder = new Finder();
        foreach ($finder->files()->in(__DIR__ . '/Resources/config/validation') as $file) {
            $validatorFiles[] = $file->getRealPath();
        }
        $validatorBuilder->addMethodCall('addYamlMappings', array($validatorFiles));
    }
}

我真的不喜欢这样,如果能真正解决这个问题,我将不胜感激。