/** * /user/login should match /user/login before /user/:id or /user/:name **/ public function testStaticOverDynamic() { $router = new Frapi_Router(); $routesToPrepare = array("user-id-login" => "/user/:id/login", "user" => "/user/", "user-id" => "/user/:id", "user-login" => "/user/login", "user-logout" => "/user/logout", "container-get" => "/container/get/:id", "container-get-all" => "/container/get/all"); $router->setPreparedRoutes(Frapi_Router::prepareRoutes($routesToPrepare)); $this->assertEquals("user", end($router->match("/user"))); $this->assertEquals("user-id", end($router->match("/user/1234"))); $this->assertEquals("user-login", end($router->match("/user/login/"))); $this->assertEquals("user-logout", end($router->match("/user/logout"))); $this->assertEquals("container-get-all", end($router->match("/container/get/all"))); $this->assertEquals("container-get", end($router->match("/container/get/12345"))); }
/** * /user/login should match /user/login before /user/:id or /user/:name **/ public function testStaticOverDynamic() { $router = new Frapi_Router(); $routesToPrepare = array("user-id-login" => "/user/:id/login", "user" => "/user/", "user-id" => "/user/:id", "user-login" => "/user/login", "user-logout" => "/user/logout", "container-get" => "/container/get/:id", "container-get-all" => "/container/get/all", "container-get-all-wild" => '/container/get/all/*'); $router->setPreparedRoutes(Frapi_Router::prepareRoutes($routesToPrepare)); $user = $router->match("/user"); $user_id = $router->match("/user/1234"); $user_login = $router->match("/user/login/"); $user_logout = $router->match("/user/logout"); $container_get = $router->match("/container/get/12345"); $container_get_all = $router->match("/container/get/all"); $container_get_all_wild = $router->match("/container/get/all/testing/name/is"); $this->assertEquals("user", end($user)); $this->assertEquals("user-id", end($user_id)); $this->assertEquals("user-login", end($user_login)); $this->assertEquals("user-logout", end($user_logout)); $this->assertEquals("container-get-all", end($container_get_all)); $this->assertEquals("container-get", end($container_get)); $this->assertEquals("container-get-all-wild", end($container_get_all_wild)); }
/** * Update an action * * This method updates an action using data passed * to the $data method parameter. * * @param array $data The data array to update the action with. * @param string $id An hash that contains the id of the action to update. * @return boolean true */ public function update(array $data, $id) { $whitelist = array('name', 'enabled', 'description', 'public', 'param', 'required', 'route', 'use_custom_route'); $this->whiteList($whitelist, $data); // Replace spaces with underscores, to attempt to make it a valid name $data['name'] = ucfirst(strtolower(str_replace(' ', '_', $data['name']))); // Validate the action doesn't already exist and is a valid name $tempAction = $this->get($id); if ($tempAction['name'] != $data['name'] && in_array($data['name'], $this->getList())) { throw new RuntimeException('There is already an action with this name.'); } else { if (!preg_match('/^[A-Z][a-zA-Z0-9\\_\\-]+$/', $data['name'])) { throw new RuntimeException('Action name does not validate. Please ensure it contains only alpha-numeric characters, underscores and dashes.'); } } // Routes can only be lower case! $data['route'] = strtolower($data['route']); // Validate the route does not already exist and is valid if ($tempAction['route'] != $data['route']) { // Validate the route does not already exist and is valid $router = new Frapi_Router(); $router->loadAndPrepareRoutes(); if ($router->match($data['route'])) { throw new RuntimeException('There is already an action with this route.'); } } $segments = Frapi_Router::parseSegments($data['route']); foreach ($segments as $key => $value) { if ($key == 0) { if (!preg_match('/^[a-z0-9\\-\\_\\*]+$/', $value)) { throw new RuntimeException('Action route does not validate. Please ensure each part contains only alpha-numeric characters, underscores, dashes and colons.'); } } else { if (!preg_match('/^:?[a-z0-9\\-\\_\\*]+$/', $value)) { throw new RuntimeException('Action route does not validate. Please ensure each part contains only alpha-numeric characters, underscores, dashes and colons.'); } } } $values = array('name' => $data['name'], 'enabled' => $data['enabled'], 'public' => $data['public'], 'description' => $data['description'], 'route' => $data['route']); $values['parameters'] = array(); if (isset($data['param'])) { $params = $data['param']; foreach ($params as $param => $value) { if (strlen(trim($data['param'][$param])) <= 0) { continue; } $values['parameters']['parameter'][] = array('name' => $data['param'][$param], 'required' => isset($data['required'][$param]) ? '1' : '0'); } } /* * If we have no parameters we still need a <parameters> entry in the * config file. */ if (!count($values['parameters'])) { $values['parameters'] = ''; } try { $this->config->update('action', 'hash', $id, $values); } catch (Exception $e) { } $this->refreshAPCCache(); return true; }