У меня возникли проблемы с правильным переходом от компилятора по умолчанию к пользовательскому компилятору. Похоже, я неправильно создаю класс или объект.
Из http://twig.sensiolabs.org/doc/internals.html "По умолчанию компилятор (Twig_Compiler) можно изменить, вызвав метод setCompiler(): $twig->setCompiler($compiler);"
Здесь я пытаюсь изменить компилятор:
\Twig_Autoloader::register();
$loader = new \Twig_Loader_Filesystem($templateDirectory);
$twig = new \Twig_Environment($loader);
$compiler = new Config_Generator_Twig_Compiler($twig);
$twig->setCompiler($compiler);
Но я получаю эту ошибку:
Перехватываемая фатальная ошибка PHP: аргумент 1, переданный в Twig_Node_Module::compile(), должен быть экземпляром Twig_Compiler, экземпляром Config_Generator_Twig_Compiler, который вызывается в /config_generator/Config_Generator_Twig_Compiler.php в строке 87 и определяется в /config_generator/lib/Twig/Node/ Module.php в строке 36
«Config_Generator_Twig_Compiler.php» — это просто копия Twig-файла «Compiler.php» с измененной парой строк вверху:
<?php
require_once $rootDirectory.'/lib/Twig/CompilerInterface.php'; <---- added this line
class Config_Generator_Twig_Compiler implements Twig_CompilerInterface <---- changed from "class Twig_Compiler implements Twig_CompilerInterface"
Остальная часть этого файла в настоящее время такая же, как «Compiler.php»:
{...
public function compile(Twig_NodeInterface $node, $indentation = 0)
{
$this->lastLine = null;
$this->source = '';
$this->sourceOffset = 0;
// source code starts at 1 (as we then increment it when we encounter new lines)
$this->sourceLine = 1;
$this->indentation = $indentation;
if ($node instanceof Twig_Node_Module) {
$this->filename = $node->getAttribute('filename');
}
$node->compile($this); <---- here's line 87
return $this;
}
...}
Twig_Compiler
. - person Maerlyn   schedule 12.03.2015class Config_Generator_Twig_Compiler extends Twig_Compiler
. Из-за зависимости мне также пришлось добавить их вверху моего скрипта в следующем порядке:require_once $rootDirectory.'/lib/Twig/CompilerInterface.php'; require_once $rootDirectory.'/lib/Twig/Compiler.php';
- person d_edmonton   schedule 12.03.2015