-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1ef8e2e
Showing
22 changed files
with
1,098 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
namespace KodCube\Invoker; | ||
|
||
use Exception; | ||
|
||
class MissingClassException extends Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
namespace KodCube\Invoker; | ||
|
||
use Exception; | ||
|
||
class MissingMethodException extends Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
namespace KodCube\Invoker; | ||
|
||
use Exception; | ||
|
||
class NotCallableException extends Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
namespace KodCube\Invoker; | ||
|
||
use Exception; | ||
|
||
class PrivateMethodException extends Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
namespace KodCube\Invoker; | ||
|
||
use Exception; | ||
|
||
class ProtectedMethodException extends Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.