Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
srmiles committed Jul 17, 2016
0 parents commit 1ef8e2e
Show file tree
Hide file tree
Showing 22 changed files with 1,098 additions and 0 deletions.
44 changes: 44 additions & 0 deletions GunnaPHP/Invoker/ContainerInvoker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
namespace KodCube\Invoker;

use KodCube\Helpers\Arrays;
use Interop\Container\ContainerInterface;
use ReflectionClass;

class ContainerInvoker extends Invoker implements InvokerInterface
{
protected $container = null;

public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

public function createObject($className,$classParams=null)
{
$class = new ReflectionClass($className);

if ( $class->getConstructor() === null ) {

return new $className;

}

if ( $classParams === null || empty($classParams)) {

return $this->container->get($className);

}

if ( is_array($classParams) && ! Arrays::isAssoc($classParams) ) {

// Invoke Object with multiple arguments
return $this->container->get($className,...$classParams);

}

// Invoke Object with single argument
return $this->container->get($className,$classParams);
}

}
176 changes: 176 additions & 0 deletions GunnaPHP/Invoker/Invoker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php
namespace KodCube\Invoker;

use KodCube\Helpers\Arrays;
use ReflectionMethod;
use Exception;
use Throwable;

/**
* Calls an invoked class::method with passed params
*
* @author Steven Miles <[email protected]>
*/

class Invoker implements InvokerInterface
{
/**
* @inheritdoc
*/

public function invoke(string $className, array $methodParams=null, array $classParams=null)
{
return $this($className,$methodParams,$classParams);
}

/**
* Class Invoker
*
* @param string $className
* @param array|NULL $methodParams
* @param array|NULL $classParams
* @return mixed
* @throws \Exception
*/
public function __invoke(string $className, array $methodParams=null, array $classParams=null)
{

$className = str_replace('.','\\',$className);
$methodName = null;

if (strpos($className,'::') !== false) {

list($className,$methodName) = explode('::',$className);

}

// Check if the Class Exists

if ( ! class_exists($className,true) ) throw new MissingClassException($className.' does not exist');


if ( ! is_null($methodName)) {

// Check if Static Method or Object Method Call
try {
$reflectionMethod = new \ReflectionMethod($className, $methodName);
} catch (Throwable $t) {
throw new MissingMethodException($className.'::'.$methodName.' does not exist');
}

if ($reflectionMethod->isStatic()) {

return $this->staticMethodCall($className,$methodName,$methodParams);
}


// Create Instance of Class

return $this->objectMethodCall($className,$classParams,$methodName,$methodParams);
}

// If there are no method must be a invokable object

// Create Instance of Class
$class = $this->createObject($className,$classParams);

if ( ! is_callable($class) ) {
throw new NotCallableException($className.' is not callable');
}

return $this->objectCall($class,$methodParams);

}

public function staticMethodCall($className,$methodName,$methodParams=null)
{

$reflection = new ReflectionMethod($className,$methodName);

if ($reflection->isProtected() ) {
throw new ProtectedMethodException($className.'::'.$methodName.' is Protected');
}

if ($reflection->isPrivate() ) {
throw new PrivateMethodException($className.'::'.$methodName.' is Private');
}

// Static Method Call
if ( is_array($methodParams) && ! Arrays::isAssoc($methodParams) ) {

// Invoke Static Method with multiple arguments
return $className::$methodName(...$methodParams);

}

// Invoke Static Method with single argument
return $className::$methodName($methodParams);

}

public function objectMethodCall($className,$classParams=null,$methodName,$methodParams=null)
{
$class = $this->createObject($className,$classParams);

if ( ! method_exists($class,$methodName) ) {
throw new \Exception($className.'::'.$methodName.' not found');
}

if ( ! is_callable($className.'::'.$methodName) ) {

throw new ProtectedMethodException($className.'::'.$methodName.' is not Callable');

}

// Invoke Object Method with multiple arguments
if ( is_array($methodParams) && ! Arrays::isAssoc($methodParams) ) {

return $class->$methodName(...$methodParams);

}

// Invoke Object Method with single argument
return $class->$methodName($methodParams);
}

public function objectCall($class,$methodParams=null)
{
// Invoke Object Method with multiple arguments
if ( is_array($methodParams) && ! Arrays::isAssoc($methodParams) ) {

return $class(...$methodParams);

}

// Invoke Object Method with single argument
return $class($methodParams);
}

public function createObject($className,$classParams=null)
{
$class = new \ReflectionClass($className);

if ( $class->getConstructor() === null ) {

return new $className;

}

if ( $classParams === null || empty($classParams)) {

return new $className();

}

if ( is_array($classParams) && ! Arrays::isAssoc($classParams) ) {

// Invoke Object with multiple arguments
return new $className(...$classParams);

}

// Invoke Object with single argument
return new $className($classParams);
}

}
11 changes: 11 additions & 0 deletions GunnaPHP/Invoker/InvokerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace KodCube\Invoker;



interface InvokerInterface
{

public function __invoke(string $className, array $methodParams=null, array $classParams=null);

}
9 changes: 9 additions & 0 deletions GunnaPHP/Invoker/MissingClassException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace KodCube\Invoker;

use Exception;

class MissingClassException extends Exception
{

}
9 changes: 9 additions & 0 deletions GunnaPHP/Invoker/MissingMethodException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace KodCube\Invoker;

use Exception;

class MissingMethodException extends Exception
{

}
9 changes: 9 additions & 0 deletions GunnaPHP/Invoker/NotCallableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace KodCube\Invoker;

use Exception;

class NotCallableException extends Exception
{

}
9 changes: 9 additions & 0 deletions GunnaPHP/Invoker/PrivateMethodException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace KodCube\Invoker;

use Exception;

class PrivateMethodException extends Exception
{

}
9 changes: 9 additions & 0 deletions GunnaPHP/Invoker/ProtectedMethodException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace KodCube\Invoker;

use Exception;

class ProtectedMethodException extends Exception
{

}
40 changes: 40 additions & 0 deletions GunnaPHP/Invoker/Queue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
namespace KodCube\Invoker;

use GunnaPHP\Queue\ProviderInterface AS QueueInterface;

/**
* Queues an invoked class::method to a background process instead of executing
* within the current process
*
* @author Steven Miles <[email protected]>
*/

class Queue implements InvokerInterface
{
protected $queue = null;

/**
* @param QueueInterface $queue Queue Provider Interface
*/

public function __construct(QueueInterface $queue)
{
$this->queue = $queue;
}

/**
* @inheritdoc
*/

public function __invoke(string $className, array $methodParams=null, array $classParams = null)
{

return $this->queue->enqueue([
'className' => $className,
'methodParams' => $methodParams,
'classParams' => $classParams
]);

}
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Steven Miles <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit 1ef8e2e

Please sign in to comment.