Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deprecation warning since twig 1.27 #124

Open
wants to merge 22 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 36 additions & 18 deletions examples/application/index/index.twig
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
{% extends 'layout/layout.twig' %}

{% block content %}
<div class="hero-unit">
<h1>{{ translate('Welcome to %sZend Framework 2%s'|format('<span class="zf-green">', '</span>')) }}</h1>
<p>{{ translate('Congratulations! You have successfully installed the %sZF2 Skeleton Application%s. You are currently running Zend Framework version %s. This skeleton can serve as a simple starting point for you to begin building your application on ZF2.'|format('<a href="https://github.com/zendframework/ZendSkeletonApplication" target="_blank">', '</a>', 2)) }}</p>
<p><a class="btn btn-success btn-large" href="https://github.com/zendframework/zf2" target="_blank">{{ translate('Fork Zend Framework 2 on GitHub') }} &raquo;</a></p>
</div>
<div class="jumbotron">
<h1>{{ translate('Welcome to %sZend Framework 2%s'|format('<span class="zf-green">', '</span>')) }}</h1>
<p>{{ translate('Congratulations! You have successfully installed the %sZF2 Skeleton Application%s. You are currently running Zend Framework version %s. This skeleton can serve as a simple starting point for you to begin building your application on ZF2.'|format('<a href="https://github.com/zendframework/ZendSkeletonApplication" target="_blank">', '</a>', 2)) }}</p>
<p><a class="btn btn-success btn-lg" href="https://github.com/zendframework/zf2" target="_blank">{{ translate('Fork Zend Framework 2 on GitHub') }} &raquo;</a></p>
</div>

<div class="row">
<div class="row">

<div class="span4">
<h2>{{ translate('Follow Development') }}</h2>
<p>{{ translate('Zend Framework 2 is under active development. If you are interested in following the development of ZF2, there is a special ZF2 portal on the official Zend Framework website which provides links to the ZF2 %swiki%s, %sdev blog%s, %sissue tracker%s, and much more. This is a great resource for staying up to date with the latest developments!'|format('<a href="http://framework.zend.com/wiki/display/ZFDEV2/Home">', '</a>', '<a href="http://framework.zend.com/zf2/blog">', '</a>', '<a href="http://framework.zend.com/issues/browse/ZF2">', '</a>')) }}</p>
<p><a class="btn btn-success" href="http://framework.zend.com/zf2" target="_blank">{{ translate('ZF2 Development Portal') }} &raquo;</a></p>
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ translate('Follow Development') }}</h3>
</div>
<div class="panel-body">
<p>{{ translate('Zend Framework 2 is under active development. If you are interested in following the development of ZF2, there is a special ZF2 portal on the official Zend Framework website which provides links to the ZF2 %swiki%s, %sdev blog%s, %sissue tracker%s, and much more. This is a great resource for staying up to date with the latest developments!'|format('<a href="http://framework.zend.com/wiki/display/ZFDEV2/Home">', '</a>', '<a href="http://framework.zend.com/zf2/blog">', '</a>', '<a href="http://framework.zend.com/issues/browse/ZF2">', '</a>')) }}</p>
<p><a class="btn btn-success pull-right" href="http://framework.zend.com/zf2" target="_blank">{{ translate('ZF2 Development Portal') }} &raquo;</a></p>
</div>
</div>
</div>

<div class="span4">
<h2>{{ translate('Discover Modules') }}</h2>
<p>{{ translate('The community is working on developing a community site to serve as a repository and gallery for ZF2 modules. The project is available %son GitHub%s. The site is currently live and currently contains a list of some of the modules already available for ZF2.'|format('<a href="https://github.com/zendframework/modules.zendframework.com">', '</a>')) }}</p>
<p><a class="btn btn-success" href="http://modules.zendframework.com/" target="_blank">{{ translate('Explore ZF2 Modules') }} &raquo;</a></p>
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ translate('Discover Modules') }}</h3>
</div>
<div class="panel-body">
<p>{{ translate('The community is working on developing a community site to serve as a repository and gallery for ZF2 modules. The project is available %son GitHub%s. The site is currently live and currently contains a list of some of the modules already available for ZF2.'|format('<a href="https://github.com/zendframework/modules.zendframework.com">', '</a>')) }}</p>
<p><a class="btn btn-success pull-right" href="http://modules.zendframework.com/" target="_blank">{{ translate('Explore ZF2 Modules') }} &raquo;</a></p>
</div>
</div>
</div>

