diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2018-09-08 12:48:08 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2018-09-09 12:11:12 +0200 |
commit | 222c9fed7d4ca2b3b44c05907fbb7080c1efd342 (patch) | |
tree | 62b1bfa9233000985331b9dea703d51dc24515a2 /src | |
parent | 2d6bca1357faff28bc1f86a56b432cc463ff7574 (diff) | |
parent | 8257864829ffdfb410f05e0dd0a9c781f48b741a (diff) |
Merge remote-tracking branch 'MyIgel/templating'
Diffstat (limited to 'src')
-rw-r--r-- | src/Application.php | 1 | ||||
-rw-r--r-- | src/Config/ConfigServiceProvider.php | 1 | ||||
-rw-r--r-- | src/Helpers/TranslationServiceProvider.php | 58 | ||||
-rw-r--r-- | src/Helpers/Translator.php | 105 | ||||
-rw-r--r-- | src/Http/Response.php | 41 | ||||
-rw-r--r-- | src/Http/SessionServiceProvider.php | 1 | ||||
-rw-r--r-- | src/Http/UrlGeneratorServiceProvider.php | 1 | ||||
-rw-r--r-- | src/Middleware/ErrorHandler.php | 80 | ||||
-rw-r--r-- | src/Middleware/LegacyMiddleware.php | 24 | ||||
-rw-r--r-- | src/Middleware/SetLocale.php | 49 | ||||
-rw-r--r-- | src/Renderer/RendererServiceProvider.php | 2 | ||||
-rw-r--r-- | src/Renderer/Twig/Extensions/Config.php | 31 | ||||
-rw-r--r-- | src/Renderer/Twig/Extensions/Globals.php | 23 | ||||
-rw-r--r-- | src/Renderer/Twig/Extensions/Session.php | 31 | ||||
-rw-r--r-- | src/Renderer/Twig/Extensions/Translation.php | 57 | ||||
-rw-r--r-- | src/Renderer/Twig/Extensions/Url.php | 43 | ||||
-rw-r--r-- | src/Renderer/TwigEngine.php | 41 | ||||
-rw-r--r-- | src/Renderer/TwigLoader.php | 26 | ||||
-rw-r--r-- | src/Renderer/TwigServiceProvider.php | 77 | ||||
-rw-r--r-- | src/helpers.php | 39 |
20 files changed, 711 insertions, 20 deletions
diff --git a/src/Application.php b/src/Application.php index 6644a6cf..86397a2c 100644 --- a/src/Application.php +++ b/src/Application.php @@ -107,6 +107,7 @@ class Application extends Container $this->instance('path', $appPath); $this->instance('path.config', $appPath . DIRECTORY_SEPARATOR . 'config'); $this->instance('path.lang', $appPath . DIRECTORY_SEPARATOR . 'locale'); + $this->instance('path.views', $appPath . DIRECTORY_SEPARATOR . 'templates'); } /** diff --git a/src/Config/ConfigServiceProvider.php b/src/Config/ConfigServiceProvider.php index 9fbccd68..63f43ced 100644 --- a/src/Config/ConfigServiceProvider.php +++ b/src/Config/ConfigServiceProvider.php @@ -13,6 +13,7 @@ class ConfigServiceProvider extends ServiceProvider public function register() { $config = $this->app->make(Config::class); + $this->app->instance(Config::class, $config); $this->app->instance('config', $config); foreach ($this->configFiles as $file) { diff --git a/src/Helpers/TranslationServiceProvider.php b/src/Helpers/TranslationServiceProvider.php new file mode 100644 index 00000000..9d86df7d --- /dev/null +++ b/src/Helpers/TranslationServiceProvider.php @@ -0,0 +1,58 @@ +<?php + +namespace Engelsystem\Helpers; + +use Engelsystem\Config\Config; +use Engelsystem\Container\ServiceProvider; +use Symfony\Component\HttpFoundation\Session\Session; + +class TranslationServiceProvider extends ServiceProvider +{ + public function register() + { + /** @var Config $config */ + $config = $this->app->get('config'); + /** @var Session $session */ + $session = $this->app->get('session'); + + $locales = $config->get('locales'); + $locale = $config->get('default_locale'); + + $sessionLocale = $session->get('locale', $locale); + if (isset($locales[$sessionLocale])) { + $locale = $sessionLocale; + } + + $this->initGettext(); + $session->set('locale', $locale); + + $translator = $this->app->make( + Translator::class, + ['locale' => $locale, 'locales' => $locales, 'localeChangeCallback' => [$this, 'setLocale']] + ); + $this->app->instance(Translator::class, $translator); + $this->app->instance('translator', $translator); + } + + /** + * @param string $textDomain + * @param string $encoding + * @codeCoverageIgnore + */ + protected function initGettext($textDomain = 'default', $encoding = 'UTF-8') + { + bindtextdomain($textDomain, $this->app->get('path.lang')); + bind_textdomain_codeset($textDomain, $encoding); + textdomain($textDomain); + } + + /** + * @param string $locale + * @codeCoverageIgnore + */ + public function setLocale($locale) + { + putenv('LC_ALL=' . $locale); + setlocale(LC_ALL, $locale); + } +} diff --git a/src/Helpers/Translator.php b/src/Helpers/Translator.php new file mode 100644 index 00000000..1e953a21 --- /dev/null +++ b/src/Helpers/Translator.php @@ -0,0 +1,105 @@ +<?php + +namespace Engelsystem\Helpers; + +class Translator +{ + /** @var string[] */ + protected $locales; + + /** @var string */ + protected $locale; + + /** @var callable */ + protected $localeChangeCallback; + + /** + * Translator constructor. + * + * @param string $locale + * @param string[] $locales + * @param callable $localeChangeCallback + */ + public function __construct(string $locale, array $locales = [], callable $localeChangeCallback = null) + { + $this->localeChangeCallback = $localeChangeCallback; + + $this->setLocale($locale); + $this->setLocales($locales); + } + + /** + * Get the translation for a given key + * + * @param string $key + * @param array $replace + * @return string + */ + public function translate(string $key, array $replace = []): string + { + $translated = $this->translateGettext($key); + + if (!empty($replace)) { + $translated = call_user_func_array('sprintf', array_merge([$translated], $replace)); + } + + return $translated; + } + + /** + * Translate the key via gettext + * + * @param string $key + * @return string + * @codeCoverageIgnore + */ + protected function translateGettext(string $key): string + { + return _($key); + } + + /** + * @return string + */ + public function getLocale(): string + { + return $this->locale; + } + + /** + * @param string $locale + */ + public function setLocale(string $locale) + { + $this->locale = $locale; + + if (is_callable($this->localeChangeCallback)) { + call_user_func_array($this->localeChangeCallback, [$locale]); + } + } + + /** + * @return string[] + */ + public function getLocales(): array + { + return $this->locales; + } + + /** + * @param string $locale + * @return bool + */ + public function hasLocale(string $locale): bool + { + return isset($this->locales[$locale]); + } + + /** + * @param string[] $locales + */ + public function setLocales(array $locales) + { + $this->locales = $locales; + } +} diff --git a/src/Http/Response.php b/src/Http/Response.php index 9db6fa83..d79ab98b 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -2,6 +2,7 @@ namespace Engelsystem\Http; +use Engelsystem\Renderer\Renderer; use Psr\Http\Message\ResponseInterface; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; @@ -9,6 +10,25 @@ class Response extends SymfonyResponse implements ResponseInterface { use MessageTrait; + /** @var Renderer */ + protected $view; + + /** + * @param string $content + * @param int $status + * @param array $headers + * @param Renderer $view + */ + public function __construct( + $content = '', + int $status = 200, + array $headers = array(), + Renderer $view = null + ) { + $this->view = $view; + parent::__construct($content, $status, $headers); + } + /** * Return an instance with the specified status code and, optionally, reason phrase. * @@ -72,4 +92,25 @@ class Response extends SymfonyResponse implements ResponseInterface return $new; } + + /** + * Return an instance with the rendered content. + * + * THis method retains the immutability of the message and returns + * an instance with the updated status and headers + * + * @param string $view + * @param array $data + * @param int $status + * @param string[]|string[][] $headers + * @return Response + */ + public function withView($view, $data = [], $status = 200, $headers = []) + { + if (!$this->view instanceof Renderer) { + throw new \InvalidArgumentException('Renderer not defined'); + } + + return $this->create($this->view->render($view, $data), $status, $headers); + } } diff --git a/src/Http/SessionServiceProvider.php b/src/Http/SessionServiceProvider.php index 55e3f48b..59121a3b 100644 --- a/src/Http/SessionServiceProvider.php +++ b/src/Http/SessionServiceProvider.php @@ -17,6 +17,7 @@ class SessionServiceProvider extends ServiceProvider $this->app->bind(SessionStorageInterface::class, 'session.storage'); $session = $this->app->make(Session::class); + $this->app->instance(Session::class, $session); $this->app->instance('session', $session); /** @var Request $request */ diff --git a/src/Http/UrlGeneratorServiceProvider.php b/src/Http/UrlGeneratorServiceProvider.php index 37304076..9b9988aa 100644 --- a/src/Http/UrlGeneratorServiceProvider.php +++ b/src/Http/UrlGeneratorServiceProvider.php @@ -9,6 +9,7 @@ class UrlGeneratorServiceProvider extends ServiceProvider public function register() { $urlGenerator = $this->app->make(UrlGenerator::class); + $this->app->instance(UrlGenerator::class, $urlGenerator); $this->app->instance('http.urlGenerator', $urlGenerator); } } diff --git a/src/Middleware/ErrorHandler.php b/src/Middleware/ErrorHandler.php new file mode 100644 index 00000000..a7c4cfe6 --- /dev/null +++ b/src/Middleware/ErrorHandler.php @@ -0,0 +1,80 @@ +<?php + +namespace Engelsystem\Middleware; + +use Engelsystem\Http\Response; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\MiddlewareInterface; +use Psr\Http\Server\RequestHandlerInterface; +use Twig_LoaderInterface as TwigLoader; + +class ErrorHandler implements MiddlewareInterface +{ + /** @var TwigLoader */ + protected $loader; + + /** @var string */ + protected $viewPrefix = 'errors/'; + + /** + * @param TwigLoader $loader + */ + public function __construct(TwigLoader $loader) + { + $this->loader = $loader; + } + + /** + * Handles any error messages + * + * Should be added at the beginning + * + * @param ServerRequestInterface $request + * @param RequestHandlerInterface $handler + * @return ResponseInterface + */ + public function process( + ServerRequestInterface $request, + RequestHandlerInterface $handler + ): ResponseInterface { + $response = $handler->handle($request); + + $statusCode = $response->getStatusCode(); + if ($statusCode < 400 || !$response instanceof Response) { + return $response; + } + + $view = $this->selectView($statusCode); + + return $response->withView( + $this->viewPrefix . $view, + [ + 'status' => $statusCode, + 'content' => $response->getContent(), + ], + $statusCode, + $response->getHeaders() + ); + } + + /** + * Select a view based on the given status code + * + * @param int $statusCode + * @return string + */ + protected function selectView(int $statusCode): string + { + $hundreds = intdiv($statusCode, 100); + + $viewsList = [$statusCode, $hundreds, $hundreds * 100]; + foreach ($viewsList as $view) { + if ($this->loader->exists($this->viewPrefix . $view)) { + return $view; + } + } + + return 'default'; + } +} diff --git a/src/Middleware/LegacyMiddleware.php b/src/Middleware/LegacyMiddleware.php index 276fb3ee..78132815 100644 --- a/src/Middleware/LegacyMiddleware.php +++ b/src/Middleware/LegacyMiddleware.php @@ -83,7 +83,7 @@ class LegacyMiddleware implements MiddlewareInterface } if (empty($title) and empty($content)) { - $page = '404'; + $page = 404; $title = _('Page not found'); $content = _('This page could not be found or you don\'t have permission to view it. You probably have to sign in or register in order to gain access!'); } @@ -277,29 +277,17 @@ class LegacyMiddleware implements MiddlewareInterface $parameters['meetings'] = 1; } - $status = 200; - if ($page == '404') { - $status = 404; - $content = info($content, true); + if (!empty($page) && is_int($page)) { + return response($content, (int)$page); } - return response(view(__DIR__ . '/../../templates/layout.html', [ - 'theme' => isset($user) ? $user['color'] : config('theme'), + return response(view('layouts/app', [ 'title' => $title, - 'atom_link' => ($page == 'news' || $page == 'user_meetings') - ? ' <link href="' - . page_link_to('atom', $parameters) - . '" type = "application/atom+xml" rel = "alternate" title = "Atom Feed">' - : '', - 'start_page_url' => page_link_to('/'), - 'credits_url' => page_link_to('credits'), + 'atom_feed' => ($page == 'news' || $page == 'user_meetings') ? $parameters : [], 'menu' => make_menu(), 'content' => msg() . $content, 'header_toolbar' => header_toolbar(), - 'faq_url' => config('faq_url'), - 'contact_email' => config('contact_email'), - 'locale' => locale(), 'event_info' => EventConfig_info($event_config) . ' <br />' - ]), $status); + ]), 200); } } diff --git a/src/Middleware/SetLocale.php b/src/Middleware/SetLocale.php new file mode 100644 index 00000000..86fa0b7f --- /dev/null +++ b/src/Middleware/SetLocale.php @@ -0,0 +1,49 @@ +<?php + +namespace Engelsystem\Middleware; + +use Engelsystem\Helpers\Translator; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\MiddlewareInterface; +use Psr\Http\Server\RequestHandlerInterface; +use Symfony\Component\HttpFoundation\Session\Session; + +class SetLocale implements MiddlewareInterface +{ + /** @var Translator */ + protected $translator; + + /** @var Session */ + protected $session; + + /** + * @param Translator $translator + * @param Session $session + */ + public function __construct(Translator $translator, Session $session) + { + $this->translator = $translator; + $this->session = $session; + } + + /** + * Process an incoming server request and setting the locale if required + * + * @param ServerRequestInterface $request + * @param RequestHandlerInterface $handler + * @return ResponseInterface + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + $query = $request->getQueryParams(); + if (isset($query['set-locale']) && $this->translator->hasLocale($query['set-locale'])) { + $locale = $query['set-locale']; + + $this->translator->setLocale($locale); + $this->session->set('locale', $locale); + } + + return $handler->handle($request); + } +} diff --git a/src/Renderer/RendererServiceProvider.php b/src/Renderer/RendererServiceProvider.php index 3e8d69bc..2e41837b 100644 --- a/src/Renderer/RendererServiceProvider.php +++ b/src/Renderer/RendererServiceProvider.php @@ -24,12 +24,14 @@ class RendererServiceProvider extends ServiceProvider protected function registerRenderer() { $renderer = $this->app->make(Renderer::class); + $this->app->instance(Renderer::class, $renderer); $this->app->instance('renderer', $renderer); } protected function registerHtmlEngine() { $htmlEngine = $this->app->make(HtmlEngine::class); + $this->app->instance(HtmlEngine::class, $htmlEngine); $this->app->instance('renderer.htmlEngine', $htmlEngine); $this->app->tag('renderer.htmlEngine', ['renderer.engine']); } diff --git a/src/Renderer/Twig/Extensions/Config.php b/src/Renderer/Twig/Extensions/Config.php new file mode 100644 index 00000000..dbbe93e7 --- /dev/null +++ b/src/Renderer/Twig/Extensions/Config.php @@ -0,0 +1,31 @@ +<?php + +namespace Engelsystem\Renderer\Twig\Extensions; + +use Engelsystem\Config\Config as EngelsystemConfig; +use Twig_Extension as TwigExtension; +use Twig_Function as TwigFunction; + +class Config extends TwigExtension +{ + /** @var EngelsystemConfig */ + protected $config; + + /** + * @param EngelsystemConfig $config + */ + public function __construct(EngelsystemConfig $config) + { + $this->config = $config; + } + + /** + * @return TwigFunction[] + */ + public function getFunctions() + { + return [ + new TwigFunction('config', [$this->config, 'get']), + ]; + } +} diff --git a/src/Renderer/Twig/Extensions/Globals.php b/src/Renderer/Twig/Extensions/Globals.php new file mode 100644 index 00000000..f9bffbc8 --- /dev/null +++ b/src/Renderer/Twig/Extensions/Globals.php @@ -0,0 +1,23 @@ +<?php + +namespace Engelsystem\Renderer\Twig\Extensions; + +use Twig_Extension as TwigExtension; +use Twig_Extension_GlobalsInterface as GlobalsInterface; + +class Globals extends TwigExtension implements GlobalsInterface +{ + /** + * Returns a list of global variables to add to the existing list. + * + * @return array An array of global variables + */ + public function getGlobals() + { + global $user; + + return [ + 'user' => isset($user) ? $user : [], + ]; + } +} diff --git a/src/Renderer/Twig/Extensions/Session.php b/src/Renderer/Twig/Extensions/Session.php new file mode 100644 index 00000000..4690f701 --- /dev/null +++ b/src/Renderer/Twig/Extensions/Session.php @@ -0,0 +1,31 @@ +<?php + +namespace Engelsystem\Renderer\Twig\Extensions; + +use Symfony\Component\HttpFoundation\Session\Session as SymfonySession; +use Twig_Extension as TwigExtension; +use Twig_Function as TwigFunction; + +class Session extends TwigExtension +{ + /** @var SymfonySession */ + protected $session; + + /** + * @param SymfonySession $session + */ + public function __construct(SymfonySession $session) + { + $this->session = $session; + } + + /** + * @return TwigFunction[] + */ + public function getFunctions() + { + return [ + new TwigFunction('session_get', [$this->session, 'get']), + ]; + } +} diff --git a/src/Renderer/Twig/Extensions/Translation.php b/src/Renderer/Twig/Extensions/Translation.php new file mode 100644 index 00000000..63f9800e --- /dev/null +++ b/src/Renderer/Twig/Extensions/Translation.php @@ -0,0 +1,57 @@ +<?php + +namespace Engelsystem\Renderer\Twig\Extensions; + +use Engelsystem\Helpers\Translator; +use Twig_Extension as TwigExtension; +use Twig_Extensions_TokenParser_Trans as TranslationTokenParser; +use Twig_Filter as TwigFilter; +use Twig_Function as TwigFunction; +use Twig_TokenParserInterface as TwigTokenParser; + +class Translation extends TwigExtension +{ + /** @var Translator */ + protected $translator; + + /** @var TranslationTokenParser */ + protected $tokenParser; + + /** + * @param Translator $translator + * @param TranslationTokenParser $tokenParser + */ + public function __construct(Translator $translator, TranslationTokenParser $tokenParser) + { + $this->translator = $translator; + $this->tokenParser = $tokenParser; + } + + /** + * @return array + */ + public function getFilters() + { + return [ + new TwigFilter('trans', [$this->translator, 'translate']), + ]; + } + + /** + * @return TwigFunction[] + */ + public function getFunctions() + { + return [ + new TwigFunction('__', [$this->translator, 'translate']), + ]; + } + + /** + * @return TwigTokenParser[] + */ + public function getTokenParsers() + { + return [$this->tokenParser]; + } +} diff --git a/src/Renderer/Twig/Extensions/Url.php b/src/Renderer/Twig/Extensions/Url.php new file mode 100644 index 00000000..62e59782 --- /dev/null +++ b/src/Renderer/Twig/Extensions/Url.php @@ -0,0 +1,43 @@ +<?php + +namespace Engelsystem\Renderer\Twig\Extensions; + +use Engelsystem\Http\UrlGenerator; +use Twig_Extension as TwigExtension; +use Twig_Function as TwigFunction; + +class Url extends TwigExtension +{ + /** @var UrlGenerator */ + protected $urlGenerator; + + /** + * @param UrlGenerator $urlGenerator + */ + public function __construct(UrlGenerator $urlGenerator) + { + $this->urlGenerator = $urlGenerator; + } + + /** + * @return TwigFunction[] + */ + public function getFunctions() + { + return [ + new TwigFunction('url', [$this, 'getUrl']), + ]; + } + + /** + * @param string $path + * @param array $parameters + * @return UrlGenerator|string + */ + public function getUrl($path, $parameters = []) + { + $path = str_replace('_', '-', $path); + + return $this->urlGenerator->to($path, $parameters); + } +} diff --git a/src/Renderer/TwigEngine.php b/src/Renderer/TwigEngine.php new file mode 100644 index 00000000..55a2e299 --- /dev/null +++ b/src/Renderer/TwigEngine.php @@ -0,0 +1,41 @@ +<?php + +namespace Engelsystem\Renderer; + +use Twig_Environment as Twig; +use Twig_Error_Loader as LoaderError; +use Twig_Error_Runtime as RuntimeError; +use Twig_Error_Syntax as SyntaxError; + +class TwigEngine implements EngineInterface +{ + /** @var Twig */ + protected $twig; + + public function __construct(Twig $twig) + { + $this->twig = $twig; + } + + /** + * Render a twig template + * + * @param string $path + * @param array $data + * @return string + * @throws LoaderError|RuntimeError|SyntaxError + */ + public function get($path, $data = []) + { + return $this->twig->render($path, $data); + } + + /** + * @param string $path + * @return bool + */ + public function canRender($path) + { + return $this->twig->getLoader()->exists($path); + } +} diff --git a/src/Renderer/TwigLoader.php b/src/Renderer/TwigLoader.php new file mode 100644 index 00000000..154e6dbb --- /dev/null +++ b/src/Renderer/TwigLoader.php @@ -0,0 +1,26 @@ +<?php + +namespace Engelsystem\Renderer; + +use Twig_Error_Loader; +use Twig_Loader_Filesystem as FilesystemLoader; + +class TwigLoader extends FilesystemLoader +{ + /** + * @param string $name + * @param bool $throw + * @return false|string + * @throws Twig_Error_Loader + */ + public function findTemplate($name, $throw = true) + { + $extension = '.twig'; + $extensionLength = strlen($extension); + if (substr($name, -$extensionLength, $extensionLength) !== $extension) { + $name .= $extension; + } + + return parent::findTemplate($name, $throw); + } +} diff --git a/src/Renderer/TwigServiceProvider.php b/src/Renderer/TwigServiceProvider.php new file mode 100644 index 00000000..0f453989 --- /dev/null +++ b/src/Renderer/TwigServiceProvider.php @@ -0,0 +1,77 @@ +<?php + +namespace Engelsystem\Renderer; + +use Engelsystem\Container\ServiceProvider; +use Engelsystem\Renderer\Twig\Extensions\Config; +use Engelsystem\Renderer\Twig\Extensions\Globals; +use Engelsystem\Renderer\Twig\Extensions\Session; +use Engelsystem\Renderer\Twig\Extensions\Translation; +use Engelsystem\Renderer\Twig\Extensions\Url; +use Twig_Environment as Twig; +use Twig_LoaderInterface as TwigLoaderInterface; + +class TwigServiceProvider extends ServiceProvider +{ + /** @var array */ + protected $extensions = [ + 'config' => Config::class, + 'globals' => Globals::class, + 'session' => Session::class, + 'url' => Url::class, + 'translation' => Translation::class, + ]; + + public function register() + { + $this->registerTwigEngine(); + + foreach ($this->extensions as $alias => $class) { + $this->registerTwigExtensions($class, $alias); + } + } + + public function boot() + { + /** @var Twig $renderer */ + $renderer = $this->app->get('twig.environment'); + + foreach ($this->app->tagged('twig.extension') as $extension) { + $renderer->addExtension($extension); + } + } + + protected function registerTwigEngine() + { + $viewsPath = $this->app->get('path.views'); + + $twigLoader = $this->app->make(TwigLoader::class, ['paths' => $viewsPath]); + $this->app->instance(TwigLoader::class, $twigLoader); + $this->app->instance(TwigLoaderInterface::class, $twigLoader); + $this->app->instance('twig.loader', $twigLoader); + + $twig = $this->app->make(Twig::class); + $this->app->instance(Twig::class, $twig); + $this->app->instance('twig.environment', $twig); + + $twigEngine = $this->app->make(TwigEngine::class); + $this->app->instance('renderer.twigEngine', $twigEngine); + $this->app->tag('renderer.twigEngine', ['renderer.engine']); + } + + /** + * @param string $class + * @param string $alias + */ + protected function registerTwigExtensions($class, $alias) + { + $alias = 'twig.extension.' . $alias; + + $extension = $this->app->make($class); + + $this->app->instance($class, $extension); + $this->app->instance($alias, $extension); + + $this->app->tag($alias, ['twig.extension']); + } +} diff --git a/src/helpers.php b/src/helpers.php index 95571a40..84f26dfa 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -3,10 +3,11 @@ use Engelsystem\Application; use Engelsystem\Config\Config; +use Engelsystem\Helpers\Translator; use Engelsystem\Http\Request; use Engelsystem\Http\Response; -use Engelsystem\Renderer\Renderer; use Engelsystem\Http\UrlGenerator; +use Engelsystem\Renderer\Renderer; use Symfony\Component\HttpFoundation\Session\SessionInterface; /** @@ -119,6 +120,40 @@ function session($key = null, $default = null) } /** + * Translate the given message + * + * @param string $key + * @param array $replace + * @return string|Translator + */ +function trans($key = null, $replace = []) +{ + /** @var Translator $translator */ + $translator = app('translator'); + + if (is_null($key)) { + return $translator; + } + + return $translator->translate($key, $replace); +} + +/** + * Translate the given message + * + * @param string $key + * @param array $replace + * @return string + */ +function __($key, $replace = []) +{ + /** @var Translator $translator */ + $translator = app('translator'); + + return $translator->translate($key, $replace); +} + +/** * @param string $path * @param array $parameters * @return UrlGeneratorInterface|string @@ -139,7 +174,7 @@ function url($path = null, $parameters = []) * @param mixed[] $data * @return Renderer|string */ -function view($template = null, $data = null) +function view($template = null, $data = []) { $renderer = app('renderer'); |