Example #1
0
 /**
  * @controller-api
  */
 public function get(array $tplName)
 {
     $__variablesDefinitions = array();
     try {
         $tpl = new Template($tplName);
         $tpl->setLanguage(Config::req('i18n.language'));
         $tpl->setVars($__variablesDefinitions);
         if (!$tpl->validate()) {
             throw new \Psc\TPL\MissingFileException('Template ist nicht valid!');
         }
         if (!$tpl->getFile()->exists()) {
             throw new \Psc\TPL\MissingFileException('Template-File: ' . $tpl->getFile() . ' existiert nicht');
         }
         return $tpl->get();
     } catch (\Psc\TPL\MissingFileException $e) {
         throw HTTPException::NotFound('Das Template: ' . Code::varInfo($tplName) . ' ist nicht vorhanden', $e);
     }
 }
Example #2
0
 /**
  * FEHLER: Eine Resource die zum ausführen des Requests benötigt wird, wurde niicht gefunden
  *
  * z. B. Ein PUT Request wird für ein Entity gemacht welches nicht mehr vorhanden ist
  * für ein Entity soll ein Formular geladen werden, und der Identifier ergibt kein ergebnis in der Datenbank
  */
 public function resourceNotFound($method, $resourceType, $argumentValues, \Exception $e = NULL)
 {
     return HTTPException::NotFound(sprintf("Die Resource '%s' konnte nicht mit den Parametern %s gefunden werden. Der Request konnte nicht vollständig ausgeführt werden. Möglicherweise ist die Resource nicht mehr vorhanden.", $resourceType, Code::varInfo($argumentValues)), $e);
 }
Example #3
0
 /**
  * @return ServiceResponse
  */
 public function route(ServiceRequest $serviceRequest)
 {
     try {
         $this->service = $this->findService($serviceRequest);
         return $this->service->route($serviceRequest);
     } catch (\Psc\Form\ValidatorException $e) {
         $this->logError($e, $this->debugLevel, 2);
         throw HTTPException::BadRequest(sprintf("Beim Validieren der Daten ist ein Fehler aufgetreten. %s", $e->getMessage()), $e, array_merge(array('Content-Type' => 'text/html'), $this->metadataGenerator->validationError($e)->toHeaders()));
     } catch (\Psc\Form\ValidatorExceptionList $list) {
         $this->logError($list, $this->debugLevel, 2);
         throw $this->err->validationResponse($list, $this->request->accepts('application/json') ? ServiceResponse::JSON : ServiceResponse::HTML, $this->metadataGenerator);
     }
 }
Example #4
0
 public function routeController(ServiceRequest $request)
 {
     $r = $this->initRequestMatcher($request);
     $controller = $r->qmatchiRx('/^(tpl|excel|images|uploads|persona)$/i');
     if ($controller === 'tpl') {
         $x = 0;
         $tpl = array();
         while (!$r->isEmpty() && $x <= 10) {
             $tpl[] = $r->qmatchRx('/^([-a-zA-Z0-9_.]+)$/');
             $x++;
         }
         $controller = new TPLController($this->project);
         return array($controller, 'get', array($tpl));
     } elseif ($controller === 'excel') {
         $controller = new ExcelController($this->project);
         if ($request->getType() === Service::POST) {
             $body = $request->getBody();
             if ($r->part() === 'convert') {
                 // importieren
                 // im body können dann options stehen oder sowas
                 // der controller holt sich die excelFile selbst
                 return array($controller, 'convert', array(is_object($body) ? $body : new \stdClass()));
             } else {
                 // exportieren
                 return array($controller, 'create', array($body, $r->part()));
                 // nächste ist filename ohne endung
             }
         }
     } elseif ($controller === 'images') {
         $controller = new ImageController(ImageManager::createForProject($this->project, $this->getDoctrinePackage()->getEntityManager(), $this->getDoctrinePackage()->getModule()->getEntityName('Image')));
         if ($r->isEmpty() && $request->getType() === Service::POST && $request->hasFiles()) {
             // nimmt nur eine file, weil moep
             return array($controller, 'insertImageFile', array(current($request->getFiles()), $request->getBody()));
         } else {
             $method = 'getImage';
             $cacheAdapters = array('thumbnail');
             $params = array($r->qmatchiRX('/([a-z0-9]+)/'));
             // id or hash
             // filename immer am ende und optional
             $filename = NULL;
             if (\Psc\Preg::match($r->getLastPart(), '/[a-z0-9]+\\.[a-z0-9]+/i')) {
                 $filename = $r->pop();
             }
             /* gucken ob es eine Version des Images werden soll */
             if (in_array($r->part(), $cacheAdapters)) {
                 $method = 'getImageVersion';
                 $params[] = $r->matchNES();
                 // type
                 $params[] = $r->getLeftParts();
                 // parameter für den cache adapter
             }
             if ($filename) {
                 $params[] = $filename;
             }
             return array($controller, $method, $params);
         }
     } elseif ($controller === 'uploads') {
         $controller = new FileUploadController(UploadManager::createForProject($this->project, $this->dc));
         $this->log('upload-request method: ' . $request->getType());
         $this->log('  has files: ' . ($request->hasFiles() ? 'true' : 'false'), 1);
         if ($r->isEmpty() && $request->getType() === Service::POST && $request->hasFiles()) {
             // nimmt nur eine file, weil moep
             return array($controller, 'insertFile', array(current($request->getFiles()), $request->getBody()));
         } elseif ($r->isEmpty() && $request->getType() === Service::GET) {
             $params = array();
             // criterias
             $params[] = array();
             $params[] = $r->matchOrderBy($r->qVar('orderby'), array('name' => 'originalName'));
             return array($controller, 'getFiles', $params);
         } else {
             $method = 'getFile';
             $params = array($r->qmatchiRX('/([a-z0-9]+)/'));
             // id or hash
             // filename immer am ende und optional
             $filename = NULL;
             try {
                 if (\Psc\Preg::match($r->getLastPart(), '/^(.+)\\.(.+)$/')) {
                     $filename = $r->pop();
                 }
             } catch (\Webforge\Common\Exception $e) {
                 if (mb_strpos('.', $r->getLastPart()) !== FALSE) {
                     $filename = \Webforge\Common\System\File::safeName($r->pop());
                     $filename = Preg::replace($filename, '/_+/', '_');
                 }
             }
             if ($filename) {
                 $params[] = $filename;
             }
             return array($controller, $method, $params);
         }
     } elseif ($controller === 'persona') {
         $controller = new \Webforge\Persona\Controller();
         return array($controller, 'verify', array($r->bvar('assertion')));
     }
     throw HTTPException::NotFound('Die Resource für cms ' . $request . ' existiert nicht.');
 }
Example #5
0
 /**
  * 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);
 }
Example #6
0
 public static function provideServiceExceptions()
 {
     return array(array(\Psc\Net\HTTP\HTTPException::NotFound()), array(\Psc\Net\HTTP\HTTPException::BadRequest()), array(new \Exception('Manno')), array(new \Psc\Exception('I am faling')));
 }
Example #7
0
 public function validateController($controller, $method, array $params)
 {
     if (!is_object($controller) || !method_exists($controller, $method)) {
         throw HTTPException::NotFound('Methode ' . Code::getClass($controller) . '::' . $method . ' ist nicht definiert');
     }
 }