コード例 #1
0
ファイル: EntityService.php プロジェクト: pscheit/psc-cms
 /**
  * Findet den Controller anhand des Requests
  * 
  * GET [/$prefix]/person/1
  *  =>
  * \Project::getNamespace()\Controllers\PersonController::getEntity(1)
  * =>
  * \CoC\Controllers\PersonController::getEntity(1)
  *
  * GET [/$prefix]/person/1/form
  * =>
  * \Project::getNamespace()\Controllers\PersonController::getEntity(1,'form')
  *
  * GET [/$prefix]/persons/grid?filter1=value1&filter2=value2
  * =>
  * \Project::getNamespace()\Controllers\PersonController::getEntities(array('filter1'=>'value1', 'filter2'=>'value2'),'grid')
  */
 public function routeController(ServiceRequest $request)
 {
     $r = $this->initRequestMatcher($request);
     $entityPart = $r->qmatchRx('/^[a-z-0-9]+$/i', 0);
     if (mb_strpos($entityPart, '-') !== FALSE) {
         $entityPart = Code::dashToCamelCase($entityPart);
     }
     // alle weiteren Parameter an den Controller weitergeben
     $params = $r->getLeftParts();
     if ($request->getType() === self::GET) {
         $this->log('EntityPart: ' . $entityPart . ' ' . ($this->isPlural($entityPart) ? 'ist plural' : 'ist singular'), 2);
         if ($this->isPlural($entityPart)) {
             if ($r->part() === 'form') {
                 $method = 'getNewEntityFormular';
             } else {
                 $method = 'getEntities';
                 A::insert($params, $request->getQuery(), 0);
                 // query als 1. parameter
             }
             $entityPart = Inflector::singular($entityPart);
         } else {
             $method = 'getEntity';
             $params = array();
             $params[] = $r->shift();
             // id
             $params[] = count($r->getLeftParts()) > 1 ? $r->getLeftParts() : $r->shift();
             // subresource
             $params[] = $request->getQuery();
         }
     } elseif ($request->getType() === self::PUT) {
         if ($r->part() === 'grid') {
             $entityPart = Inflector::singular($entityPart);
             $controller = $this->getEntityController($entityPart);
             $method = 'saveSort';
             $params = array($r->bvar($controller->getSortField(), array()));
         } elseif ($request->hasMeta('revision') && $request->getMeta('revision') !== 'default') {
             $method = 'saveEntityAsRevision';
             A::insert($params, $request->getMeta('revision'), 1);
             A::insert($params, (object) $request->getBody(), 2);
         } else {
             $method = 'saveEntity';
             A::insert($params, (object) $request->getBody(), 1);
             // $formData als parameter 2
         }
     } elseif ($request->getType() === self::PATCH) {
         $method = 'patchEntity';
         A::insert($params, (object) $request->getBody(), 1);
         // $formData als parameter 2
     } elseif ($request->getType() === self::DELETE) {
         $method = 'deleteEntity';
         // das gibt einen "missing argument 1" fehler, wenn id fehlt, aber ka welche httpException ich hier nehmensoll, deshalb bleibt das erstmal so
     } elseif ($request->getType() === self::POST) {
         $entityPart = Inflector::singular($entityPart);
         // singular und plural okay
         A::insert($params, $request->getBody(), 0);
         // $formData als parameter 1
         if ($request->hasMeta('revision')) {
             $method = 'insertEntityRevision';
             A::insert($params, $request->getMeta('revision'), 0);
         } else {
             $method = 'insertEntity';
         }
     } else {
         // das kann glaub ich nicht mehr passieren, weil wir jetzt alle haben: put/pust/delete/get gibts nicht noch head?
         throw HTTPException::MethodNotAllowed('Die Methode: ' . $request->getType() . ' ist für diesen Request nicht erlaubt');
     }
     if (!isset($controller)) {
         $controller = $this->getEntityController($entityPart);
     }
     return array($controller, $method, $params);
 }