Symfony 3 TwigExtensionAbstractExtension 在生产模式下找不到错误

2022-01-22 00:00:00 php symfony twig

我有一个在开发模式下运行良好的应用程序,但是当我在生产环境中运行它时会弹出:

I have an app that runs fine in dev mode, but when I run it in production this pops up:

Fatal error: Class 'TwigExtensionAbstractExtension' not found in C:UserswackmPhpstormProjectspoemvendorsymfonysymfonysrcSymfonyBridgeTwigExtensionLogoutUrlExtension.php on line 23

我猜这与我的自定义树枝扩展有关:

I'm guessing this relates to my custom twig extension:

<?php
// src/AppBundle/Twig/VersionExtension.php
namespace AppBundleTwig;

use AppBundleEntityVersion;
use PhpOfficePhpSpreadsheetCalculationDateTime;

/**
 * Class VersionExtension - converts versions to readable HTML.
 * @package AppBundleTwig
 */
class VersionExtension extends Twig_Extension
{
    /**
     * Defines the function name for use in twig templates.
     * @return array
     */
    public function getFunctions()
    {
        return array(
            new Twig_SimpleFunction('version', array($this, 'versionFunction')),
        );
    }

    /**
     * Actual function that converts a Version object into a HTML string.
     *
     * Indent indents the associated entities within each other, but NOT
     * the individual items, it does this by incrementing in 0.5 and truncating
     * $indent with intval()
     * @param array $hydrated_entity
     * @param string $title
     * @param indent
     * @return string
     */
    public function versionFunction($hydrated_entity, $title = null, $indent = 0)
    {
        $result_html = '';
        if (!empty($hydrated_entity) and $title != null) {
            $result_html .= '<div style="text-indent:' . (intval($indent) * 20) . 'px"><strong>' . $title . '</strong></div>';
        }
        foreach ($hydrated_entity as $key => $value) {
            // Ignore meta attributes.
            if (in_array($key, array("ID", "Updated At", "Created At"))) {
                continue;
            }

            if (is_array($value)) {
                $result_html .= $this->versionFunction($value, $key, ($indent + 0.5));
            } else {
                $value = $this->tidyValue($value);
                if ($this->startsWith($key, "added_")) {
                    $result_html .= '<div class="bg-success" style="text-indent:' . (intval($indent) * 20) . 'px">' . explode("_", $key)[1] . ': ' . '<p>' . $value . '</p>' . '</div>';
                } else if ($this->startsWith($key, "removed_")) {
                    $result_html .= '<div class="bg-danger" style="text-indent:' . (intval($indent) * 20) . 'px">' . explode("_", $key)[1] . ': ' . '<p>' . $value . '</p>' . '</div>';
                } else {
                    $result_html .= '<div class="bg-info" style="text-indent:' . (intval($indent) * 20) . 'px">' . $key . ': ' . '<p>' . $value . '</p>' . '</div>';
                }
            }
        }
        return $result_html;
    }

    /**
     * Tidies up value for display.
     * @param $value
     * @return string
     */
    private function tidyValue($value)
    {
        // Convert DateTime to String.
        if ($value instanceof DateTime) {
            $value = $value->format('Y-m-d');
        } else if (is_bool($value)) {
            $value = ($value) ? 'true' : 'false';
        } else if (is_string($value)) {
            //Remove extra <p> tags if present.
            $value = preg_replace('!^<p>(.*?)</p>$!i', '$1', $value);
        }
        return $value;
    }

    /**
     * Checks whether a string begins with a another string.
     * @param $haystack
     * @param $needle
     * @return bool
     */
    private function startsWith($haystack, $needle)
    {
        $length = strlen($needle);
        return (substr($haystack, 0, $length) === $needle);
    }
}

扩展是否应该扩展 LogoutUrlExtension.php 以外的其他内容?试图扩展它似乎是一个奇怪的文件.PHPStorm 警告我 Twig_Extension 有多个定义,所以我认为这是相关的.

