Symfony - twig - Autoloader expected class

2019-06-24 22:12发布

问题:

I have a problem with symfony and apparently twig. I've been working on a project for some time, and as I was about to make a new release (by uploading everything througn ftp, following this : https://medium.com/@runawaycoin/deploying-symfony-4-application-to-shared-hosting-with-just-ftp-access-e65d2c5e0e3d).

Just before doing the upload, I discover that the application stopped working a day before. I start to search for the issue, but there wasn't anything in the logs. I decide to reupload everything and clear the cache, in case that could be the problem, but still nothing, just a 500 error.

I then enable the debug, and get the error :

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.

I tried everything I could find online, but the only one who had a similar problem solved it by adding twig/extensions, but this does not solve it either.

I don't get at all what may have happened, but this problem seemed to have appeared with no reason, and without any file change.

The application did work before, and still works offline.

Here's the code of the AbstractExtension.php, which is the same as on the twigphp/twig repo :

<?php

namespace Twig\Extension;

class_exists('Twig_Extension');

if (\false) {
    class AbstractExtension extends \Twig_Extension
    {
    }
}

PS : this if false sounds weird but is the same as online so i don't get it

EDIT : here's the log as text :

(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

here's my 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: I'm personally not extending the AbstractExtension class anywhere (nor any other twig classes), and haven't touched the container compiler either (still relatively new here), but yes, that might be it

回答1:

Looking at the commit that caused the error here :

https://github.com/twigphp/Twig/commit/2c174e4015dcfdcb4f39d004e3b1aeccae77aa25#diff-d2944cb9ff29770359c04590530f931aL5

The only change is the way the class is expected to be loaded, since class_exists('Twig_Extension') will call __autoload() thus the class is expected to be already called at this stage.

It looks like you already updated your dependencies, but did you clear the cache ? Since the problem is not available locally (works fine on dev), on the issue is only on your server (app runing in prod mode) symfony could be still using the old cache which explain the error.

If that's the case, you will need to refresh the cache on your server by running:

php bin/console cache:clear --no-warmup --env=prod



标签: php symfony twig