-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Put SyntaxExtender in another class file
- Loading branch information
Showing
2 changed files
with
63 additions
and
41 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,62 @@ | ||
<?php | ||
namespace SQLBuilder; | ||
use BadMethodCallException; | ||
use ReflectionObject; | ||
use ReflectionClass; | ||
|
||
/** | ||
* SyntaxExtender let you register a customized method name to a syntax class... | ||
* | ||
* | ||
* Add the code below to support syntax extender: | ||
* | ||
* | ||
* class FooQuery { | ||
* | ||
* use SQLBuilder\SyntaxExtender; | ||
* | ||
* public function __call($methodName, $arguments = array()) { | ||
* return $this->someProperty = $this->handleSyntax($methodName, $arguments); | ||
* } | ||
* | ||
* } | ||
* | ||
* | ||
*/ | ||
trait SyntaxExtender { | ||
|
||
protected $extraSyntax = array(); | ||
|
||
protected $syntaxClass = array(); | ||
|
||
protected $reflectionCache = array(); | ||
|
||
public function registerClass($methodName, $class) | ||
{ | ||
$this->syntaxClass[ $methodName ] = $class; | ||
} | ||
|
||
public function registerCallback($methodName, callable $callback) | ||
{ | ||
$this->extraSyntax[$methodName] = $callback; | ||
} | ||
|
||
|
||
public function handleSyntax($methodName, array $arguments = array()) | ||
{ | ||
if (isset($this->syntaxClass[$methodName])) { | ||
$refClass = null; | ||
if (isset($this->reflectionCache[$methodName])) { | ||
$refClass = $this->reflectionCache[$methodName]; | ||
} else { | ||
$this->reflectionCache[$methodName] = $refClass = new ReflectionClass($this->syntaxClass[$methodName]); | ||
} | ||
return $refClass->newInstanceArgs($arguments); | ||
} else if (isset($this->extraSyntax[$methodName])) { | ||
return call_user_func_array($this->extraSyntax[$methodName], $arguments); | ||
} else { | ||
throw new BadMethodCallException; | ||
} | ||
} | ||
} | ||
|
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