From 5b4b419863c58cac4df3f7ea1202a350340dc1de Mon Sep 17 00:00:00 2001 From: Osma Suominen Date: Wed, 31 Jan 2024 10:40:06 +0200 Subject: [PATCH] upgrade to Twig 3.8; adjust to API changes --- composer.json | 2 +- src/controller/WebController.php | 26 +++++++++++++------------- tests/Http304Test.php | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index 966c91710..32998712d 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "sweetrdf/easyrdf": "1.7.*", "symfony/polyfill-php80": "1.*", "symfony/polyfill-php81": "1.*", - "twig/twig": "^2.15.3", + "twig/twig": "3.8.*", "willdurand/negotiation": "3.1.*", "punic/punic": "3.5.1", "ml/json-ld": "1.*", diff --git a/src/controller/WebController.php b/src/controller/WebController.php index 46d145f3c..0e898327d 100644 --- a/src/controller/WebController.php +++ b/src/controller/WebController.php @@ -37,9 +37,9 @@ public function __construct($model) } // specify where to look for templates and cache - $loader = new Twig_Loader_Filesystem(__DIR__ . '/../view'); + $loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/../view'); // initialize Twig environment - $this->twig = new Twig_Environment($loader, array('cache' => $tmpDir,'auto_reload' => true)); + $this->twig = new \Twig\Environment($loader, array('cache' => $tmpDir,'auto_reload' => true)); // used for setting the base href for the relative urls $this->twig->addGlobal("BaseHref", $this->getBaseHref()); // setting the service name string from the config.ttl @@ -58,10 +58,10 @@ public function __construct($model) $this->twig->addGlobal("PreferredProperties", array('skos:prefLabel', 'skos:narrower', 'skos:broader', 'skosmos:memberOf', 'skos:altLabel', 'skos:related')); // register a Twig filter for generating URLs for vocabulary resources (concepts and groups) - $this->twig->addFilter(new Twig_SimpleFilter('link_url', array($this, 'linkUrlFilter'))); + $this->twig->addFilter(new \Twig\TwigFilter('link_url', array($this, 'linkUrlFilter'))); // register a Twig filter for generating strings from language codes with CLDR - $langFilter = new Twig_SimpleFilter('lang_name', function ($langcode, $lang) { + $langFilter = new \Twig\TwigFilter('lang_name', function ($langcode, $lang) { return Language::getName($langcode, $lang); }); $this->twig->addFilter($langFilter); @@ -144,7 +144,7 @@ public function invokeLandingPage($request) { $this->model->setLocale($request->getLang()); // load template - $template = $this->twig->loadTemplate('landing.twig'); + $template = $this->twig->load('landing.twig'); // set template variables $categoryLabel = $this->model->getClassificationLabel($request->getLang()); $sortedVocabs = $this->model->getVocabularyList(false, true); @@ -189,7 +189,7 @@ public function invokeVocabularyConcept(Request $request) $customLabels = $vocab->getConfig()->getPropertyLabelOverrides(); $pluginParameters = json_encode($vocab->getConfig()->getPluginParameters()); - $template = $this->twig->loadTemplate('concept.twig'); + $template = $this->twig->load('concept.twig'); $crumbs = $vocab->getBreadCrumbs($request->getContentLang(), $uri); echo $template->render( @@ -212,7 +212,7 @@ public function invokeVocabularyConcept(Request $request) */ public function invokeFeedbackForm($request) { - $template = $this->twig->loadTemplate('feedback.twig'); + $template = $this->twig->load('feedback.twig'); $this->model->setLocale($request->getLang()); $vocabList = $this->model->getVocabularyList(false); $vocab = $request->getVocab(); @@ -300,7 +300,7 @@ public function sendFeedback($request, $message, $messageSubject, $fromName = nu mail($toAddress, $messageSubject, $message, $headers, $params); } catch (Exception $e) { header("HTTP/1.0 404 Not Found"); - $template = $this->twig->loadTemplate('error.twig'); + $template = $this->twig->load('error.twig'); if ($this->model->getConfig()->getLogCaughtExceptions()) { error_log('Caught exception: ' . $e->getMessage()); } @@ -320,7 +320,7 @@ public function sendFeedback($request, $message, $messageSubject, $fromName = nu */ public function invokeAboutPage($request) { - $template = $this->twig->loadTemplate('about.twig'); + $template = $this->twig->load('about.twig'); $this->model->setLocale($request->getLang()); $url = $request->getServerConstant('HTTP_HOST'); @@ -339,7 +339,7 @@ public function invokeAboutPage($request) public function invokeGlobalSearch($request) { $lang = $request->getLang(); - $template = $this->twig->loadTemplate('global-search.twig'); + $template = $this->twig->load('global-search.twig'); $this->model->setLocale($request->getLang()); $parameters = new ConceptSearchParameters($request, $this->model->getConfig()); @@ -408,7 +408,7 @@ public function invokeGlobalSearch($request) */ public function invokeVocabularySearch($request) { - $template = $this->twig->loadTemplate('vocab-search.twig'); + $template = $this->twig->load('vocab-search.twig'); $this->model->setLocale($request->getLang()); $vocab = $request->getVocab(); $searchResults = null; @@ -495,7 +495,7 @@ public function invokeVocabularyHome($request) } $pluginParameters = json_encode($vocab->getConfig()->getPluginParameters()); - $template = $this->twig->loadTemplate('vocab-home.twig'); + $template = $this->twig->load('vocab-home.twig'); echo $template->render( array( @@ -516,7 +516,7 @@ public function invokeGenericErrorPage($request, $message = null) { $this->model->setLocale($request->getLang()); header("HTTP/1.0 404 Not Found"); - $template = $this->twig->loadTemplate('error.twig'); + $template = $this->twig->load('error.twig'); echo $template->render( array( 'languages' => $this->languages, diff --git a/tests/Http304Test.php b/tests/Http304Test.php index b8b0cf412..b215f7d86 100644 --- a/tests/Http304Test.php +++ b/tests/Http304Test.php @@ -49,7 +49,7 @@ public function initObjects(string $vocabularyName) $mockedTemplate = Mockery::mock(); $mockedTemplate->shouldReceive("render")->andReturn("rendered"); $this->twig->allows([ - "loadTemplate" => $mockedTemplate + "load" => $mockedTemplate ]); $this->controller->twig = $this->twig; }