/**
  * @return \Cawa\App\Module|Module
  */
 public function module()
 {
     if (!$this->module) {
         $this->module = AbstractApp::instance()->getModule('Cawa\\SwaggerServer\\Module');
     }
     return $this->module;
 }
 /**
  * @param \Throwable $exception
  *
  * @return bool
  */
 public function exceptionHandler(\Throwable $exception)
 {
     if ($exception instanceof ResponseCode) {
         try {
             $out = $this->render($exception->getCode(), [], $exception->display());
         } catch (\Throwable $exception) {
             return $this->exceptionHandler($exception);
         }
         // debug on dev / display trace
         if (!(AbstractApp::env() != AbstractApp::PRODUCTION && ob_get_length() > 0)) {
             self::response()->addHeader('Content-Type', $this->getErrorContentType());
         }
         self::response()->setStatus($exception->getCode());
         self::response()->setBody($out);
         HttpApp::instance()->end();
     } else {
         Handler::log($exception);
         if (AbstractApp::env() != AbstractApp::PRODUCTION) {
             Handler::exceptionHandler($exception);
         } else {
             $throw = new ResponseCode($exception->getMessage(), 500, $exception);
             $this->exceptionHandler($throw);
         }
     }
     return true;
 }
Esempio n. 3
0
 /**
  * @return void
  */
 public function end()
 {
     parent::end();
     echo self::response()->send();
     $exitCode = self::response()->getStatus() >= 500 && self::response()->getStatus() < 600 ? 1 : 0;
     exit($exitCode);
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function end()
 {
     parent::end();
     if (self::$exitCode > 255) {
         self::$exitCode = 255;
     }
     exit(self::$exitCode);
 }
Esempio n. 5
0
 /**
  * @return string
  */
 public function end()
 {
     AbstractApp::end();
     $out = ob_get_clean();
     if ($out) {
         self::response()->setBody($out . self::response()->getBody());
     }
     return self::response()->send();
 }
Esempio n. 6
0
 /**
  * @param string $templatePath
  * @param array $data
  *
  * @return string
  */
 public static function renderTemplate(string $templatePath, array $data = []) : string
 {
     if (substr($templatePath, 0, 1) != '/') {
         $templatePath = AbstractApp::getAppRoot() . '/' . $templatePath;
     }
     $phtml = new static($templatePath);
     $phtml->addDatas($data);
     return $phtml->render();
 }
Esempio n. 7
0
 /**
  * @return string
  */
 public function render()
 {
     if (!self::$renderer) {
         $loader = new Twig_Loader_Filesystem();
         $loader->prependPath('/');
         $twig = new Twig_Environment($loader, ['cache' => AbstractApp::getAppRoot() . '/cache/twig']);
         self::$renderer = $twig;
     }
     if (empty($this->templatePath)) {
         $this->setTemplatePath();
     }
     $template = self::$renderer->loadTemplate($this->templatePath . '.twig');
     return $template->render($this->getData());
 }
Esempio n. 8
0
 /**
  * @param string $service
  *
  * @return string
  */
 public function end(string $service)
 {
     $provider = AbstractProvider::create($service);
     $user = $provider->getUser();
     self::session()->set(Module::SESSION_NAME, $user);
     self::session()->remove(SessionStorage::SESSION_VAR_STATE);
     self::session()->remove(SessionStorage::SESSION_VAR_TOKEN);
     /* @var \Cawa\Oauth\Module $module */
     $module = AbstractApp::instance()->getModule('Cawa\\Oauth\\Module');
     $url = self::session()->get(Module::SESSION_FROM);
     self::session()->remove(Module::SESSION_FROM);
     if (!$user instanceof User) {
         $url = null;
     }
     if (!$url) {
         $url = self::uri($module->getRedirectRoute());
     }
     self::response()->redirect($url);
 }
Esempio n. 9
0
 /**
  * @param \Throwable $exception
  *
  * @return bool
  */
 public static function exceptionHandler(\Throwable $exception)
 {
     // This error code is not included in error_reporting
     if (!error_reporting() || $exception->getLine() == 0) {
         return;
     }
     if ('cli' === PHP_SAPI || isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'curl') !== false) {
         $formatterClass = '\\Cawa\\Error\\Formatter\\CliFormatter';
     } else {
         $formatterClass = '\\Cawa\\Error\\Formatter\\HtmlFormatter';
     }
     if (AbstractApp::isInit() && AbstractApp::instance() instanceof HttpApp) {
         self::log($exception);
         self::response()->setStatus(500);
         if (AbstractApp::env() != AbstractApp::PROD || Ip::isAdmin()) {
             $formatter = new $formatterClass();
             self::render($formatter, $exception);
         } else {
             self::clearAllBuffer();
             echo self::router()->returnError(500);
         }
         AbstractApp::instance()->end();
     } else {
         if (!headers_sent()) {
             header('HTTP/1.1 500 Internal Server Error');
         }
         if (AbstractApp::env() != AbstractApp::PROD) {
             $formatter = new $formatterClass();
             self::render($formatter, $exception);
         } else {
             self::clearAllBuffer();
             echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">' . "\n" . '<html><head>' . "\n" . '<title>' . self::response()->getStatusString(500) . '</title>' . "\n" . '</head><body>' . "\n" . '<h1>' . self::response()->getStatusString(500) . '</h1>' . "\n" . '</body></html>';
         }
     }
 }
