Updated system to use plates instead of twig

This commit is contained in:
Alejandro Celaya 2017-10-12 09:40:42 +02:00
parent d10583c7c5
commit 453ca1728e
16 changed files with 107 additions and 177 deletions

View file

@ -6,7 +6,6 @@ branches:
- develop
php:
- 5.6
- 7
- 7.1
- 7.2

View file

@ -13,39 +13,40 @@
],
"require": {
"php": "^7.0",
"roave/security-advisories": "dev-master",
"zendframework/zend-expressive": "^2.0",
"zendframework/zend-expressive-fastroute": "^2.0",
"zendframework/zend-expressive-twigrenderer": "^1.4",
"zendframework/zend-stdlib": "^3.0",
"zendframework/zend-servicemanager": "^3.0",
"zendframework/zend-paginator": "^2.6",
"zendframework/zend-config": "^3.0",
"zendframework/zend-i18n": "^2.7",
"zendframework/zend-config-aggregator": "^0.1",
"acelaya/ze-content-based-error-handler": "^2.0",
"doctrine/orm": "^2.5 <2.6",
"doctrine/dbal": "^2.5 <2.6",
"doctrine/annotations": "^1.4 <1.5",
"doctrine/cache": "^1.6 <1.7",
"doctrine/collections": "^1.4 <1.5",
"doctrine/common": "^2.7 <2.8",
"doctrine/cache": "^1.6 <1.7",
"guzzlehttp/guzzle": "^6.2",
"symfony/console": "^3.0",
"symfony/process": "^3.0",
"symfony/filesystem": "^3.0",
"firebase/php-jwt": "^4.0",
"monolog/monolog": "^1.21",
"theorchard/monolog-cascade": "^0.4",
"endroid/qrcode": "^1.7",
"mikehaertl/phpwkhtmltopdf": "^2.2",
"doctrine/dbal": "^2.5 <2.6",
"doctrine/migrations": "^1.4",
"http-interop/http-middleware": "^0.4.1"
"doctrine/orm": "^2.5 <2.6",
"endroid/qrcode": "^1.7",
"firebase/php-jwt": "^4.0",
"guzzlehttp/guzzle": "^6.2",
"http-interop/http-middleware": "^0.4.1",
"mikehaertl/phpwkhtmltopdf": "^2.2",
"monolog/monolog": "^1.21",
"roave/security-advisories": "dev-master",
"symfony/console": "^3.0",
"symfony/filesystem": "^3.0",
"symfony/process": "^3.0",
"theorchard/monolog-cascade": "^0.4",
"zendframework/zend-config": "^3.0",
"zendframework/zend-config-aggregator": "^0.1",
"zendframework/zend-expressive": "^2.0",
"zendframework/zend-expressive-fastroute": "^2.0",
"zendframework/zend-expressive-helpers": "^4.2",
"zendframework/zend-expressive-platesrenderer": "^1.3",
"zendframework/zend-i18n": "^2.7",
"zendframework/zend-paginator": "^2.6",
"zendframework/zend-servicemanager": "^3.0",
"zendframework/zend-stdlib": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7 || ^6.0",
"squizlabs/php_codesniffer": "^3.1",
"filp/whoops": "^2.0",
"phpunit/phpunit": "^6.0",
"squizlabs/php_codesniffer": "^3.1",
"symfony/var-dumper": "^3.0",
"vlucas/phpdotenv": "^2.2",
"zendframework/zend-expressive-tooling": "^0.4"
@ -81,6 +82,7 @@
"pretty-test": "phpunit --coverage-html build/coverage"
},
"config": {
"process-timeout": 0
"process-timeout": 0,
"sort-packages": true
}
}

View file

