Darkstarone Darkstarone - 10 months ago 183
Twig Question

Symfony 3 Twig\Extension\AbstractExtension not found error in production mode

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

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


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

<?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);
}
}


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.

I'm using Twig 1.8.

Can anyone offer any insight?

Edit:

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

Answer Source

Symfony\Bridge\Twig\Extension\LogoutUrlExtension extends Twig\Extension\AbstractExtension\AbstractExtension (which seemed to be missing for you).

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

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)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download