Esempio n. 10
0
 /**
  * @param string $name
  * @param string $rename
  * @param bool $appendLang
  *
  * @return bool
  */
 public function addFile(string $name, string $rename = null, bool $appendLang = true) : bool
 {
     if (substr($name, 0, 1) == '/') {
         $path = $name;
         if (is_null($rename)) {
             throw new \LogicException(sprintf("Missing rename parameter on '%s'", $name));
         }
         $name = $rename;
     } else {
         $path = AbstractApp::getAppRoot() . '/lang/' . $name;
     }
     if ($appendLang) {
         $path .= '.' . $this->locale . '.php';
     } else {
         $path .= '.php';
     }
     if (!file_exists($path)) {
         throw new \InvalidArgumentException(sprintf("Invalid locale files path '%s'", $name));
     }
     /* @noinspection PhpIncludeInspection */
     $data = (require $path);
     if (!is_array($data)) {
         throw new \LogicException(sprintf("Invalid locale files '%' format, must be a php array", $path));
     }
     $this->translations[$name] = $data;
     return true;
 }
Esempio n. 11
0
 /**
  * @param string $file
  * @param string $extension
  * @param string $extensionFrom
  * @param int $width
  * @param int $height
  * @param string $position
  * @param string $effect
  *
  * @return string
  */
 public function resize(string $file, string $extension, string $extensionFrom = null, int $width = null, int $height = null, string $position = null, string $effect = null) : string
 {
     $options = class_exists('Imagick') ? ['driver' => 'imagick'] : [];
     $manager = new ImageManager($options);
     $path = $_SERVER['DOCUMENT_ROOT'] . $file . '.' . ($extensionFrom ? $extensionFrom : $extension);
     $timerEvent = new TimerEvent('image.make', ['path' => $path]);
     if (!file_exists($path)) {
         self::response()->setStatus(404);
         $img = $manager->make(dirname(__DIR__) . '/assets/404.png');
     } else {
         $img = $manager->make($path);
     }
     $timerEvent->addData(['width' => $img->width(), 'heigth' => $img->height(), 'size' => $img->filesize()]);
     self::emit($timerEvent);
     if (!$height) {
         $height = round($width * $img->height() / $img->width());
     }
     if (!$width) {
         $width = round($height * $img->width() / $img->height());
     }
     $timerEvent = new TimerEvent('image.resize');
     $interlace = DI::config()->getIfExists('image/interlace');
     $interlace = is_null($interlace) ? true : $interlace;
     $sharpen = DI::config()->getIfExists('image/sharpen');
     $sharpen = is_null($sharpen) ? 5 : $sharpen;
     $quality = DI::config()->getIfExists('image/quality');
     $timerEvent->addData(['width' => $width, 'heigth' => $height]);
     $positions = [];
     if ($position) {
         foreach (str_split($position) as $letter) {
             if ($letter == 't') {
                 $positions[] = 'top';
             } elseif ($letter == 'b') {
                 $positions[] = 'bottom';
             } elseif ($letter == 'l') {
                 $positions[] = 'left';
             } elseif ($letter == 'r') {
                 $positions[] = 'right';
             }
         }
     }
     $encoded = $img->fit($width, $height, null, sizeof($positions) > 0 ? implode('-', $positions) : null);
     self::emit($timerEvent);
     if ($interlace) {
         $timerEvent = new TimerEvent('image.effect', ['type' => 'interlace']);
         $encoded->interlace();
         self::emit($timerEvent);
     }
     if ($sharpen) {
         $timerEvent = new TimerEvent('image.effect', ['type' => 'sharpen']);
         $encoded->sharpen($sharpen);
         self::emit($timerEvent);
     }
     if ($effect) {
         /* @var \Cawa\ImageModule\Module $module */
         $module = AbstractApp::instance()->getModule('Cawa\\ImageModule\\Module');
         foreach (explode('-', $effect) as $currentEffect) {
             $timerEvent = new TimerEvent('image.effect', ['type' => $currentEffect]);
             $filter = $module->getEffect($currentEffect);
             $encoded->filter($filter);
             self::emit($timerEvent);
         }
     }
     $encoded = $encoded->encode($extension, $quality);
     self::response()->addHeader('Content-Type', $encoded->mime());
     self::response()->addHeader('Content-Length', (string) strlen($encoded->getEncoded()));
     return $encoded->getEncoded();
 }