Skip to content

Commit

Permalink
add bind method
Browse files Browse the repository at this point in the history
  • Loading branch information
francescobianco committed Jul 3, 2019
1 parent eafb05c commit 57df9e0
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 34 deletions.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');


```


Expand All @@ -55,5 +52,3 @@ $app->register(MyPlugin::class, 'init');
```bash
$ docker-compose run --rm phpunit --stop-on-failure tests
```


8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"name": "javanile/granular",
"version": "0.0.9",
"version": "0.0.10",
"authors": [
{
"name": "Francesco Bianco",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Javanile\\Granular\\": "src/",
Expand Down
8 changes: 6 additions & 2 deletions granular.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
/**
* @version 0.0.9
* @version 0.0.10
*/
/*
Plugin Name: Granular
Plugin URI: https://github.com/javanile/granular
Description: WordPress extension framework based on object-oriented paradigm.
Author: Francesco Bianco
Version: 0.0.9
Version: 0.0.10
Author URI: https://github.com/javanile
*/

Expand All @@ -18,3 +18,7 @@
$app = new Javanile\Granular\Autoload();

$app->autoload('Javanile\\Granular\\', __DIR__.'/src');

$app->bindings([
'action:init' => 'init'
]);
29 changes: 26 additions & 3 deletions src/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
class Autoload
{
/**
* Override functions.
* Dependency injection container.
*
* @array
* @var ContainerInterface
*/
protected $container;

Expand Down Expand Up @@ -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.
*
Expand All @@ -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)) {
Expand Down
37 changes: 18 additions & 19 deletions src/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand All @@ -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());
};
}
}
15 changes: 15 additions & 0 deletions tests/AutoloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/CallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down

0 comments on commit 57df9e0

Please sign in to comment.