Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzap committed Sep 15, 2016
2 parents af0b0d8 + 7fd9a61 commit 780f72c
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 44 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,13 @@ that, as you might have guessed, is used to add providers to the collector.
In short, when `provide()` method is called, it returns the context from all the providers that
were added to it.

Tha package ships with a single implementation `WpTemplateContextCollector` that uses `array_merge`
to build a context array from the arrays "provided" by the added providers that "accept" the given query.
Tha package ships with a two implementations:

- `ArrayMergeContextCollector`
- `ArrayMergeRecursiveContextCollector`

they are pretty identical, but first uses `array_merge`, the second `array_merge_recursive` to build
the context array from the arrays "provided" by the added providers that "accepts" the given query.

# Context Loader

Expand Down
4 changes: 2 additions & 2 deletions examples/hierachy-mustache/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
namespace Brain\Context\Examples\HierarchyMustache;

use Brain\Context\WpContextLoader;
use Brain\Context\WpTemplateContextCollector;
use Brain\Context\ArrayMergeContextCollector;
use Brain\Hierarchy\Finder\SubfolderTemplateFinder;
use Brain\Hierarchy\QueryTemplate;

// Let's add all our providers, the collector will pull context from them when the query fits.
add_action('brain.context.providers', function (WpTemplateContextCollector $collector) {
add_action('brain.context.providers', function (ArrayMergeContextCollector $collector) {

$collector
->addProvider(new Providers\HomePageContext())
Expand Down
4 changes: 2 additions & 2 deletions examples/simple/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
namespace Brain\Context\Examples\Simple;

use Brain\Context\WpContextLoader;
use Brain\Context\WpTemplateContextCollector;
use Brain\Context\ArrayMergeContextCollector;

// Let's add all our providers, the collector will pull context from them
// when the query fits.
add_action('brain.context.providers', function (WpTemplateContextCollector $collector) {
add_action('brain.context.providers', function (ArrayMergeContextCollector $collector) {

$collector
->addProvider(new HomePageContext())
Expand Down
47 changes: 47 additions & 0 deletions src/ArrayMergeContextCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php # -*- coding: utf-8 -*-
/*
* This file is part of the Context package.
*
* (c) Giuseppe Mazzapica
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Brain\Context;

/**
* @author Giuseppe Mazzapica <giuseppe.mazzapica@gmail.com>
* @package Context
* @license http://opensource.org/licenses/MIT MIT
*/
final class ArrayMergeContextCollector implements ContextCollectorInterface
{
use ArrayMergeContextCollectorTrait;

/**
* @return array
*/
public function provide()
{
if (!$this->query instanceof \WP_Query) {
return [];
}

$context = [];
while (!$this->providers->isEmpty()) {
/** @var ContextProviderInterface|UpdatableContextProviderInterface $provider */
$provider = $this->providers->dequeue();
if (!$provider->accept($this->query)) {
continue;
}

$context = array_merge($context, $provider->provide());
if ($provider instanceof UpdatableContextProviderInterface) {
$context = $provider->update($context);
}
}

return $context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @package Context
* @license http://opensource.org/licenses/MIT MIT
*/
final class WpTemplateContextCollector implements ContextCollectorInterface
trait ArrayMergeContextCollectorTrait
{

/**
Expand All @@ -39,6 +39,8 @@ public function __construct()
*/
public function addProvider(ContextProviderInterface $provider)
{
/** @var $this ContextCollectorInterface */

$this->providers->enqueue($provider);

// By using this function it is possible to remove the just-added provider
Expand All @@ -58,30 +60,4 @@ public function accept(\WP_Query $query)

return true;
}

/**
* @return array
*/
public function provide()
{
if (!$this->query instanceof \WP_Query) {
return [];
}

$context = [];
while (!$this->providers->isEmpty()) {
/** @var ContextProviderInterface|UpdatableContextProviderInterface $provider */
$provider = $this->providers->dequeue();
if (!$provider->accept($this->query)) {
continue;
}

$context = array_merge($context, $provider->provide());
if ($provider instanceof UpdatableContextProviderInterface) {
$context = $provider->update($context);
}
}

return $context;
}
}
48 changes: 48 additions & 0 deletions src/ArrayMergeRecursiveContextCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php # -*- coding: utf-8 -*-
/*
* This file is part of the Context package.
*
* (c) Giuseppe Mazzapica
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Brain\Context;

/**
* @author Giuseppe Mazzapica <giuseppe.mazzapica@gmail.com>
* @package Context
* @license http://opensource.org/licenses/MIT MIT
*/
final class ArrayMergeRecursiveContextCollector implements ContextCollectorInterface
{

use ArrayMergeContextCollectorTrait;

/**
* @return array
*/
public function provide()
{
if (!$this->query instanceof \WP_Query) {
return [];
}

$context = [];
while (!$this->providers->isEmpty()) {
/** @var ContextProviderInterface|UpdatableContextProviderInterface $provider */
$provider = $this->providers->dequeue();
if (!$provider->accept($this->query)) {
continue;
}

$context = array_merge_recursive($context, $provider->provide());
if ($provider instanceof UpdatableContextProviderInterface) {
$context = $provider->update($context);
}
}

return $context;
}
}
10 changes: 7 additions & 3 deletions src/WpContextLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ class WpContextLoader
* to collector using `brain.context.providers` hook.
*
* @param \WP_Query $query
* @param ContextCollectorInterface $collector
* @return array
*/
public static function load(\WP_Query $query)
public static function load(\WP_Query $query, ContextCollectorInterface $collector = null)
{
$collector = new WpTemplateContextCollector();
$collector->accept($query);
$collector or $collector = new ArrayMergeContextCollector();

if (!$collector->accept($query)) {
return [];
}

// Use this hook to add context provider calling `addProvider()` method on the
// passed collector object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@

use Andrew\Proxy;
use Brain\Context\ContextProviderInterface;
use Brain\Context\WpTemplateContextCollector;
use Brain\Context\ArrayMergeContextCollector;
use Brain\Monkey\WP\Actions;

/**
* @author Giuseppe Mazzapica <giuseppe.mazzapica@gmail.com>
* @package Context
* @license http://opensource.org/licenses/MIT MIT
*/
class WpTemplateContextCollectorTest extends TestCase
class ArrayMergeContextCollectorTest extends TestCase
{

public function testAddProvider()
{
$collector = new WpTemplateContextCollector();
$collector = new ArrayMergeContextCollector();

$context_a = \Mockery::mock(ContextProviderInterface::class);
$context_b = clone $context_a;
Expand Down Expand Up @@ -60,7 +60,7 @@ public function testAddProvider()

public function testAccept()
{
$collector = new WpTemplateContextCollector();
$collector = new ArrayMergeContextCollector();

$query = \Mockery::mock('WP_Query');
$accepted = $collector->accept($query);
Expand All @@ -75,7 +75,7 @@ public function testAccept()

public function testProvideDoNothingWithNoQuery()
{
$collector = new WpTemplateContextCollector();
$collector = new ArrayMergeContextCollector();

$query = \Mockery::mock('WP_Query');

Expand All @@ -102,7 +102,7 @@ public function testProvideDoNothingWithNoQuery()

public function testProvide()
{
$collector = new WpTemplateContextCollector();
$collector = new ArrayMergeContextCollector();

$query = \Mockery::mock('WP_Query');

Expand Down
76 changes: 76 additions & 0 deletions tests/src/ArrayMergeRecursiveContextCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php # -*- coding: utf-8 -*-
/*
* This file is part of the Context package.
*
* (c) Giuseppe Mazzapica
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Brain\Context\Tests;

use Brain\Context\ArrayMergeRecursiveContextCollector;
use Brain\Context\ContextProviderInterface;
use Brain\Context\ArrayMergeContextCollector;
use Brain\Monkey\WP\Actions;

/**
* @author Giuseppe Mazzapica <giuseppe.mazzapica@gmail.com>
* @package Context
* @license http://opensource.org/licenses/MIT MIT
*/
class ArrayMergeRecursiveContextCollectorTest extends TestCase
{

public function testProvide()
{
$collector = new ArrayMergeRecursiveContextCollector();

$query = \Mockery::mock('WP_Query');

$context_a = \Mockery::mock(ContextProviderInterface::class);
$context_b = clone $context_a;
$context_c = clone $context_b;

$context_a->shouldReceive('accept')->once()->with($query)->andReturn(true);
$context_b->shouldReceive('accept')->once()->with($query)->andReturn(false);
$context_c->shouldReceive('accept')->once()->with($query)->andReturn(true);

$context_a->shouldReceive('provide')->once()->andReturn([
'message' => 'Hello from A!',
'letters' => ['a']
]);

$context_b->shouldReceive('provide')->once()->andReturn([
'message' => 'Goodbye from B!',
'meh' => 'meh'
]);

$context_c->shouldReceive('provide')->once()->andReturn([
'message' => 'Hello from C!',
'letters' => ['b', 'c', 'd'],
'color' => 'yellow'
]);

Actions::expectFired('brain.context.added')
->times(3)
->with(\Mockery::type(ContextProviderInterface::class), \Mockery::type('SplQueue'));

$collector
->addProvider($context_a)
->addProvider($context_b)
->addProvider($context_c);

$collector->accept($query);

$expected = [
'message' => ['Hello from A!', 'Hello from C!'],
'letters' => ['a', 'b', 'c', 'd'],
'color' => 'yellow'
];

assertSame($expected, $collector->provide());
}

}
4 changes: 2 additions & 2 deletions tests/src/WpContextLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Brain\Context\ContextProviderInterface;
use Brain\Context\UpdatableContextProviderInterface;
use Brain\Context\WpContextLoader;
use Brain\Context\WpTemplateContextCollector;
use Brain\Context\ArrayMergeContextCollector;
use Brain\Monkey\WP\Actions;
use Brain\Monkey\WP\Filters;

Expand Down Expand Up @@ -121,7 +121,7 @@ public function testLoad()
Actions::expectFired('brain.context.providers')
->once()
->whenHappen(
function (WpTemplateContextCollector $C) use ($archive, $page, $singular, $post) {
function (ArrayMergeContextCollector $C) use ($archive, $page, $singular, $post) {
$C
->addProvider($archive)
->addProvider($page)
Expand Down

0 comments on commit 780f72c

Please sign in to comment.