Skip to content
This repository has been archived by the owner on Dec 17, 2021. It is now read-only.

Commit

Permalink
Rename classes
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiomontefuscolo committed May 15, 2018
1 parent 4eb8592 commit b2bd946
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 29 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "tikiorg/simple-hooks-manager",
"name": "montefuscolo/php-hooks",
"description": "A class to provide hooks as actions and filters",
"type": "library",
"require-dev": {
Expand All @@ -16,7 +16,7 @@
"scripts": {
"test": [
"find src -name '*.php' -exec php -l {} \\;",
"./vendor/bin/phpunit --bootstrap vendor/autoload.php tests/SimpleHooksManagerTest"
"./vendor/bin/phpunit --bootstrap vendor/autoload.php tests/BaseMediatorTest"
]
},
"autoload": {
Expand Down
63 changes: 46 additions & 17 deletions src/SimpleHooksManager.php → src/BaseMediator.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
<?php
namespace TikiOrg;
namespace montefuscolo;

class SimpleHooksManager {
class BaseMediator {
private static $instance = null;
private $filters = null;
private $actions = null;
private $prefix = 'root:';

private function __construct() {
$this->filters = array();
$this->actions = array();
}

public static function getInstance() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}

private function add(&$chain, $name, $callback, $priority) {
if (empty($chain[ $name ])) {
$chain[ $name ] = array();
}
$chain[ $name ][ ] = array($callback, $priority);
}

public function add_filter($name, $callback, $priority=10) {
$this->add($this->filters, $name, $callback, $priority);
}

public function add_action($name, $callback, $priority=10) {
$this->add($this->actions, $name, $callback, $priority);
}

private function remove(&$chain, $name, $func) {
if (empty($chain[ $name ])) {
return;
Expand All @@ -49,6 +35,49 @@ private function remove(&$chain, $name, $func) {
}
}

private function run(&$chain, $name, $subject=null) {
if (empty($chain[ $name ])) {
return $subject;
}

$filtering = $this->filters === $chain;
$subject = array_slice(func_get_args(), 2);

usort($chain[ $name ], function($a, $b) {
return $a[1] === $b[1] ? 0 : ( $a[1] < $b[1] ? -1 : 1 );
});

foreach ($chain[ $name ] as $function) {
list($callback, $priority) = $function;
$result = call_user_func_array($callback, $subject);

if ($filtering) {
$subject = array($result);
}
}

return $subject;
}

private function get_hook_name($name) {
return $this->prefix . $name;
}

public static function getInstance() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}

public function add_filter($name, $callback, $priority=10) {
$this->add($this->filters, $name, $callback, $priority);
}

public function add_action($name, $callback, $priority=10) {
$this->add($this->actions, $name, $callback, $priority);
}

public function remove_filter($name, $func=null) {
return $this->remove($this->filters, $name, $func);
}
Expand Down
20 changes: 10 additions & 10 deletions tests/SimpleHooksManagerTest.php → tests/BaseMediatorTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

use PHPUnit\Framework\TestCase;
use TikiOrg\SimpleHooksManager;
use montefuscolo\BaseMediator;


class SimpleHooksManagerTest extends TestCase {
class BaseMediatorTest extends TestCase {

public function testUniqueInstance() {
$instance1 = SimpleHooksManager::getInstance();
$instance2 = SimpleHooksManager::getInstance();
$instance1 = BaseMediator::getInstance();
$instance2 = BaseMediator::getInstance();

$this->assertSame($instance1, $instance2);
$this->assertEquals($instance1, $instance2);
Expand All @@ -17,7 +17,7 @@ public function testUniqueInstance() {
public function testAddAction() {
$self = $this;

$hooks = SimpleHooksManager::getInstance();
$hooks = BaseMediator::getInstance();
$hooks->add_action('test_add_action', function() use ($self) {
$self->assertTrue(true);
});
Expand All @@ -28,7 +28,7 @@ public function testAddAction() {
public function testAddActionWithParameters() {
$self = $this;

$hooks = SimpleHooksManager::getInstance();
$hooks = BaseMediator::getInstance();
$hooks->add_action('test_addaction_with_parameters', function($subject) use ($self) {
$self->assertEquals('the-parameter', $subject);
});
Expand All @@ -38,7 +38,7 @@ public function testAddActionWithParameters() {


public function testAddActionPriority() {
$hooks = SimpleHooksManager::getInstance();
$hooks = BaseMediator::getInstance();
$state = (object) array('name' => '');

$hooks->add_action('test_add_action_priority', function($subject) {
Expand All @@ -58,7 +58,7 @@ public function testAddActionPriority() {
}

public function testRemoveAction() {
$hooks = SimpleHooksManager::getInstance();
$hooks = BaseMediator::getInstance();

$callback1 = function($subject) { $subject->name .= 'callback1'; };
$hooks->add_action('test_remove_action', $callback1, 10);
Expand All @@ -77,7 +77,7 @@ public function testRemoveAction() {
}

public function testEmptyAction() {
$hooks = SimpleHooksManager::getInstance();
$hooks = BaseMediator::getInstance();

$callback1 = function($subject) { $subject->name .= 'callback1'; };
$hooks->add_action('test_empty_action', $callback1, 10);
Expand All @@ -92,7 +92,7 @@ public function testEmptyAction() {
}

public function testAddFilter() {
$hooks = SimpleHooksManager::getInstance();
$hooks = BaseMediator::getInstance();

$hooks->add_filter('test_add_filter', function($subject){
return $subject * 2;
Expand Down

0 comments on commit b2bd946

Please sign in to comment.