Should the extension be extending something else other than LogoutUrlExtension.php? It seems like a weird file for it to be trying to extend. PHPStorm has warned me that Twig_Extension has multiple definitions, so I'm thinking this is related.

我使用的是 Twig 1.8.

I'm using Twig 1.8.

谁能提供任何见解?

作曲家.json:

{
  "name": "symfony/framework-standard-edition",
  "license": "MIT",
  "type": "project",
  "description": "The "Symfony Standard Edition" distribution",
  "minimum-stability": "dev",
  "prefer-stable": true,
  "autoload": {
    "psr-4": {
      "": "src/"
    },
    "classmap": [
      "app/AppKernel.php",
      "app/AppCache.php"
    ]
  },
  "autoload-dev": {
    "psr-4": {
      "Tests\": "tests/"
    }
  },
  "require": {
    "php": "7.0.0",
    "symfony/symfony": "3.2.*",
    "doctrine/orm": "^2.5",
    "doctrine/doctrine-bundle": "^1.6",
    "doctrine/doctrine-cache-bundle": "^1.2",
    "symfony/swiftmailer-bundle": "^2.3",
    "symfony/monolog-bundle": "^2.8",
    "symfony/polyfill-apcu": "^1.0",
    "sensio/distribution-bundle": "^5.0",
    "sensio/framework-extra-bundle": "^3.0.2",
    "incenteev/composer-parameter-handler": "^2.0",
    "doctrine/doctrine-fixtures-bundle": "2.3.0",
    "symfony/assetic-bundle": "v2.8.0",
    "twig/twig": "1.*",
    "components/jquery": "3.1.1",
    "friendsofsymfony/user-bundle": "~2.0.0-beta2",
    "fr3d/ldap-bundle": "3.0.*@dev",
    "doctrine/doctrine-migrations-bundle": "^1.0",
    "symfony/console": "^3.2",
    "phpoffice/phpexcel": "^1.8",
    "waldo/datatable-bundle": "^4.0"
  },
  "require-dev": {
    "sensio/generator-bundle": "^3.0",
    "symfony/phpunit-bridge": "^3.0",
    "liip/functional-test-bundle": "^1.6",
    "phpunit/phpunit": "5.7"
  },
  "scripts": {
    "symfony-scripts": [
      "Incenteev\ParameterHandler\ScriptHandler::buildParameters",
      "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap",
      "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache",
      "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets",
      "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installRequirementsFile",
      "Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::prepareDeploymentTarget"
    ],
    "post-install-cmd": [
      "@symfony-scripts"
    ],
    "post-update-cmd": [
      "@symfony-scripts"
    ]
  },
  "config": {
    "platform": {
      "php": "7.0.0"
    }
  },
  "extra": {
    "symfony-app-dir": "app",
    "symfony-bin-dir": "bin",
    "symfony-var-dir": "var",
    "symfony-web-dir": "web",
    "symfony-tests-dir": "tests",
    "symfony-assets-install": "relative",
    "incenteev-parameters": {
      "file": "app/config/parameters.yml"
    },
    "branch-alias": {
      "dev-master": "3.1-dev"
    }
  }
}

推荐答案

SymfonyBridgeTwigExtensionLogoutUrlExtension 扩展 TwigExtensionAbstractExtensionAbstractExtension(其中你好像不见了).

SymfonyBridgeTwigExtensionLogoutUrlExtension extends TwigExtensionAbstractExtensionAbstractExtension (which seemed to be missing for you).

该文件是 twig/extensions 包的一部分,因此 composer require twig/extensions 应该可以解决问题.

That file is part of the twig/extensions package, so composer require twig/extensions should to the trick.

我不知道为什么 Symfony Twig Bridge 不需要 twig/extensions.(packagist.orgrequires 部分> 只提到 twig/twig)

Why twig/extensions isn't required by the Symfony Twig Bridge I don't know. (The requires section on packagist.org only mentions twig/twig)

相关文章