From 36d7788b74ca8a186a66344d4e89d4c551559526 Mon Sep 17 00:00:00 2001 From: Marko Kallio Date: Mon, 4 Jan 2016 07:53:57 +0200 Subject: [PATCH] Some statics to non-statics. --- src/YamlRoute/ConversionTrait.php | 4 +- src/YamlRoute/Generator.php | 117 ++++++++++++++-------------- src/YamlRoute/Plugin.php | 19 +++-- tests/YamlRoute/IntegrationTest.php | 25 +++--- 4 files changed, 84 insertions(+), 81 deletions(-) diff --git a/src/YamlRoute/ConversionTrait.php b/src/YamlRoute/ConversionTrait.php index 45cce85..44a0332 100644 --- a/src/YamlRoute/ConversionTrait.php +++ b/src/YamlRoute/ConversionTrait.php @@ -16,7 +16,7 @@ trait ConversionTrait * * @return string */ - private static function _arrToStr($array) + private function _arrToStr($array) { $str = '['; foreach ($array as $key => $value) { @@ -39,7 +39,7 @@ private static function _arrToStr($array) * * @return mixed */ - private static function _varsToString($string) + private function _varsToString($string) { $string = str_replace('{', ':', $string); diff --git a/src/YamlRoute/Generator.php b/src/YamlRoute/Generator.php index 2eae5bb..70ab3e9 100644 --- a/src/YamlRoute/Generator.php +++ b/src/YamlRoute/Generator.php @@ -21,7 +21,7 @@ class Generator * * @var array */ - private static $_routeConfigs = []; + private $_routeConfigs = []; /** * Instance of Generator @@ -33,17 +33,17 @@ class Generator /** * @var */ - private static $_dump; + private $_dump; /** * @var bool */ - private static $_debug = false; + private $_debug = false; /** * @var bool */ - private static $_executed = [ + private $_executed = [ '\Cake\Core\Plugin::routes()' => false, ]; @@ -66,30 +66,30 @@ public static function getInstance() * * @param $config */ - private static function _addRouteConfig($config) + private function _addRouteConfig($config) { - self::$_routeConfigs[] = $config; + $this->_routeConfigs[] = $config; } /** * Set route configurations */ - private static function _setRouteConfigs() + private function _setRouteConfigs() { - foreach (Plugin::getLoaded() as $item) { - self::_addRouteConfig($item); + foreach (Plugin::getInstance()->getLoaded() as $item) { + $this->_addRouteConfig($item); } } /** * Load project routes.yml file */ - private static function _loadProjectConfig() + private function _loadProjectConfig() { $path = ROOT . DS . 'config' . DS . 'routes.yml'; if (file_exists($path)) { $route = Yaml::parse(file_get_contents($path)); - self::_addRouteConfig(['name' => 'Project', 'route' => $route]); + $this->_addRouteConfig(['name' => 'Project', 'route' => $route]); } } @@ -98,27 +98,26 @@ private static function _loadProjectConfig() * * @return array */ - private static function _getRouteConfigs() + private function _getRouteConfigs() { - return self::$_routeConfigs; + return $this->_routeConfigs; } /** * Generate routes */ - private static function _generateRoutes() + private function _generateRoutes() { - self::_loadProjectConfig(); - $configs = self::_getRouteConfigs(); + $this->_loadProjectConfig(); + $configs = $this->_getRouteConfigs(); foreach ($configs as $config) { if (isset($config['route'])) { foreach ($config['route'] as $name => $route) { - - self::_newRoute($name, $route); + $this->_newRoute($name, $route); } } } - self::_pluginRoutes(); + $this->_pluginRoutes(); } /** @@ -127,7 +126,7 @@ private static function _generateRoutes() * @param $name * @param $route */ - private static function _newRoute($name, $route) + private function _newRoute($name, $route) { $method = 'scope'; $path = '/'; @@ -137,7 +136,7 @@ private static function _newRoute($name, $route) if (!is_array($route['config'])) { $route['config'] = self::_loadRouteConfig($route['config']); } - if (isset($route['config']['plugin']) && Plugin::isLoaded($route['config']['plugin'])) { + if (isset($route['config']['plugin']) && Plugin::getInstance()->isLoaded($route['config']['plugin'])) { $method = 'plugin'; $path = $route['config']['plugin']; $options = ['path' => $route['path']]; @@ -152,8 +151,8 @@ private static function _newRoute($name, $route) } // Debugging - if (self::$_debug) { - self::_addToDump("\\Cake\\Routing\\Router::$method('$path', " . self::_arrToStr($options) . ", function (" . '$routes' . ") {"); + if ($this->_debug) { + $this->_addToDump("\\Cake\\Routing\\Router::$method('$path', " . $this->_arrToStr($options) . ", function (" . '$routes' . ") {"); } Router::$method( @@ -164,11 +163,11 @@ private static function _newRoute($name, $route) if (isset($route['config']['extensions']) && is_array($route['config']['extensions'])) { /* @var \Cake\Routing\Router $routes */ $routes->extensions($route['config']['extensions']); - if (self::$_debug) { - self::_addToDump("\t" . '$routes->extensions(' . self::_arrToStr($route['config']['extensions']) . ');'); + if ($this->_debug) { + $this->_addToDump("\t" . '$routes->extensions(' . $this->_arrToStr($route['config']['extensions']) . ');'); } } - $route = self::_createPassParams($route); + $route = $this->_createPassParams($route); $opts = []; foreach ($route['config'] as $key => $item) { @@ -177,14 +176,14 @@ private static function _newRoute($name, $route) } } - $thirdParam = self::_buildThirdParam($name, $route); + $thirdParam = $this->_buildThirdParam($name, $route); /* @var \Cake\Routing\Router $routes */ $routes->connect('/', $opts, $thirdParam); // Debugging - if (self::$_debug) { - self::_addToDump("\t" . '$routes->connect(\'/\', ' . self::_arrToStr($opts) . ', ' . self::_arrToStr($thirdParam) . ');'); + if ($this->_debug) { + $this->_addToDump("\t" . '$routes->connect(\'/\', ' . $this->_arrToStr($opts) . ', ' . $this->_arrToStr($thirdParam) . ');'); } } if (isset($route['config']) && isset($route['config']['routes'])) { @@ -192,8 +191,8 @@ private static function _newRoute($name, $route) if (isset($x['extensions']) && is_array($x['extensions'])) { /* @var \Cake\Routing\Router $routes */ $routes->extensions($x['extensions']); - if (self::$_debug) { - self::_addToDump("\t" . '$routes->extensions(' . self::_arrToStr($x['extensions']) . ');'); + if ($this->_debug) { + $this->_addToDump("\t" . '$routes->extensions(' . $this->_arrToStr($x['extensions']) . ');'); } } $x = self::_createPassParams($x); @@ -204,14 +203,14 @@ private static function _newRoute($name, $route) } } - $thirdParam = self::_buildThirdParam($key, $x); + $thirdParam = $this->_buildThirdParam($key, $x); /* @var \Cake\Routing\Router $routes */ - $routes->connect('/' . self::_varsToString($x['path']), $opts, $thirdParam); + $routes->connect('/' . $this->_varsToString($x['path']), $opts, $thirdParam); // Debugging - if (self::$_debug) { - self::_addToDump("\t" . '$routes->connect(\'' . self::_varsToString($x['path']) . '\', ' . self::_arrToStr($opts) . ', ' . self::_arrToStr($thirdParam) . ');'); + if ($this->_debug) { + $this->_addToDump("\t" . '$routes->connect(\'' . $this->_varsToString($x['path']) . '\', ' . $this->_arrToStr($opts) . ', ' . $this->_arrToStr($thirdParam) . ');'); } } } @@ -224,15 +223,15 @@ private static function _newRoute($name, $route) $routes->fallbacks($fallbacks); // Debugging - if (self::$_debug) { - self::_addToDump("\t" . '$routes->fallbacks(\'' . $fallbacks . '\');'); + if ($this->_debug) { + $this->_addToDump("\t" . '$routes->fallbacks(\'' . $fallbacks . '\');'); } } ); // Debugging - if (self::$_debug) { - self::_addToDump('});' . "\n"); + if ($this->_debug) { + $this->_addToDump('});' . "\n"); } } @@ -284,15 +283,15 @@ private static function _createPassParams($route) /** * Run Plugin::routes() */ - private static function _pluginRoutes() + private function _pluginRoutes() { if (!self::_isExecuted('\Cake\Core\Plugin::routes()')) { CakePlugin::routes(); - self::setExecuted('\Cake\Core\Plugin::routes()'); + $this->_setExecuted('\Cake\Core\Plugin::routes()'); // Debugging - if (self::$_debug) { - self::_addToDump('\Cake\Core\Plugin::routes();' . "\n"); + if ($this->_debug) { + $this->_addToDump('\Cake\Core\Plugin::routes();' . "\n"); } } } @@ -302,9 +301,9 @@ private static function _pluginRoutes() * * @param $name */ - private static function setExecuted($name) + private function _setExecuted($name) { - self::$_executed[$name] = true; + $this->_executed[$name] = true; } /** @@ -314,9 +313,9 @@ private static function setExecuted($name) * * @return mixed */ - private static function _isExecuted($name) + private function _isExecuted($name) { - return self::$_executed[$name]; + return $this->_executed[$name]; } /** @@ -324,9 +323,9 @@ private static function _isExecuted($name) * * @param $string */ - private static function _addToDump($string) + private function _addToDump($string) { - self::$_dump .= $string . "\n"; + $this->_dump .= $string . "\n"; } /** @@ -334,9 +333,9 @@ private static function _addToDump($string) * * @return mixed */ - public static function getDump() + public function getDump() { - return trim(self::$_dump); + return trim($this->_dump); } /** @@ -346,11 +345,11 @@ public static function getDump() * * @return array */ - private static function _loadRouteConfig($config) + private function _loadRouteConfig($config) { if (strpos($config, '.') !== false) { list($plugin, $file) = explode('.', $config); - $pluginPaths = Plugin::getLoaded(); + $pluginPaths = Plugin::getInstance()->getLoaded(); $path = $pluginPaths[$plugin] . DS . 'config' . DS . $file . '.yml'; } else { $path = ROOT . DS . 'config' . DS . $config . '.yml'; @@ -363,11 +362,15 @@ private static function _loadRouteConfig($config) * Generate routes based on yml files * * @param $debug + * + * @return $this */ - public static function run($debug = false) + public function run($debug = false) { - self::$_debug = $debug; - self::_setRouteConfigs(); - self::_generateRoutes(); + $this->_debug = $debug; + $this->_setRouteConfigs(); + $this->_generateRoutes(); + + return $this; } } \ No newline at end of file diff --git a/src/YamlRoute/Plugin.php b/src/YamlRoute/Plugin.php index 3893a15..c8efec1 100644 --- a/src/YamlRoute/Plugin.php +++ b/src/YamlRoute/Plugin.php @@ -2,7 +2,6 @@ namespace YamlRoute; -use \Cake\Core\App; use Cake\Core\Configure; use Cake\Core\Plugin as CakePlugin; use Symfony\Component\Yaml\Yaml; @@ -21,7 +20,7 @@ class Plugin * * @var array */ - private static $_loaded = []; + private $_loaded = []; /** * Instance pf Plugin class @@ -35,9 +34,9 @@ class Plugin * * @param $plugin */ - private static function _addLoaded($plugin) + private function _addLoaded($plugin) { - self::$_loaded[] = $plugin; + $this->_loaded[] = $plugin; } /** @@ -59,9 +58,9 @@ public static function getInstance() * * @return array */ - public static function getLoaded() + public function getLoaded() { - return self::$_loaded; + return $this->_loaded; } /** @@ -70,7 +69,7 @@ public static function getLoaded() * * @throws \YamlRoute\YamlRouteException */ - public static function load($plugins, $options) + public function load($plugins, $options) { $routes = isset($options['routes']) && $options['routes'] === true ? true : false; $options['routes'] = false; @@ -83,7 +82,7 @@ public static function load($plugins, $options) } foreach ($plugins as $plugin) { - if (self::isLoaded($plugin)) { + if ($this->isLoaded($plugin)) { throw new YamlRouteException("Plugin $plugin is loaded already and should not be loaded twice."); } $path = Configure::read('App.paths.plugins')[0] . DS . $plugin . DS . 'config' . DS . 'routes.yml'; @@ -91,7 +90,7 @@ public static function load($plugins, $options) throw new YamlRouteException("Yaml route configuration file not found in path $path."); } $route = Yaml::parse(file_get_contents($path)); - self::_addLoaded(['name' => $plugin, 'route' => $route]); + $this->_addLoaded(['name' => $plugin, 'route' => $route]); } } } @@ -103,7 +102,7 @@ public static function load($plugins, $options) * * @return bool */ - public static function isLoaded($plugin) + public function isLoaded($plugin) { foreach (self::getLoaded() as $loaded) { if ($plugin === $loaded['name']) { diff --git a/tests/YamlRoute/IntegrationTest.php b/tests/YamlRoute/IntegrationTest.php index 7728f32..f742882 100644 --- a/tests/YamlRoute/IntegrationTest.php +++ b/tests/YamlRoute/IntegrationTest.php @@ -13,25 +13,26 @@ class IntegrationTest extends \PHPUnit_Framework_TestCase { /** - * @throws \YamlRoute\CakeYamlException + * Basic integration test + * @throws \YamlRoute\YamlRouteException */ - public function testLoad() + public function testIntegration() { - $assert = '\Cake\Routing\Router::plugin(\'PluginCars\', [\'path\' => \'/cars\'], function ($routes) {'."\n"; - $assert .= "\t".'$routes->extensions([\'0\' => \'json\', \'1\' => \'xml\']);'."\n"; - $assert .= "\t".'$routes->connect(\'/\', [\'plugin\' => \'PluginCars\', \'controller\' => \'Cars\', \'action\' => \'index\'], [\'_name\' => \'cars\']);'."\n"; - $assert .= "\t".'$routes->connect(\'/bmws\', [\'path\' => \'/bmws\', \'controller\' => \'Bmws\'], [\'_name\' => \'bmws_list\']);'."\n"; - $assert .= "\t".'$routes->connect(\'/bmws/:id\', [\'_method\' => \'GET\', \'path\' => \'/bmws/{id}\', \'controller\' => \'Bmws\', \'action\' => \'view\'], [\'_name\' => \'bmws_view\', \'pass\' => [\'0\' => \'id\'], \'id\' => \'[0-9]+\']);'."\n"; - $assert .= "\t".'$routes->connect(\'/bmws/add\', [\'_method\' => \'POST\', \'path\' => \'/bmws/add\', \'controller\' => \'Bmws\', \'action\' => \'add\'], [\'_name\' => \'bmws_add\']);'."\n"; - $assert .= "\t".'$routes->connect(\'/ladas\', [\'path\' => \'/ladas\', \'controller\' => \'Ladas\'], [\'_name\' => \'ladas\']);'."\n"; - $assert .= "\t".'$routes->fallbacks(\'DashedRoute\');'."\n"; - $assert .= '});'."\n\n"; + $assert = '\Cake\Routing\Router::plugin(\'PluginCars\', [\'path\' => \'/cars\'], function ($routes) {' . "\n"; + $assert .= "\t" . '$routes->extensions([\'0\' => \'json\', \'1\' => \'xml\']);' . "\n"; + $assert .= "\t" . '$routes->connect(\'/\', [\'plugin\' => \'PluginCars\', \'controller\' => \'Cars\', \'action\' => \'index\'], [\'_name\' => \'cars\']);' . "\n"; + $assert .= "\t" . '$routes->connect(\'/bmws\', [\'path\' => \'/bmws\', \'controller\' => \'Bmws\'], [\'_name\' => \'bmws_list\']);' . "\n"; + $assert .= "\t" . '$routes->connect(\'/bmws/:id\', [\'_method\' => \'GET\', \'path\' => \'/bmws/{id}\', \'controller\' => \'Bmws\', \'action\' => \'view\'], [\'_name\' => \'bmws_view\', \'pass\' => [\'0\' => \'id\'], \'id\' => \'[0-9]+\']);' . "\n"; + $assert .= "\t" . '$routes->connect(\'/bmws/add\', [\'_method\' => \'POST\', \'path\' => \'/bmws/add\', \'controller\' => \'Bmws\', \'action\' => \'add\'], [\'_name\' => \'bmws_add\']);' . "\n"; + $assert .= "\t" . '$routes->connect(\'/ladas\', [\'path\' => \'/ladas\', \'controller\' => \'Ladas\'], [\'_name\' => \'ladas\']);' . "\n"; + $assert .= "\t" . '$routes->fallbacks(\'DashedRoute\');' . "\n"; + $assert .= '});' . "\n\n"; $assert .= '\Cake\Core\Plugin::routes();'; Plugin::getInstance()->load('PluginCars', ['bootstrap' => false, 'routes' => true]); Generator::getInstance()->run(true); - $res = Generator::getDump(); + $res = Generator::getInstance()->getDump(); $this->assertEquals($assert, $res); } }