Skip to content

Commit

Permalink
Put SyntaxExtender in another class file
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Apr 16, 2015
1 parent 6b648cf commit 363ff35
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 41 deletions.
62 changes: 62 additions & 0 deletions src/SQLBuilder/SyntaxExtender.php
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;
}
}
}

42 changes: 1 addition & 41 deletions src/SQLBuilder/Universal/Query/AlterTableQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,47 +24,7 @@
use SQLBuilder\Exception\CriticalIncompatibleUsageException;

use SQLBuilder\MySQL\Syntax\AlterTableOrderBy;

use BadMethodCallException;
use ReflectionObject;
use ReflectionClass;

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;
}
}
}
use SQLBuilder\SyntaxExtender;

class AlterTableQuery implements ToSqlInterface
{
Expand Down

0 comments on commit 363ff35

Please sign in to comment.