Skip to content

Commit

Permalink
Fix coding style
Browse files Browse the repository at this point in the history
- Remove trailing spaces
- Add type hinting
- Coding style fix
- Document controller expand method
- Add php 5.6 to the travis list
- Coding style fix
  • Loading branch information
c9s committed Aug 17, 2015
1 parent 3690354 commit 9df0f6e
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -2,9 +2,11 @@ language: php
php:
- 5.4
- 5.5
- 5.6
matrix:
allow_failures:
- php: 5.5
- php: 5.6
install:
- sudo apt-get update
- sudo apt-get install php5-dev php-pear
Expand Down
33 changes: 22 additions & 11 deletions src/Pux/Controller.php
Expand Up @@ -5,9 +5,11 @@
use ReflectionMethod;
use Pux\Mux;

class Controller {
class Controller
{

protected function parseMethodAnnotation($method) {
protected function parseMethodAnnotation($method)
{

$annotations = array();
$doc = $method->getDocComment();
Expand All @@ -22,7 +24,8 @@ protected function parseMethodAnnotation($method) {
return $annotations;
}

protected function parseMethods($refObject, & $args, $parent = 0) {
protected function parseMethods($refObject, & $args, $parent = 0)
{
if ($pClassRef = $refObject->getParentClass()) {
$this->parseMethods($pClassRef, $args, 1);
}
Expand All @@ -47,15 +50,16 @@ protected function parseMethods($refObject, & $args, $parent = 0) {
}



public function getActionMethods() {
public function getActionMethods()
{
$refObject = new ReflectionObject($this);
$args = array();
$this->parseMethods($refObject, $args, 0);
return $args;
}

protected function translatePath($methodName) {
protected function translatePath($methodName)
{
$methodName = preg_replace('/Action$/', '', $methodName);
return '/' . preg_replace_callback('/[A-Z]/', function($matches) {
return '/' . strtolower($matches[0]);
Expand All @@ -66,8 +70,10 @@ protected function translatePath($methodName) {
/**
* Return [["/path", "testAction", [ "method" => ... ] ],...]
*
* @return array returns routes array
*/
public function getActionRoutes() {
public function getActionRoutes()
{
$pairs = array();
$actions = $this->getActionMethods();

Expand Down Expand Up @@ -96,19 +102,24 @@ public function getActionRoutes() {
return $pairs;
}

public function expand() {
/**
* Expand controller actions into Mux object
*
* @return Mux
*/
public function expand()
{
$mux = new Mux();
$paths = $this->getActionRoutes();

foreach ($paths as $path) {
$mux->add($path[0], array(get_class($this), $path[1]), $path[2]);
}

$mux->sort();
return $mux;
}

public function toJson($data) {
public function toJson($data)
{
return json_encode($data);
}

Expand Down
27 changes: 16 additions & 11 deletions src/Pux/Executor.php
@@ -1,38 +1,43 @@
<?php
namespace Pux;
use Exception;
use LogicException;
use ReflectionClass;
use ReflectionObject;
use ReflectionParameter;

class Executor
{
/*
/**
* Execute the matched route
*
* $route: {pcre flag}, {pattern}, {callback}, {options}
*
* @return string the response
*/
public static function execute($route)
public static function execute(array $route)
{
list($pcre,$pattern,$cb,$options) = $route;
list($pcre, $pattern, $cb, $options) = $route;

// create the reflection class
$rc = new ReflectionClass( $cb[0] );

$constructArgs = null;
if ( isset($options['constructor_args']) ) {
if (isset($options['constructor_args'])) {
$constructArgs = $options['constructor_args'];
}

// if the first argument is a class name string,
// then create the controller object.
if( is_string($cb[0]) ) {
if (is_string($cb[0])) {
$cb[0] = $controller = $constructArgs ? $rc->newInstanceArgs($constructArgs) : $rc->newInstance();
} else {
$controller = $cb[0];
}

// check controller action method
if( $controller && ! method_exists( $controller ,$cb[1]) ) {
throw new Exception('Controller exception');
if ($controller && ! method_exists( $controller ,$cb[1])) {
throw new LogicException("Controller action method '{$cb[1]}' doesn't exist.");
/*
throw new Exception('Method ' .
get_class($controller) . "->{$cb[1]} does not exist.", $route );
Expand All @@ -49,18 +54,18 @@ public static function execute($route)
;

$arguments = array();
foreach( $rps as $param ) {
foreach ($rps as $param) {
$n = $param->getName();
if( isset( $vars[ $n ] ) )
if (isset( $vars[ $n ] ))
{
$arguments[] = $vars[ $n ];
}
else if( isset($route[3]['default'][ $n ] )
else if (isset($route[3]['default'][ $n ] )
&& $default = $route[3]['default'][ $n ] )
{
$arguments[] = $default;
}
else if( ! $param->isOptional() && ! $param->allowsNull() ) {
else if ( ! $param->isOptional() && ! $param->allowsNull() ) {
throw new Exception('parameter is not defined.');
}
}
Expand Down
31 changes: 16 additions & 15 deletions src/Pux/Mux.php
Expand Up @@ -58,12 +58,12 @@ public function getId() {
return $this->id = self::generate_id();
}

public function appendRoute($pattern, $callback, $options = array() )
public function appendRoute($pattern, $callback, array $options = array())
{
$this->routes[] = array( false, $pattern, $callback, $options );
}

public function appendPCRERoute($routeArgs, $callback)
public function appendPCRERoute(array $routeArgs, $callback)
{
$this->routes[] = array(
true, // PCRE
Expand All @@ -73,7 +73,7 @@ public function appendPCRERoute($routeArgs, $callback)
);
}

public function mount($pattern, $mux, $options = array())
public function mount($pattern, $mux, array $options = array())
{
if ($mux instanceof Controller) {
$mux = $mux->expand();
Expand Down Expand Up @@ -108,55 +108,56 @@ public function mount($pattern, $mux, $options = array())
}
}

public function delete($pattern, $callback, $options = array())
public function delete($pattern, $callback, array $options = array())
{
$options['method'] = REQUEST_METHOD_DELETE;
$this->add($pattern, $callback, $options);
}

public function put($pattern, $callback, $options = array())
public function put($pattern, $callback, array $options = array())
{
$options['method'] = REQUEST_METHOD_PUT;
$this->add($pattern, $callback, $options);
}

public function get($pattern, $callback, $options = array())
public function get($pattern, $callback, array $options = array())
{
$options['method'] = REQUEST_METHOD_GET;
$this->add($pattern, $callback, $options);
}

public function post($pattern, $callback, $options = array())
public function post($pattern, $callback, array $options = array())
{
$options['method'] = REQUEST_METHOD_POST;
$this->add($pattern, $callback, $options);
}

public function patch($pattern, $callback, $options = array())
public function patch($pattern, $callback, array $options = array())
{
$options['method'] = REQUEST_METHOD_PATCH;
$this->add($pattern, $callback, $options);
}


public function head($pattern, $callback, $options = array())
public function head($pattern, $callback, array $options = array())
{
$options['method'] = REQUEST_METHOD_HEAD;
$this->add($pattern, $callback, $options);
}


public function options($pattern, $callback, $options = array())
public function options($pattern, $callback, array $options = array())
{
$options['method'] = REQUEST_METHOD_OPTIONS;
$this->add($pattern, $callback, $options);
}

public function any($pattern, $callback, $options = array()) {
public function any($pattern, $callback, array $options = array())
{
$this->add($pattern, $callback, $options);
}

public function add($pattern, $callback, $options = array())
public function add($pattern, $callback, array $options = array())
{
if ( is_string($callback) && strpos($callback,':') !== false ) {
$callback = explode(':', $callback);
Expand Down Expand Up @@ -318,7 +319,7 @@ public function dispatch($path)
} else {
$s = substr($path, strlen($route[1]));
return $submux->dispatch(
substr($path, strlen($route[1])) ?: ''
substr($path, strlen($route[1])) ?: ''
);
}
} else {
Expand All @@ -327,12 +328,12 @@ public function dispatch($path)
}
}

public function length()
public function length()
{
return count($this->routes);
}

public function getRoutes()
public function getRoutes()
{
return $this->routes;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Pux/PatternCompiler.php
Expand Up @@ -22,13 +22,13 @@ class PatternCompiler
* @param string $pattern
* @param array $options
*/
static function compilePattern($pattern, $options = array() )
static function compilePattern($pattern, array $options = array())
{

$len = strlen($pattern);
/**
* contains:
*
*
* array( 'text', $text ),
* array( 'variable', $match[0][0][0], $regexp, $var);
*
Expand Down Expand Up @@ -220,7 +220,7 @@ static function splitTokens($string)
*
* @return array compiled route info, with newly added 'compiled' key.
*/
static function compile($pattern, $options = array())
static function compile($pattern, array $options = array())
{
$route = self::compilePattern($pattern, $options);

Expand Down
4 changes: 2 additions & 2 deletions test/Pux/RESTfulControllerTest.php
Expand Up @@ -52,10 +52,10 @@ public function test()
$methods = $con->getActionMethods();
$this->assertNotEmpty($methods);
$productMux = $con->expand(); // there is a sorting bug (fixed), this tests it.
ok($productMux);
$this->assertNotEmpty($productMux);

$root = new Mux;
$root->mount('/product', $con->expand() );
$root->mount('/product', $con->expand());

$_SERVER['REQUEST_METHOD'] = 'GET';
$this->assertNotNull($root->dispatch('/product/10'));
Expand Down

0 comments on commit 9df0f6e

Please sign in to comment.