@ -1,11 +1,15 @@
<?php
declare(strict_types=1);
use Shlinkio\Shlink\Common\Factory\EmptyResponseImplicitOptionsMiddlewareFactory;
use Zend\Expressive;
use Zend\Expressive\Container;
use Zend\Expressive\Middleware;
use Zend\Expressive\Plates;
use Zend\Expressive\Router;
use Zend\Expressive\Template;
use Zend\Expressive\Twig;
use Zend\Expressive\Helper;
use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\Stratigility\Middleware\ErrorHandler;
return [
@ -13,11 +17,13 @@ return [
'dependencies' => [
'factories' => [
Expressive\Application::class => Container\ApplicationFactory::class,
Template\TemplateRendererInterface::class => Twig\TwigRendererFactory::class,
\Twig_Environment::class => Twig\TwigEnvironmentFactory::class,
Template\TemplateRendererInterface::class => Plates\PlatesRendererFactory::class,
Router\RouterInterface::class => Router\FastRouteRouterFactory::class,
ErrorHandler::class => Container\ErrorHandlerFactory::class,
Middleware\ImplicitOptionsMiddleware::class => EmptyResponseImplicitOptionsMiddlewareFactory::class,
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
Helper\ServerUrlHelper::class => InvokableFactory::class,
],
],

View file

@ -2,8 +2,8 @@
return [
'twig' => [
'cache_dir' => 'data/cache/twig',
'templates' => [
'extension' => 'phtml',
'extensions' => [
// extension service names or instances
],

View file

@ -9,7 +9,7 @@ use Shlinkio\Shlink\Common\Image;
use Shlinkio\Shlink\Common\Image\ImageBuilder;
use Shlinkio\Shlink\Common\Middleware\LocaleMiddleware;
use Shlinkio\Shlink\Common\Service;
use Shlinkio\Shlink\Common\Twig\Extension\TranslatorExtension;
use Shlinkio\Shlink\Common\Template\Extension\TranslatorExtension;
use Symfony\Component\Filesystem\Filesystem;
use Zend\I18n\Translator\Translator;
use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory;

View file

@ -1,9 +1,11 @@
<?php
use Shlinkio\Shlink\Common\Twig\Extension\TranslatorExtension;
declare(strict_types=1);
use Shlinkio\Shlink\Common\Template\Extension\TranslatorExtension;
return [
'twig' => [
'templates' => [
'extensions' => [
TranslatorExtension::class,
],

View file

@ -0,0 +1,25 @@
<?php
namespace Shlinkio\Shlink\Common\Template\Extension;
use League\Plates\Engine;
use League\Plates\Extension\ExtensionInterface;
use Zend\I18n\Translator\TranslatorInterface;
class TranslatorExtension implements ExtensionInterface
{
/**
* @var TranslatorInterface
*/
private $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
public function register(Engine $engine)
{
$engine->registerFunction('translate', [$this->translator, 'translate']);
$engine->registerFunction('translate_plural', [$this->translator, 'translatePlural']);
}
}

View file

@ -1,68 +0,0 @@
<?php
namespace Shlinkio\Shlink\Common\Twig\Extension;
use Zend\I18n\Translator\TranslatorInterface;
class TranslatorExtension extends \Twig_Extension implements TranslatorInterface
{
/**
* @var TranslatorInterface
*/
private $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return __CLASS__;
}
public function getFunctions()
{
return [
new \Twig_SimpleFunction('translate', [$this, 'translate']),
new \Twig_SimpleFunction('translate_plural', [$this, 'translatePlural']),
];
}
/**
* Translate a message.
*
* @param string $message
* @param string $textDomain
* @param string $locale
* @return string
*/
public function translate($message, $textDomain = 'default', $locale = null)
{
return $this->translator->translate($message, $textDomain, $locale);
}
/**
* Translate a plural message.
*
* @param string $singular
* @param string $plural
* @param int $number
* @param string $textDomain
* @param string|null $locale
* @return string
*/
public function translatePlural(
$singular,
$plural,
$number,
$textDomain = 'default',
$locale = null
) {
$this->translator->translatePlural($singular, $plural, $number, $textDomain, $locale);
}
}

View file

@ -24,6 +24,6 @@ class ConfigProviderTest extends TestCase
$config = $this->configProvider->__invoke();
$this->assertArrayHasKey('dependencies', $config);
$this->assertArrayHasKey('twig', $config);
$this->assertArrayHasKey('templates', $config);
}
}

View file

@ -0,0 +1,34 @@
<?php
namespace ShlinkioTest\Shlink\Common\Template\Extension;
use League\Plates\Engine;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Shlinkio\Shlink\Common\Template\Extension\TranslatorExtension;
use Zend\I18n\Translator\Translator;
class TranslatorExtensionTest extends TestCase
{
/**
* @var TranslatorExtension
*/
protected $extension;
public function setUp()
{
$this->extension = new TranslatorExtension($this->prophesize(Translator::class)->reveal());
}
/**
* @test
*/
public function properFunctionsAreReturned()
{
$engine = $this->prophesize(Engine::class);
$engine->registerFunction('translate', Argument::type('callable'))->shouldBeCalledTimes(1);
$engine->registerFunction('translate_plural', Argument::type('callable'))->shouldBeCalledTimes(1);
$funcs = $this->extension->register($engine->reveal());
}
}

View file

@ -1,70 +0,0 @@
<?php
namespace ShlinkioTest\Shlink\Common\Twig\Extension;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\Common\Twig\Extension\TranslatorExtension;
use Zend\I18n\Translator\Translator;
class TranslatorExtensionTest extends TestCase
{
/**
* @var TranslatorExtension
*/
protected $extension;
/**
* @var ObjectProphecy
*/
protected $translator;
public function setUp()
{
$this->translator = $this->prophesize(Translator::class);
$this->extension = new TranslatorExtension($this->translator->reveal());
}
/**
* @test
*/
public function extensionNameIsClassName()
{
$this->assertEquals(TranslatorExtension::class, $this->extension->getName());
}
/**
* @test
*/
public function properFunctionsAreReturned()
{
$funcs = $this->extension->getFunctions();
$this->assertCount(2, $funcs);
foreach ($funcs as $func) {
$this->assertInstanceOf(\Twig_SimpleFunction::class, $func);
}
}
/**
* @test
*/
public function translateFallbacksToTranslator()
{
$this->translator->translate('foo', 'default', null)->shouldBeCalledTimes(1);
$this->extension->translate('foo');
$this->translator->translate('bar', 'baz', 'en')->shouldBeCalledTimes(1);
$this->extension->translate('bar', 'baz', 'en');
}
/**
* @test
*/
public function translatePluralFallbacksToTranslator()
{
$this->translator->translatePlural('foo', 'bar', 'baz', 'default', null)->shouldBeCalledTimes(1);
$this->extension->translatePlural('foo', 'bar', 'baz');
$this->translator->translatePlural('foo', 'bar', 'baz', 'another', 'en')->shouldBeCalledTimes(1);
$this->extension->translatePlural('foo', 'bar', 'baz', 'another', 'en');
}
}

View file

@ -4,7 +4,7 @@ return [
'templates' => [
'paths' => [
'module/Core/templates',
'ShlinkCore' => __DIR__ . '/../templates',
],
],

View file

@ -4,8 +4,8 @@ return [
'zend-expressive' => [
'error_handler' => [
'template_404' => 'core/error/404.html.twig',
'template_error' => 'core/error/error.html.twig',
'template_404' => 'ShlinkCore::error/404',
'template_error' => 'ShlinkCore::error/error',
],
],