【发布时间】:2018-09-15 13:40:03
【问题描述】:
我对 symfony 和显然是 twig 有疑问。 我从事一个项目已经有一段时间了,当我即将发布一个新版本时(通过 ftp 上传所有内容,如下:https://medium.com/@runawaycoin/deploying-symfony-4-application-to-shared-hosting-with-just-ftp-access-e65d2c5e0e3d)。
就在上传之前,我发现应用程序在前一天停止工作。我开始搜索问题,但日志中没有任何内容。我决定重新上传所有内容并清除缓存,以防万一这可能是问题,但仍然没有,只是一个 500 错误。
然后我启用调试,并得到错误:
The autoloader expected class "Twig\Extension\AbstractExtension" to be defined in file ".../symfony/vendor/composer/../twig/twig/src/Extension/AbstractExtension.php". The file was found but the class was not in it, the class name or namespace probably has a typo.
我尝试了所有可以在网上找到的东西,但唯一遇到类似问题的人通过添加树枝/扩展来解决它,但这也不能解决它。
我完全不明白可能发生了什么,但这个问题似乎是无缘无故出现的,也没有任何文件更改。
该应用程序之前确实可以运行,并且仍然可以离线运行。
这是 AbstractExtension.php 的代码,与 twigphp/twig repo 上的代码相同:
<?php
namespace Twig\Extension;
class_exists('Twig_Extension');
if (\false) {
class AbstractExtension extends \Twig_Extension
{
}
}
PS : if false 听起来很奇怪,但和网上一样,所以我不明白
编辑:这是文本形式的日志:
(1/1) RuntimeException
The autoloader expected class "Twig\Extension\AbstractExtension" to be defined in file "/home/homeviewcx/symfony/vendor/composer/../twig/twig/src/Extension/AbstractExtension.php". The file was found but the class was not in it, the class name or namespace probably has a typo.
in DebugClassLoader.php line 288
at DebugClassLoader->checkClass('Twig\\Extension\\AbstractExtension', '/home/homeviewcx/symfony/vendor/composer/../twig/twig/src/Extension/AbstractExtension.php')
in DebugClassLoader.php line 159
at DebugClassLoader->loadClass('Twig\\Extension\\AbstractExtension')
at spl_autoload_call('Twig\\Extension\\AbstractExtension')
in CsrfExtension.php line 21
at require('/home/homeviewcx/symfony/vendor/symfony/twig-bridge/Extension/CsrfExtension.php')
in DebugClassLoader.php line 145
at DebugClassLoader->loadClass('Symfony\\Bridge\\Twig\\Extension\\CsrfExtension')
at spl_autoload_call('Symfony\\Bridge\\Twig\\Extension\\CsrfExtension')
at class_exists('Symfony\\Bridge\\Twig\\Extension\\CsrfExtension')
in FrameworkExtension.php line 1240
at FrameworkExtension->registerSecurityCsrfConfiguration(array('enabled' => true), object(MergeExtensionConfigurationContainerBuilder), object(XmlFileLoader))
in FrameworkExtension.php line 205
at FrameworkExtension->load(array(array('router' => array('resource' => 'kernel::loadRoutes', 'type' => 'service')), array('secret' => 'env_d3d895c31330f4ea_APP_SECRET_dd4bd60cef72a43e4dea38ee2ba60137', 'session' => array('handler_id' => null), 'php_errors' => array('log' => true), 'cache' => null), array('router' => array('strict_requirements' => null)), array('default_locale' => 'en', 'translator' => array('paths' => array('/home/homeviewcx/symfony/translations'), 'fallbacks' => array('en'))), array('validation' => array('email_validation_mode' => 'html5')), array('cache' => array('pools' => array('doctrine.result_cache_pool' => array('adapter' => 'cache.app'), 'doctrine.system_cache_pool' => array('adapter' => 'cache.system'))))), object(MergeExtensionConfigurationContainerBuilder))
in MergeExtensionConfigurationPass.php line 76
at MergeExtensionConfigurationPass->process(object(ContainerBuilder))
in MergeExtensionConfigurationPass.php line 39
at MergeExtensionConfigurationPass->process(object(ContainerBuilder))
in Compiler.php line 95
at Compiler->compile(object(ContainerBuilder))
in ContainerBuilder.php line 746
at ContainerBuilder->compile()
in Kernel.php line 519
at Kernel->initializeContainer()
in Kernel.php line 123
at Kernel->boot()
in Kernel.php line 183
at Kernel->handle(object(Request))
in app.php line 50
这是我的 composer.json :
{
"type": "project",
"license": "proprietary",
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"sensio/framework-extra-bundle": "^5.1",
"symfony/apache-pack": "^1.0",
"symfony/asset": "^4.1",
"symfony/config": "^4.1",
"symfony/console": "^4.1",
"symfony/dependency-injection": "^4.1",
"symfony/expression-language": "^4.1",
"symfony/flex": "^1.0",
"symfony/form": "^4.1",
"symfony/framework-bundle": "^4.1",
"symfony/lts": "^4@dev",
"symfony/monolog-bundle": "^3.1",
"symfony/orm-pack": "*",
"symfony/process": "^4.1",
"symfony/security-bundle": "^4.1",
"symfony/serializer-pack": "*",
"symfony/swiftmailer-bundle": "^3.1",
"symfony/twig-bundle": "^4.1",
"symfony/validator": "^4.1",
"symfony/web-link": "^4.1",
"symfony/yaml": "^4.1"
},
"require-dev": {
"symfony/debug-pack": "*",
"symfony/dotenv": "^4.1",
"symfony/maker-bundle": "^1.0",
"symfony/profiler-pack": "*",
"symfony/test-pack": "^1.0",
"symfony/web-server-bundle": "^4.1"
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"replace": {
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php71": "*",
"symfony/polyfill-php70": "*",
"symfony/polyfill-php56": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
],
"prod": [
"SET APP_ENV=prod",
"composer install --no-dev --optimize-autoloader",
"composer dump-autoload --optimize --no-dev --classmap-authoritative"
],
"dev": [
"SET APP_ENV=dev",
"composer install"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false
}
}
}
PS:我个人没有在任何地方扩展 AbstractExtension 类(也没有任何其他 twig 类),也没有触及容器编译器(这里仍然相对较新),但是是的,可能就是这样
【问题讨论】:
-
if (\false) 部分是从哪里得到的?这不仅听起来很奇怪,而且是问题的确切原因。此外,class_exists 在哪里也没有多大意义。
-
在那里和我的服务器上找到它:github.com/twigphp/Twig/blob/2.x/src/Extension/…
-
我进行了更多搜索,他们将 require '...' 更改为 2017 年 6 月存在的类,所以我认为这不能解释为什么我突然收到错误
-
问题可能不在 symfony 本身 - 至少不是直接的 - 否则会有更多人遇到同样的问题并且它会得到解决。鉴于您的项目在您的本地开发环境中仍然可以正常运行,但是当 symfony compiles the container 时,问题可能会在某个地方触发。
-
为了将来参考,由于 class_alias here,在野外扩展 AbstractExtension 实际上会起作用。