<div class="span4">
<h2>{{ translate('Help &amp; Support') }}</h2>
<p>{{ translate('If you need any help or support while developing with ZF2, you may reach us via IRC: %s#zftalk on Freenode%s. We\'d love to hear any questions or feedback you may have regarding the beta releases. Alternatively, you may subscribe and post questions to the %smailing lists%s.'|format('<a href="irc://irc.freenode.net/zftalk">', '</a>', '<a href="http://framework.zend.com/wiki/display/ZFDEV/Mailing+Lists">', '</a>')) }}</p>
<p><a class="btn btn-success" href="http://webchat.freenode.net?channels=zftalk" target="_blank">{{ translate('Ping us on IRC') }} &raquo;</a></p>
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ translate('Help &amp; Support') }}</h3>
</div>
<div class="panel-body">
<p>{{ translate('If you need any help or support while developing with ZF2, you may reach us via IRC: %s#zftalk on Freenode%s. We\'d love to hear any questions or feedback you may have regarding the beta releases. Alternatively, you may subscribe and post questions to the %smailing lists%s.'|format('<a href="irc://irc.freenode.net/zftalk">', '</a>', '<a href="http://framework.zend.com/wiki/display/ZFDEV/Mailing+Lists">', '</a>')) }}</p>
<p><a class="btn btn-success pull-right" href="http://webchat.freenode.net?channels=zftalk" target="_blank">{{ translate('Ping us on IRC') }} &raquo;</a></p>
</div>
</div>
</div>
</div>
{% endblock content %}
45 changes: 23 additions & 22 deletions examples/layout/layout.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,43 @@
{% block meta %}{% endblock meta %}

<!-- Le styles -->
<link href="/css/bootstrap.min.css" media="screen" rel="stylesheet" type="text/css">
<link href="/css/style.css" media="screen" rel="stylesheet" type="text/css">
<link href="/css/bootstrap-responsive.min.css" media="screen" rel="stylesheet" type="text/css">
<link href="/images/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon">
<link href="{{ basePath() }}/images/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon">
<link href="{{ basePath() }}/css/style.css" media="screen" rel="stylesheet" type="text/css">
<link href="{{ basePath() }}/css/bootstrap-theme.min.css" media="screen" rel="stylesheet" type="text/css">
<link href="{{ basePath() }}/css/bootstrap.min.css" media="screen" rel="stylesheet" type="text/css">

{% block style %}{% endblock style %}

<!-- Scripts -->
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/bootstrap.min.js"></script>
<!--[if lt IE 9]><script type="text/javascript" src="/js/html5.js"></script><![endif]-->
<script type="text/javascript" src="{{ basePath() }}/js/jquery.min.js"></script>
<script type="text/javascript" src="{{ basePath() }}/js/bootstrap.min.js"></script>
<!--[if lt IE 9]><script type="text/javascript" src="{{ basePath() }}/js/respond.min.js"></script><![endif]-->
<!--[if lt IE 9]><script type="text/javascript" src="{{ basePath() }}/js/html5shiv.js"></script><![endif]-->
{% block script %}{% endblock script %}
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="{{ url('home') }}">{{ translate('Skeleton Application') }}</a>
<div class="nav-collapse collapse">
<ul class="nav">
<li class="active"><a href="{{ url('home') }}">{{ translate('Home') }}</a></li>
</ul>
</div><!--/.nav-collapse -->
</button>
<a class="navbar-brand" href="{{ url('home') }}"><img src="{{ basePath() }}/img/zf2-logo.png" alt="Zend Framework 2"/>&nbsp;{{ translate('Skeleton Application') }}</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="{{ url('home') }}">{{ translate('Home') }}</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</nav>
<div class="container">
<!-- content|raw is required for phtml fallback -->
{% block content %}{{ content|raw }}{% endblock content %}
<hr>
<footer>
<p>&copy; 2005 - 2012 by Zend Technologies Ltd. {{ translate('All rights reserved.') }}</p>
<p>&copy; 2005 - 2014 by Zend Technologies Ltd. {{ translate('All rights reserved.') }}</p>
</footer>
</div> <!-- /container -->
{% block inline %}{% endblock inline %}
Expand Down
21 changes: 21 additions & 0 deletions src/ZfcTwig/ModuleOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class ModuleOptions extends AbstractOptions
*/
protected $environmentOptions = array();

/**
* @var array
*/
protected $globals = array();

/**
* @var array
*/
Expand Down Expand Up @@ -210,5 +215,21 @@ public function getEnvironmentClass()
{
return $this->environmentClass;
}

