Skip to content

Commit

Permalink
Merge pull request #13 from hummer2k/feature/actions-named-parameters
Browse files Browse the repository at this point in the history
added named parameters for block method calls
  • Loading branch information
hummer2k committed Oct 18, 2015
2 parents 6a6418d + c04628b commit 7d9d0e4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 45 deletions.
22 changes: 14 additions & 8 deletions src/ConLayout/Block/Factory/BlockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace ConLayout\Block\Factory;

use ConLayout\Block\BlockInterface;
use ConLayout\Exception\BadMethodCallException;
use ConLayout\Layout\LayoutInterface;
use ConLayout\NamedParametersTrait;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerAwareTrait;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
Expand All @@ -26,6 +28,7 @@ class BlockFactory implements

use ServiceLocatorAwareTrait;
use EventManagerAwareTrait;
use NamedParametersTrait;

/**
*
Expand Down Expand Up @@ -96,14 +99,17 @@ public function createBlock($blockId, array $specs)
foreach ($this->getOption('variables', $specs) as $name => $variable) {
$block->setVariable($name, $variable);
}
foreach ($this->getOption('actions', $specs) as $method => $params) {
$params = (array) $params;
if (is_callable([$block, $method])) {
call_user_func_array([$block, $method], $params);
} else {
$method = key($params);
if (is_callable([$block, $method])) {
call_user_func_array([$block, $method], (array) current($params));
foreach ($this->getOption('actions', $specs) as $params) {
if (isset($params['method'])) {
$method = (string) $params['method'];
if (method_exists($block, $method)) {
$this->invokeArgs($block, $method, $params);
} else {
throw new BadMethodCallException(sprintf(
'Call to undefined block method %s::%s()',
get_class($block),
$method
));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ConLayout/Exception/BadMethodCallException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @package ConLayout
* @author Cornelius Adams (conlabz GmbH) <cornelius.adams@conlabz.de>
*/
class BadMethodCallException extends \BadMethodCallException
class BadMethodCallException extends \BadMethodCallException implements ExceptionInterface
{

}
11 changes: 11 additions & 0 deletions src/ConLayout/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace ConLayout\Exception;

/**
* @package ConLayout
* @author Cornelius Adams (conlabz GmbH) <cornelius.adams@conlabz.de>
*/
interface ExceptionInterface
{
}
10 changes: 5 additions & 5 deletions src/ConLayout/NamedParametersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ trait NamedParametersTrait
*
* @var array
*/
protected $reflectionCache = [];
protected static $reflectionCache = [];

/**
*
Expand All @@ -26,12 +26,12 @@ trait NamedParametersTrait
*/
protected function invokeArgs($instance, $method, array $args = [])
{
$hash = spl_object_hash($instance);
if (isset($this->reflectionCache[$hash][$method])) {
$reflection = $this->reflectionCache[$hash][$method];
$class = get_class($instance);
if (isset(self::$reflectionCache[$class][$method])) {
$reflection = self::$reflectionCache[$class][$method];
} else {
$reflection = new ReflectionMethod($instance, $method);
$this->reflectionCache[$hash][$method] = $reflection;
self::$reflectionCache[$class][$method] = $reflection;
}

$pass = [];
Expand Down
51 changes: 20 additions & 31 deletions tests/ConLayoutTest/Block/Factory/BlockFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ public function testCreateBlock()
'append' => false,
'capture_to' => 'sidebarLeft',
'actions' => [
'setOption' => [
'my-option', 'value_my-option'
'my-option' => [
'method' => 'setOption',
'name' => 'my-option',
'value' => 'value_my-option'
]
]
];
Expand All @@ -72,52 +74,39 @@ public function testCreateBlock()
$this->assertEquals('value_my-option', $block->getOptions()['my-option']);
}

public function testMultipleActions()
public function testWrapBlockString()
{
$factory = new BlockFactory();
$factory->setServiceLocator(new ServiceManager());

$specs = [
'actions' => [
'set_some_option' => [
'setOption' => ['my-option', 'value_my-option']
],
'set_another_option' => [
'setOption' => ['some-other-option', 'some-option-value']
],
'setVariable' => ['testVar', 'valueTestVar'],
'set_some_var' => [
'setVariable' => ['testVar2', 'SOME_VAR']
],
'set_another_var' => [
'setVariable' => ['testVar3', 'ANOTHER_VAR']
]
]
'template' => 'my/tpl',
'wrapper' => 'my/wrapper'
];

$block = $factory->createBlock('block.id', $specs);
$block = $factory->createBlock('my-block', $specs);

$this->assertEquals('my/wrapper', $block->getTemplate());

$this->assertEquals('value_my-option', $block->getOption('my-option'));
$this->assertEquals('some-option-value', $block->getOption('some-other-option'));
$this->assertEquals('valueTestVar', $block->getVariable('testVar'));
$this->assertEquals('SOME_VAR', $block->getVariable('testVar2'));
$this->assertEquals('ANOTHER_VAR', $block->getVariable('testVar3'));
}

public function testWrapBlockString()
/**
* @expectedException ConLayout\Exception\BadMethodCallException
*/
public function testThrowsExceptionOnMissingMethod()
{
$factory = new BlockFactory();
$factory->setServiceLocator(new ServiceManager());

$specs = [
'template' => 'my/tpl',
'wrapper' => 'my/wrapper'
'actions' => [
'not-exists' => [
'method' => 'notExists___'
]
]
];

$block = $factory->createBlock('my-block', $specs);

$this->assertEquals('my/wrapper', $block->getTemplate());

$factory->createBlock('my-block', $specs);
}

public function testWrapBlockArray()
Expand Down

0 comments on commit 7d9d0e4

Please sign in to comment.