生产模式中没有找到Symfony 3 Twig \ Extension \ AbstractExtension错误

时间:2017-11-23 01:17:21

标签: php symfony twig

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

Fatal error: Class 'Twig\Extension\AbstractExtension' not found in C:\Users\wackm\PhpstormProjects\poem\vendor\symfony\symfony\src\Symfony\Bridge\Twig\Extension\LogoutUrlExtension.php on line 23

我猜测这与我的自定义树枝延伸相关:

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

use AppBundle\Entity\Version;
use PhpOffice\PhpSpreadsheet\Calculation\DateTime;

/**
 * Class VersionExtension - converts versions to readable HTML.
 * @package AppBundle\Twig
 */
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有多个定义,所以我认为这是相关的。

我正在使用Twig 1.8。

任何人都可以提供任何见解吗?

编辑:

Composer.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"
    }
  }
}

1 个答案:

答案 0 :(得分:1)

Symfony\Bridge\Twig\Extension\LogoutUrlExtension延伸Twig\Extension\AbstractExtension\AbstractExtension(这对您来说似乎不见了。)

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

Symfony Twig Bridge不需要twig/extensions为什么我不知道。 (packagist.org上的requires部分仅提及twig/twig