From 57df9e09ec07619ff00120157559dcb3d349872d Mon Sep 17 00:00:00 2001 From: francesco Date: Thu, 4 Jul 2019 01:29:57 +0200 Subject: [PATCH] add bind method --- README.md | 9 ++------- composer.json | 8 +++++++- granular.php | 8 ++++++-- src/Autoload.php | 29 ++++++++++++++++++++++++++--- src/Callback.php | 37 ++++++++++++++++++------------------- tests/AutoloadTest.php | 15 +++++++++++++++ tests/CallbackTest.php | 4 ++-- 7 files changed, 76 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index c39557d..492b27b 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,9 @@ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) WordPress extension framework based on object-oriented paradigm. -Usign Granular you can write PSR compliant code increasing the general code quality +Using Granular you can write PSR compliant code increasing the general code quality performing a better project organization. Organize your code in feature -or group wordpres actions and filters in the same scope. +or group WordPress actions and filters in the same scope. ```php namespace Acme\Plugin; @@ -38,15 +38,12 @@ class MyFirstPluginFeature extends Bindable ```php - use Javanile\Granular\Autoload; $app = new Autoload(); // add MyPlugin::init() method to WordPress init action $app->register(MyPlugin::class, 'init'); - - ``` @@ -55,5 +52,3 @@ $app->register(MyPlugin::class, 'init'); ```bash $ docker-compose run --rm phpunit --stop-on-failure tests ``` - - diff --git a/composer.json b/composer.json index 6281e75..4b97a6f 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,12 @@ { "name": "javanile/granular", - "version": "0.0.9", + "version": "0.0.10", + "authors": [ + { + "name": "Francesco Bianco", + "email": "bianco@javanile.org" + } + ], "autoload": { "psr-4": { "Javanile\\Granular\\": "src/", diff --git a/granular.php b/granular.php index bf7ec83..8a1053f 100644 --- a/granular.php +++ b/granular.php @@ -1,13 +1,13 @@ autoload('Javanile\\Granular\\', __DIR__.'/src'); + +$app->bindings([ + 'action:init' => 'init' +]); diff --git a/src/Autoload.php b/src/Autoload.php index 14ae450..434999f 100644 --- a/src/Autoload.php +++ b/src/Autoload.php @@ -15,9 +15,9 @@ class Autoload { /** - * Override functions. + * Dependency injection container. * - * @array + * @var ContainerInterface */ protected $container; @@ -69,6 +69,18 @@ public function autoload($namespace, $path) return $autoload; } + /** + * Bind single method of app class. + * + * @param $binding + * @param $method + * @return array + */ + public function bind($binding, $method = null) + { + return $this->processBindings([$binding => $method], $this); + } + /** * Register Class and specific method bindings. * @@ -87,8 +99,19 @@ public function register($class, $bindings = null) $bindings = [$bindings]; } + return $this->processBindings($bindings, $class); + } + + /** + * + * @param $bindings + * @param $referer + * @return array + */ + private function processBindings($bindings, $referer) + { $correctBindings = []; - $callback = new Callback($class, $this->container); + $callback = new Callback($referer, $this->container); foreach ($bindings as $binding => $methods) { if (is_numeric($binding)) { diff --git a/src/Callback.php b/src/Callback.php index 890bfd2..9ecf303 100644 --- a/src/Callback.php +++ b/src/Callback.php @@ -17,16 +17,16 @@ final class Callback /** * Referenced class to this instance. * - * @var string + * @var string|object */ - private $refClass; + private $referer; /** * Generated object using referenced class. * * @var object */ - private $refObject; + private $instance; /** * DI Container @@ -36,32 +36,31 @@ final class Callback /** * Callback constructor. * - * @param mixed $refClass - * + * @param $referer * @param ContainerInterface $container - * @internal param $class - * @internal param $method */ - public function __construct($refClass, ContainerInterface $container = null) + public function __construct($referer, ContainerInterface $container = null) { - $this->refClass = $refClass; + $this->referer = $referer; $this->container = $container; } /** - * Retrieve referenced object. + * Retrieve instance by referer or container key. */ - private function getRefObject() + private function getInstance() { - if ($this->container !== null && $this->container->has($this->refClass)) { - return $this->container->get($this->refClass); - } - - if ($this->refObject === null) { - $this->refObject = new $this->refClass(); + if ($this->instance !== null) { + return $this->instance; + } elseif (is_object($this->referer)) { + $this->instance = $this->referer; + } elseif ($this->container !== null && $this->container->has($this->referer)) { + $this->instance = $this->container->get($this->referer); + } else { + $this->instance = new $this->referer(); } - return $this->refObject; + return $this->instance; } /** @@ -74,7 +73,7 @@ private function getRefObject() public function getMethodCallback($method) { return function () use ($method) { - return call_user_func_array([$this->getRefObject(), $method], func_get_args()); + return call_user_func_array([$this->getInstance(), $method], func_get_args()); }; } } diff --git a/tests/AutoloadTest.php b/tests/AutoloadTest.php index 0347ad7..e6b1c59 100644 --- a/tests/AutoloadTest.php +++ b/tests/AutoloadTest.php @@ -19,6 +19,21 @@ public function testAutoload() ); } + public function testBindMethod() + { + $autoload = new Autoload(new FakeContainer); + + $this->assertEquals( + ['action:init' => ['init']], + $autoload->bind('init') + ); + + $this->assertEquals( + ['filter:the_content' => ['theContent']], + $autoload->bind('the_content', 'theContent') + ); + } + public function testRegisterClass() { $autoload = new Autoload(new FakeContainer); diff --git a/tests/CallbackTest.php b/tests/CallbackTest.php index 5ba97cf..6597bb4 100644 --- a/tests/CallbackTest.php +++ b/tests/CallbackTest.php @@ -14,9 +14,9 @@ public function testCallback() { $callback = new Callback(FakeRefClass::class, new FakeContainer); - $getRefObjectMethod = new ReflectionMethod($callback, 'getRefObject'); + $getInstanceMethod = new ReflectionMethod($callback, 'getInstance'); - $this->assertTrue($getRefObjectMethod->isPrivate()); + $this->assertTrue($getInstanceMethod->isPrivate()); $fakeRefMethod = $callback->getMethodCallback('fakeRefMethod');