/** * Load route config * * @param $config * * @return array */ private function _loadRouteConfig($config) { if (strpos($config, '.') !== false) { list($plugin, $file) = explode('.', $config); $pluginPaths = Plugin::getInstance()->getLoaded(); $path = Configure::read('App.paths.plugins')[0] . $pluginPaths[$plugin]['name'] . DS . 'config' . DS . $file . '.yml'; } else { $path = ROOT . DS . 'config' . DS . $config . '.yml'; } return Yaml::parse(file_get_contents($path)); }
/** * Basic integration test * * @throws \makallio85\YamlRoute\Exception\GeneratorException * @throws \makallio85\YamlRoute\Exception\ValidatorException * @throws \makallio85\YamlRoute\Exception\PluginException */ public function testIntegration() { $assert = '\\Cake\\Routing\\Router::defaultRouteClass(\'DashedRoute\');' . "\n\n"; $assert .= '\\Cake\\Routing\\Router::scope(\'/\', [], function ($routes) {' . "\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\', [\'controller\' => \'Bmws\', \'foo\' => \'bar\'], [\'_name\' => \'bmws_list\', \'pass\' => [\'0\' => \'foo\']]);' . "\n"; $assert .= "\t" . '$routes->connect(\'/bmws/:id\', [\'_method\' => \'GET\', \'controller\' => \'Bmws\', \'action\' => \'view\'], [\'_name\' => \'bmws_view\', \'pass\' => [\'0\' => \'id\'], \'id\' => \'[0-9]+\']);' . "\n"; $assert .= "\t" . '$routes->connect(\'/bmws/add\', [\'_method\' => \'POST\', \'controller\' => \'Bmws\', \'action\' => \'add\'], [\'_name\' => \'bmws_add\']);' . "\n"; $assert .= "\t" . '$routes->connect(\'/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::getInstance()->getDump(); $this->assertEquals($assert, $res); }
/** * New route * * @param $name * @param $route */ private function _newRoute($name, $route) { $method = 'scope'; $path = '/'; $options = []; if (isset($route['config'])) { if (isset($route['config']['plugin']) && Plugin::getInstance()->isLoaded($route['config']['plugin'])) { $method = 'plugin'; $path = $route['config']['plugin']; $options = ['path' => $route['path']]; } } // Set default route class if (isset($route['config']['default_route_class'])) { if (in_array($route['config']['default_route_class'], ['Route', 'InflectedRoute', 'DashedRoute'])) { Router::defaultRouteClass($route['config']['default_route_class']); $this->_addToDump("\\Cake\\Routing\\Router::defaultRouteClass('" . $route['config']['default_route_class'] . "');\n"); } } // Debugging if ($this->_debug) { $this->_addToDump("\\Cake\\Routing\\Router::{$method}('{$path}', " . $this->_arrToStr($options) . ", function (" . '$routes' . ") {"); } Router::$method($path, $options, function ($routes) use($route, $name, $method) { $exclude = ['validate', 'routes', 'extensions', 'default_route_class', 'path']; if (isset($route['config'])) { if (isset($route['config']['extensions']) && is_array($route['config']['extensions'])) { /* @var \Cake\Routing\Router $routes */ $routes->extensions($route['config']['extensions']); if ($this->_debug) { $this->_addToDump("\t" . '$routes->extensions(' . $this->_arrToStr($route['config']['extensions']) . ');'); } } if (isset($route['config']['controller'])) { $opts = []; foreach ($route['config'] as $key => $item) { if (!in_array($key, $exclude)) { if (is_array($item) && $key === 'pass') { foreach ($item as $i => $y) { $opts[$i] = $y; } } else { $opts[$key] = $item; } } } $route = $this->_createPassParams($route); $thirdParam = $this->_buildThirdParam($name, $route); $path = $method == 'scope' ? $route['path'] : '/'; /* @var \Cake\Routing\Router $routes */ $routes->connect($path, $opts, $thirdParam); // Debugging if ($this->_debug) { $this->_addToDump("\t" . '$routes->connect(\'' . $path . '\', ' . $this->_arrToStr($opts) . ', ' . $this->_arrToStr($thirdParam) . ');'); } } if (isset($route['config']['routes'])) { foreach ($route['config']['routes'] as $key => $x) { if (isset($x['config'])) { if (isset($x['config']['extensions']) && is_array($x['config']['extensions'])) { /* @var \Cake\Routing\Router $routes */ $routes->extensions($x['config']['extensions']); if ($this->_debug) { $this->_addToDump("\t" . '$routes->extensions(' . $this->_arrToStr($x['config']['extensions']) . ');'); } } $opts = []; foreach ($x['config'] as $k => $item) { if (!in_array($k, $exclude)) { if (is_array($item) && $k === 'pass') { foreach ($item as $i => $y) { $opts[$i] = $y; } } else { $opts[$k] = $item; } } } $x = self::_createPassParams($x); $thirdParam = $this->_buildThirdParam($key, $x); /* @var \Cake\Routing\Router $routes */ $routes->connect('/' . $this->_varsToString($x['path']), $opts, $thirdParam); // Debugging if ($this->_debug) { $this->_addToDump("\t" . '$routes->connect(\'' . $this->_varsToString($x['path']) . '\', ' . $this->_arrToStr($opts) . ', ' . $this->_arrToStr($thirdParam) . ');'); } } } } } $this->_createFallbacks($routes, $route); }); // Debugging if ($this->_debug) { $this->_addToDump('});' . "\n"); } }
/** * @expectedException \makallio85\YamlRoute\Exception\PluginException */ public function testMissingRouteFile() { Plugin::getInstance()->load('PluginAnimals', ['bootstrap' => false, 'routes' => true]); }