/**
* @param array $globals
*/
public function setGlobals($globals)
{
$this->globals = $globals;
}

/**
* @return array
*/
public function getGlobals()
{
return $this->globals;
}
}

7 changes: 6 additions & 1 deletion src/ZfcTwig/Twig/EnvironmentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public function createService(ServiceLocatorInterface $serviceLocator)
/** @var \ZfcTwig\moduleOptions $options */
$options = $serviceLocator->get('ZfcTwig\ModuleOptions');
$envClass = $options->getEnvironmentClass();


/** @var \Twig_Environment $env */
$env = new $envClass(null, $options->getEnvironmentOptions());

if ($options->getEnableFallbackFunctions()) {
Expand All @@ -45,6 +46,10 @@ function ($name) use ($helperPluginManager) {

$env->setLoader($serviceLocator->get($options->getEnvironmentLoader()));

foreach ($options->getGlobals() as $name => $value) {
$env->addGlobal($name, $value);
}

// Extensions are loaded later to avoid circular dependencies (for example, if an extension needs Renderer).
return $env;
}
Expand Down
8 changes: 7 additions & 1 deletion src/ZfcTwig/Twig/MapLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public function getSource($name)
$name
));
}
if(!file_exists($this->map[$name])) {
throw new Twig_Error_Loader(sprintf(
'Unable to open file "%s" from template map',
$this->map[$name]
));
}
return file_get_contents($this->map[$name]);
}

Expand All @@ -71,4 +77,4 @@ public function isFresh($name, $time)
{
return filemtime($this->map[$name]) <= $time;
}
}
}
11 changes: 7 additions & 4 deletions src/ZfcTwig/Twig/StackLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getDefaultSuffix()
return $this->defaultSuffix;
}

protected function findTemplate($name)
protected function findTemplate($name, $throw = true)
{
$name = (string) $name;

Expand Down Expand Up @@ -78,7 +78,10 @@ protected function findTemplate($name)
return $this->cache[$name] = $path.'/'.$name;
}
}

throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace])));
if ($throw) {
throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace])));
} else {
return false;
}
}
}
}
3 changes: 2 additions & 1 deletion src/ZfcTwig/View/HelperPluginManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function createService(ServiceLocatorInterface $serviceLocator)

$baseManager = $serviceLocator->get('ViewHelperManager');
$twigManager = new HelperPluginManager(new Config($managerOptions));
$twigManager->setServiceLocator($serviceLocator);
$twigManager->addPeeringServiceManager($baseManager);

foreach ($managerConfigs as $configClass) {
Expand All @@ -51,4 +52,4 @@ public function createService(ServiceLocatorInterface $serviceLocator)

return $twigManager;
}
}
}
5 changes: 5 additions & 0 deletions src/ZfcTwig/View/TwigRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class TwigRenderer implements RendererInterface, TreeRendererInterface
*/
protected $helperPluginManager;

/**
* @var Twig_Loader_Chain
*/
protected $loader;

/**
* @var TwigResolver
*/
Expand Down
18 changes: 11 additions & 7 deletions tests/ZfcTwigTest/View/Strategy/TwigStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@
use PHPUnit_Framework_TestCase as TestCase;
use Twig_Environment;
use Twig_Loader_Array;
use Twig_Loader_Chain;
use Zend\View\View;
use Zend\View\ViewEvent;
use ZfcTwig\View\Renderer\TwigRenderer;
use ZfcTwig\View\Strategy\TwigStrategy;
use ZfcTwig\View\Resolver\TwigResolver;
use ZfcTwig\View\TwigRenderer;
use ZfcTwig\View\TwigStrategy;
use ZfcTwig\View\TwigResolver;

class TwigStrategyTest extends TestCase
{
public function setUp()
{
$this->environment = new Twig_Environment(new Twig_Loader_Array(array('key1' => 'var1')));
$this->renderer = new TwigRenderer(new View, $this->environment, new TwigResolver($this->environment));
$this->strategy = new TwigStrategy($this->renderer);
$this->event = new ViewEvent();
$this->chain = $chain = new Twig_Loader_Chain();
$this->loader = new Twig_Loader_Array(array('key1' => 'var1'));
$this->chain->addLoader($this->loader);
$this->environment = new Twig_Environment($this->chain);
$this->renderer = new TwigRenderer(new View, $this->chain, $this->environment, new TwigResolver($this->environment));
$this->strategy = new TwigStrategy($this->renderer);
$this->event = new ViewEvent();
}

public function testSelectRendererWhenTemplateFound()
Expand Down