Exemplo n.º 1
0
 public function __construct()
 {
     // Set PhrestDI - Always construct first to set up the app
     parent::__construct();
     // Set up services
     $this->setUpServices();
     // Run final functions before app runs
     return $this->finalize();
 }
Exemplo n.º 2
0
 /**
  * @param PhrestDI    $di
  * @param null|string $srcDir
  */
 public function __construct(PhrestDI $di, $srcDir = null)
 {
     // Set the applications src directory
     if (!$srcDir) {
         // Assume the src directory based on standard structure
         $srcDir = dirname(dirname(dirname(dirname(__DIR__)))) . '/src';
     }
     $this->srcDir = $srcDir;
     // Collections are how we handler our routes
     $di->set('collections', function () {
         return $this->getCollections();
     });
     // Set the Exception handler
     $this->setExceptionHandler($di);
     $this->setDI($di);
     // Handle a 404
     $this->notFound(function () use($di) {
         // Method
         if (PhrestSDK::$method && PhrestSDK::$uri) {
             // Set exception message
             $message = sprintf('Route not found: %s (via SDK) to %s', PhrestSDK::$method, PhrestSDK::$uri);
         } else {
             // Set exception message
             /** @var PhrestRequest $request */
             $request = $di->get('request');
             $message = sprintf('Route not found: %s to %s', $request->getMethod(), $request->getURI());
         }
         throw new HandledException($message, 404);
     });
     // Mount all of the collections, which makes the routes active.
     foreach ($di->get('collections') as $collection) {
         $this->mount($collection);
     }
     $this->getResponseType();
     // Check unauthorized 401 access
     $this->before(function () use($di) {
         // If the access is unauthorized:
         //throw new Exceptions\UnauthorizedException;
     });
     // Send the response if required
     $this->after(function () use($di) {
         // Internal request will return the response
         if ($this->isInternalRequest) {
             return;
         }
         $controllerResponse = $this->getReturnedValue();
         if (is_a($controllerResponse, 'Phrest\\API\\Responses\\ResponseArray')) {
             /** @var $controllerResponse \Phrest\API\Response\ResponseArray */
             $controllerResponse->setCount($controllerResponse->getCount());
         }
         //var_dump($controllerResponse);
         /** @var PhrestRequest $response */
         $request = $di->get('request');
         if ($request->isJSON() || !$request->getFormat()) {
             $di->set('response', new JSONResponse($controllerResponse));
         } elseif ($request->isCSV()) {
             $di->set('response', new CSVResponse($controllerResponse));
         }
         /** @var Response $response */
         $response = $di->get('response');
         $response->send();
     });
 }