コード例 #1
0
ファイル: Action.php プロジェクト: rjack/frapi
 /**
  * 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;
 }
コード例 #2
0
ファイル: RouterTest.php プロジェクト: crlang44/frapi
 /**
  * Test segments of route or path are parsed correctly
  * @dataProvider segmentsProvider
  **/
 public function testSegmentsParser($uri, $expectedSegments)
 {
     $this->assertEquals($expectedSegments, Frapi_Router::parseSegments($uri));
 }