mirror of
https://github.com/Mibew/mibew.git
synced 2025-01-31 13:24:41 +03:00
Create "route" Handlebars.php helper
This commit is contained in:
parent
eba2eadaca
commit
5ee217accb
@ -23,6 +23,8 @@ use Mibew\Asset\AssetUrlGeneratorAwareInterface;
|
||||
use Mibew\Asset\AssetUrlGeneratorInterface;
|
||||
use Mibew\Authentication\AuthenticationManagerAwareInterface;
|
||||
use Mibew\Authentication\AuthenticationManagerInterface;
|
||||
use Mibew\Handlebars\HandlebarsAwareInterface;
|
||||
use Mibew\Handlebars\Helper\RouteHelper;
|
||||
use Mibew\Routing\RouterAwareInterface;
|
||||
use Mibew\Routing\RouterInterface;
|
||||
use Mibew\Style\StyleInterface;
|
||||
@ -64,6 +66,13 @@ abstract class AbstractController implements
|
||||
public function setRouter(RouterInterface $router)
|
||||
{
|
||||
$this->router = $router;
|
||||
|
||||
// Update router in the style helpers
|
||||
if (!is_null($this->style) && $this->style instanceof HandlebarsAwareInterface) {
|
||||
if ($this->style->getHandlebars()->hasHelper('route')) {
|
||||
$this->style->getHandlebars()->getHelper('route')->setRouter($router);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -176,7 +185,7 @@ abstract class AbstractController implements
|
||||
protected function getStyle()
|
||||
{
|
||||
if (is_null($this->style)) {
|
||||
$this->style = new PageStyle(PageStyle::getDefaultStyle());
|
||||
$this->style = $this->prepareStyle(new PageStyle(PageStyle::getDefaultStyle()));
|
||||
}
|
||||
|
||||
return $this->style;
|
||||
@ -206,4 +215,26 @@ abstract class AbstractController implements
|
||||
return $this->getAssetUrlGenerator()
|
||||
->generate($relative_path, $reference_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares a style right after creation.
|
||||
*
|
||||
* One can use this method to add custom helpers to a template engine using
|
||||
* by the style.
|
||||
*
|
||||
* @param StyleInterface $style A style that should be prepared.
|
||||
* @return StyleInterface A ready to use style instance.
|
||||
*/
|
||||
protected function prepareStyle(StyleInterface $style)
|
||||
{
|
||||
if ($style instanceof HandlebarsAwareInterface) {
|
||||
// Add more helpers to template engine
|
||||
$style->getHandlebars()->addHelper(
|
||||
'route',
|
||||
new RouteHelper($this->getRouter())
|
||||
);
|
||||
}
|
||||
|
||||
return $style;
|
||||
}
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ abstract class AbstractController extends BaseAbstractController
|
||||
protected function getStyle()
|
||||
{
|
||||
if (is_null($this->style)) {
|
||||
$this->style = new ChatStyle(ChatStyle::getDefaultStyle());
|
||||
$this->style = $this->prepareStyle(new ChatStyle(ChatStyle::getDefaultStyle()));
|
||||
}
|
||||
|
||||
return $this->style;
|
||||
|
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is a part of Mibew Messenger.
|
||||
*
|
||||
* Copyright 2005-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace Mibew\Handlebars\Helper;
|
||||
|
||||
use Handlebars\Context;
|
||||
use Handlebars\Helper as HelperInterface;
|
||||
use Handlebars\Template;
|
||||
use Mibew\Routing\RouterAwareInterface;
|
||||
use Mibew\Routing\RouterInterface;
|
||||
|
||||
/**
|
||||
* A helper that generates url based on route name and parameters list.
|
||||
*
|
||||
* Example of usage:
|
||||
* <code>
|
||||
* {{route "hello" to="world"}}
|
||||
* </code>
|
||||
* The code above generates URL for route named "hello" and pass parameter
|
||||
* "to" equals to "world" to URL generator.
|
||||
*/
|
||||
class RouteHelper implements HelperInterface, RouterAwareInterface
|
||||
{
|
||||
/**
|
||||
* @var RouterInterface
|
||||
*/
|
||||
protected $router = null;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getRouter()
|
||||
{
|
||||
return $this->router;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setRouter(RouterInterface $router)
|
||||
{
|
||||
$this->router = $router;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper's constructor.
|
||||
*
|
||||
* @param RouterInterface $router A Router instance.
|
||||
*/
|
||||
public function __construct(RouterInterface $router)
|
||||
{
|
||||
$this->setRouter($router);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @todo Use combined arguments parser when it will be implemented in
|
||||
* Handlebars.php.
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
{
|
||||
$named_args = $template->parseNamedArguments($args);
|
||||
$positional_args = $template->parseArguments($args);
|
||||
$route_name = (string)$context->get($positional_args[0]);
|
||||
|
||||
$parameters = array();
|
||||
foreach ($named_args as $name => $parsed_arg) {
|
||||
$parameters[$name] = $context->get($parsed_arg);
|
||||
}
|
||||
|
||||
return $this->getRouter()->generate($route_name, $parameters);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user