From 9df0f6ee72521881a3b77d3a5b65d9118c87a0f3 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 17 Aug 2015 23:23:31 +0800 Subject: [PATCH] Fix coding style - Remove trailing spaces - Add type hinting - Coding style fix - Document controller expand method - Add php 5.6 to the travis list - Coding style fix --- .travis.yml | 2 ++ src/Pux/Controller.php | 33 ++++++++++++++++++++---------- src/Pux/Executor.php | 27 ++++++++++++++---------- src/Pux/Mux.php | 31 ++++++++++++++-------------- src/Pux/PatternCompiler.php | 6 +++--- test/Pux/RESTfulControllerTest.php | 4 ++-- 6 files changed, 61 insertions(+), 42 deletions(-) diff --git a/.travis.yml b/.travis.yml index ddea528..9bf73fe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/src/Pux/Controller.php b/src/Pux/Controller.php index 2dd48c4..e721399 100644 --- a/src/Pux/Controller.php +++ b/src/Pux/Controller.php @@ -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(); @@ -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); } @@ -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]); @@ -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(); @@ -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); } diff --git a/src/Pux/Executor.php b/src/Pux/Executor.php index 288bd15..e0793b9 100644 --- a/src/Pux/Executor.php +++ b/src/Pux/Executor.php @@ -1,38 +1,43 @@ 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 ); @@ -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.'); } } diff --git a/src/Pux/Mux.php b/src/Pux/Mux.php index a14bbde..028b621 100644 --- a/src/Pux/Mux.php +++ b/src/Pux/Mux.php @@ -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 @@ -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(); @@ -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); @@ -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 { @@ -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; } diff --git a/src/Pux/PatternCompiler.php b/src/Pux/PatternCompiler.php index d1dc01d..f36d089 100644 --- a/src/Pux/PatternCompiler.php +++ b/src/Pux/PatternCompiler.php @@ -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); * @@ -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); diff --git a/test/Pux/RESTfulControllerTest.php b/test/Pux/RESTfulControllerTest.php index ccc49e3..9418a2e 100644 --- a/test/Pux/RESTfulControllerTest.php +++ b/test/Pux/RESTfulControllerTest.php @@ -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'));