mirror of
https://github.com/Mibew/mibew.git
synced 2025-02-07 16:24:43 +03:00
Move common helpers to a separate composer package
This commit is contained in:
parent
f5ee6448e4
commit
a441bed2a2
@ -19,6 +19,7 @@
|
||||
},
|
||||
"require": {
|
||||
"xamin/handlebars.php": "0.10.*",
|
||||
"justblackbird/handlebars.php-helpers": "~0.2.1",
|
||||
"symfony/http-foundation": "2.6.*",
|
||||
"symfony/routing": "2.6.*",
|
||||
"symfony/config": "2.6.*",
|
||||
|
@ -1,77 +0,0 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* A storage for overridable blocks' content.
|
||||
*/
|
||||
class BlockStorage
|
||||
{
|
||||
/**
|
||||
* Associative array of blocks. The keys are blocks names and the values are
|
||||
* blocks content.
|
||||
*
|
||||
* @type string[]
|
||||
*/
|
||||
protected $blocks = array();
|
||||
|
||||
/**
|
||||
* Gets content of a block.
|
||||
*
|
||||
* @param string $name Block's name.
|
||||
* @return string Block's content.
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
return isset($this->blocks[$name]) ? $this->blocks[$name] : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets content of a block.
|
||||
*
|
||||
* @param string $name Block's name.
|
||||
* @param string $content Block's content.
|
||||
*/
|
||||
public function set($name, $content)
|
||||
{
|
||||
$this->blocks[$name] = $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a block exists in the storage.
|
||||
*
|
||||
* @param string $name Block's name
|
||||
* @return boolean
|
||||
*/
|
||||
public function has($name)
|
||||
{
|
||||
return isset($this->blocks[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes block from the storage.
|
||||
*
|
||||
* @param string $name Block's name.
|
||||
*/
|
||||
public function remove($name)
|
||||
{
|
||||
unset($this->blocks[$name]);
|
||||
}
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
<?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 Mibew\Handlebars\BlockStorage;
|
||||
|
||||
/**
|
||||
* Contains base functionality for all helpers related with blocks overriding.
|
||||
*/
|
||||
abstract class AbstractBlockHelper
|
||||
{
|
||||
/**
|
||||
* @var BlockStorage
|
||||
*/
|
||||
protected $blocksStorage;
|
||||
|
||||
/**
|
||||
* Helper's constructor.
|
||||
*
|
||||
* @param BlockStorage $storage A Blocks context instance
|
||||
*/
|
||||
public function __construct(BlockStorage $storage)
|
||||
{
|
||||
$this->blocksStorage = $storage;
|
||||
}
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* A helper for defining default content of a block.
|
||||
*
|
||||
* Example of usage:
|
||||
* <code>
|
||||
* {{#block "blockName"}}
|
||||
* Default content for the block
|
||||
* {{/block}}
|
||||
* </code>
|
||||
*/
|
||||
class BlockHelper extends AbstractBlockHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
{
|
||||
// Get block name
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"block" helper expects exactly one argument.'
|
||||
);
|
||||
}
|
||||
$block_name = $context->get(array_shift($parsed_args));
|
||||
|
||||
// If the block is not overridden render and show the default value
|
||||
if (!$this->blocksStorage->has($block_name)) {
|
||||
return $template->render($context);
|
||||
}
|
||||
|
||||
// Show overridden content
|
||||
return $this->blocksStorage->get($block_name);
|
||||
}
|
||||
}
|
@ -1,53 +0,0 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* Cut string if it exceeds specified length.
|
||||
*
|
||||
* Example of usage:
|
||||
* <code>
|
||||
* {{cutString string length}}
|
||||
* </code>
|
||||
*/
|
||||
class CutStringHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 2) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"cutString" helper expects exactly two arguments.'
|
||||
);
|
||||
}
|
||||
|
||||
$string = $context->get($parsed_args[0]);
|
||||
$length = intval($context->get($parsed_args[1]));
|
||||
|
||||
return substr($string, 0, $length);
|
||||
}
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* A helper for templates inheritance.
|
||||
*
|
||||
* Example of usage:
|
||||
* <code>
|
||||
* {{#extends "parentTemplateName"}}
|
||||
* {{#override "blockName"}}
|
||||
* Overridden first block
|
||||
* {{/override}}
|
||||
*
|
||||
* {{#override "anotherBlockName"}}
|
||||
* Overridden second block
|
||||
* {{/override}}
|
||||
* {{/extends}}
|
||||
* </code>
|
||||
*/
|
||||
class ExtendsHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
{
|
||||
// Get name of the parent template
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"extends" helper expects exactly one argument.'
|
||||
);
|
||||
}
|
||||
$parent_template = $context->get(array_shift($parsed_args));
|
||||
|
||||
// Render content inside "extends" block to override blocks
|
||||
$template->render($context);
|
||||
|
||||
// We need another instance of \Handlebars\Template to render parent
|
||||
// template. It can be got from Handlebars engine, so get the engine.
|
||||
$handlebars = $template->getEngine();
|
||||
|
||||
// Render the parent template
|
||||
return $handlebars->render($parent_template, $context);
|
||||
}
|
||||
}
|
@ -1,84 +0,0 @@
|
||||
<?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\String;
|
||||
use Handlebars\Template;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if at least one argumet can be treated as
|
||||
* "true" value.
|
||||
*
|
||||
* Example of usage:
|
||||
* <code>
|
||||
* {{#ifAny first second third}}
|
||||
* At least one of argument can be threated as "true".
|
||||
* {{else}}
|
||||
* All values are "falsy"
|
||||
* {{/ifAny}}
|
||||
* </code>
|
||||
*/
|
||||
class IfAnyHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) == 0) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifAny" helper expects at least one argument.'
|
||||
);
|
||||
}
|
||||
|
||||
$condition = false;
|
||||
foreach ($parsed_args as $parsed_arg) {
|
||||
$value = $context->get($parsed_arg);
|
||||
|
||||
if ($value instanceof String) {
|
||||
// We need to get internal string. Casting any object of
|
||||
// \Handlebars\String will have positive result even for those
|
||||
// with empty internal strings.
|
||||
$value = $value->getString();
|
||||
}
|
||||
|
||||
if ($value) {
|
||||
$condition = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($condition) {
|
||||
$template->setStopToken('else');
|
||||
$buffer = $template->render($context);
|
||||
$template->setStopToken(false);
|
||||
} else {
|
||||
$template->setStopToken('else');
|
||||
$template->discard();
|
||||
$template->setStopToken(false);
|
||||
$buffer = $template->render($context);
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if two values are equal or not.
|
||||
*
|
||||
* Example of usage:
|
||||
* <code>
|
||||
* {{#ifEqual first second}}
|
||||
* The first argument is equal to the second one.
|
||||
* {{else}}
|
||||
* The arguments are not equal.
|
||||
* {{/ifEqual}}
|
||||
* </code>
|
||||
*/
|
||||
class IfEqualHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 2) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifEqual" helper expects exactly two arguments.'
|
||||
);
|
||||
}
|
||||
|
||||
$condition = ($context->get($parsed_args[0]) == $context->get($parsed_args[1]));
|
||||
|
||||
if ($condition) {
|
||||
$template->setStopToken('else');
|
||||
$buffer = $template->render($context);
|
||||
$template->setStopToken(false);
|
||||
} else {
|
||||
$template->setStopToken('else');
|
||||
$template->discard();
|
||||
$template->setStopToken(false);
|
||||
$buffer = $template->render($context);
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if specified argument is even or not.
|
||||
*
|
||||
* Example of usage:
|
||||
* <code>
|
||||
* {{#ifEven value}}
|
||||
* The value is even.
|
||||
* {{else}}
|
||||
* The value is odd.
|
||||
* {{/ifEven}}
|
||||
* </code>
|
||||
*/
|
||||
class IfEvenHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifEven" helper expects exactly one argument.'
|
||||
);
|
||||
}
|
||||
|
||||
$condition = ($context->get($parsed_args[0]) % 2 == 0);
|
||||
|
||||
if ($condition) {
|
||||
$template->setStopToken('else');
|
||||
$buffer = $template->render($context);
|
||||
$template->setStopToken(false);
|
||||
} else {
|
||||
$template->setStopToken('else');
|
||||
$template->discard();
|
||||
$template->setStopToken(false);
|
||||
$buffer = $template->render($context);
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if specified argument is odd or not.
|
||||
*
|
||||
* Example of usage:
|
||||
* <code>
|
||||
* {{#ifOdd value}}
|
||||
* The value is odd.
|
||||
* {{else}}
|
||||
* The value is even.
|
||||
* {{/ifOdd}}
|
||||
* </code>
|
||||
*/
|
||||
class IfOddHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifOdd" helper expects exactly one argument.'
|
||||
);
|
||||
}
|
||||
|
||||
$condition = ($context->get($parsed_args[0]) % 2 == 1);
|
||||
|
||||
if ($condition) {
|
||||
$template->setStopToken('else');
|
||||
$buffer = $template->render($context);
|
||||
$template->setStopToken(false);
|
||||
} else {
|
||||
$template->setStopToken('else');
|
||||
$template->discard();
|
||||
$template->setStopToken(false);
|
||||
$buffer = $template->render($context);
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if block overridden or not.
|
||||
*
|
||||
* Example of usage:
|
||||
* <code>
|
||||
* {{#ifOverridden "blockName"}}
|
||||
* The block was overridden
|
||||
* {{else}}
|
||||
* The block was not overridden
|
||||
* {{/ifOverridden}}
|
||||
* </code>
|
||||
*/
|
||||
class IfOverriddenHelper extends AbstractBlockHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
{
|
||||
// Get block name
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"ifOverridden" helper expects exactly one argument.'
|
||||
);
|
||||
}
|
||||
$block_name = $context->get(array_shift($parsed_args));
|
||||
|
||||
// Check condition and render blocks
|
||||
if ($this->blocksStorage->has($block_name)) {
|
||||
$template->setStopToken('else');
|
||||
$buffer = $template->render($context);
|
||||
$template->setStopToken(false);
|
||||
} else {
|
||||
$template->setStopToken('else');
|
||||
$template->discard();
|
||||
$template->setStopToken(false);
|
||||
$buffer = $template->render($context);
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
}
|
@ -1,66 +0,0 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* A helper for overriding content of a block.
|
||||
*
|
||||
* Example of usage:
|
||||
* <code>
|
||||
* {{#extends "parentTemplateName"}}
|
||||
* {{#override "blockName"}}
|
||||
* Overridden first block
|
||||
* {{/override}}
|
||||
*
|
||||
* {{#override "anotherBlockName"}}
|
||||
* Overridden second block
|
||||
* {{/override}}
|
||||
* {{/extends}}
|
||||
* </code>
|
||||
*/
|
||||
class OverrideHelper extends AbstractBlockHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
{
|
||||
// Get block name
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"override" helper expects exactly one argument.'
|
||||
);
|
||||
}
|
||||
$block_name = $context->get(array_shift($parsed_args));
|
||||
|
||||
// We need to provide unlimited inheritence level. Rendering is started
|
||||
// from the deepest level template. If the content is in the block
|
||||
// storage it is related with the deepest level template. Thus we do not
|
||||
// need to override it.
|
||||
if (!$this->blocksStorage->has($block_name)) {
|
||||
$this->blocksStorage->set($block_name, $template->render($context));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,53 +0,0 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* Helper for repeating content.
|
||||
*
|
||||
* Example of usage:
|
||||
* <code>
|
||||
* {{#repeat times}}content to repeat{{/repeat}}
|
||||
* </code>
|
||||
*/
|
||||
class RepeatHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"repeat" helper expects exactly one argument.'
|
||||
);
|
||||
}
|
||||
|
||||
$times = intval($context->get($parsed_args[0]));
|
||||
$string = $template->render($context);
|
||||
|
||||
return str_repeat($string, $times);
|
||||
}
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* Helper for replacing substrings.
|
||||
*
|
||||
* Example of usage:
|
||||
* <code>
|
||||
* {{#replace search replacement}}target content{{/replace}}
|
||||
* </code>
|
||||
*/
|
||||
class ReplaceHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
{
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 2) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"replace" helper expects exactly two arguments.'
|
||||
);
|
||||
}
|
||||
|
||||
$search = $context->get($parsed_args[0]);
|
||||
$replacement = $context->get($parsed_args[1]);
|
||||
$subject = $template->render($context);
|
||||
|
||||
return str_replace($search, $replacement, $subject);
|
||||
}
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* Conditional helper that checks if block overridden or not.
|
||||
*
|
||||
* Example of usage:
|
||||
* <code>
|
||||
* {{#unlessOverridden "blockName"}}
|
||||
* The block was not overridden
|
||||
* {{else}}
|
||||
* The block was overridden
|
||||
* {{/unlessOverridden}}
|
||||
* </code>
|
||||
*/
|
||||
class UnlessOverriddenHelper extends AbstractBlockHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function execute(Template $template, Context $context, $args, $source)
|
||||
{
|
||||
// Get block name
|
||||
$parsed_args = $template->parseArguments($args);
|
||||
if (count($parsed_args) != 1) {
|
||||
throw new \InvalidArgumentException(
|
||||
'"unlessOverridden" helper expects exactly one argument.'
|
||||
);
|
||||
}
|
||||
$block_name = $context->get(array_shift($parsed_args));
|
||||
|
||||
// Check condition and render blocks
|
||||
if (!$this->blocksStorage->has($block_name)) {
|
||||
$template->setStopToken('else');
|
||||
$buffer = $template->render($context);
|
||||
$template->setStopToken(false);
|
||||
} else {
|
||||
$template->setStopToken('else');
|
||||
$template->discard();
|
||||
$template->setStopToken(false);
|
||||
$buffer = $template->render($context);
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
}
|
||||
}
|
@ -20,6 +20,7 @@
|
||||
namespace Mibew\Handlebars;
|
||||
|
||||
use Handlebars\Helpers as BaseHelpers;
|
||||
use JustBlackBird\HandlebarsHelpers;
|
||||
|
||||
/**
|
||||
* Handlebars helpers collection.
|
||||
@ -35,23 +36,25 @@ class Helpers extends BaseHelpers
|
||||
{
|
||||
parent::addDefaultHelpers();
|
||||
|
||||
$blocks = new BlockStorage();
|
||||
$this->add('l10n', new Helper\L10nHelper());
|
||||
$this->add('extends', new Helper\ExtendsHelper());
|
||||
$this->add('block', new Helper\BlockHelper($blocks));
|
||||
$this->add('override', new Helper\OverrideHelper($blocks));
|
||||
$this->add('ifOverridden', new Helper\IfOverriddenHelper($blocks));
|
||||
$this->add('unlessOverridden', new Helper\UnlessOverriddenHelper($blocks));
|
||||
$this->add('ifEqual', new Helper\IfEqualHelper());
|
||||
$this->add('ifAny', new Helper\IfAnyHelper());
|
||||
$this->add('ifEven', new Helper\IfEvenHelper());
|
||||
$this->add('ifOdd', new Helper\IfOddHelper());
|
||||
$this->add('generatePagination', new Helper\GeneratePaginationHelper());
|
||||
$this->add('repeat', new Helper\RepeatHelper());
|
||||
$this->add('replace', new Helper\ReplaceHelper());
|
||||
$this->add('formatDate', new Helper\FormatDateHelper());
|
||||
$this->add('formatDateDiff', new Helper\FormatDateDiffHelper());
|
||||
$this->add('cutString', new Helper\CutStringHelper());
|
||||
$this->add('csrfTokenInput', new Helper\CsrfTokenInputHelper());
|
||||
|
||||
// Use third party helpers
|
||||
$this->add('ifEqual', new HandlebarsHelpers\Comparison\IfEqualHelper());
|
||||
$this->add('ifAny', new HandlebarsHelpers\Comparison\IfAnyHelper());
|
||||
$this->add('ifEven', new HandlebarsHelpers\Comparison\IfEvenHelper());
|
||||
$this->add('ifOdd', new HandlebarsHelpers\Comparison\IfOddHelper());
|
||||
$this->add('repeat', new HandlebarsHelpers\String\RepeatHelper());
|
||||
$this->add('replace', new HandlebarsHelpers\String\ReplaceHelper());
|
||||
$this->add('cutString', new HandlebarsHelpers\String\TruncateHelper());
|
||||
$blocks = new HandlebarsHelpers\Layout\BlockStorage();
|
||||
$this->add('extends', new HandlebarsHelpers\Layout\ExtendsHelper($blocks));
|
||||
$this->add('block', new HandlebarsHelpers\Layout\BlockHelper($blocks));
|
||||
$this->add('override', new HandlebarsHelpers\Layout\OverrideHelper($blocks));
|
||||
$this->add('ifOverridden', new HandlebarsHelpers\Layout\IfOverriddenHelper($blocks));
|
||||
$this->add('unlessOverridden', new HandlebarsHelpers\Layout\UnlessOverriddenHelper($blocks));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user