Exemplo n.º 1
0
 /**
  * @param Request $request
  * @param Response $response
  */
 public function render(Request $request, Response $response)
 {
     if (!$this->environment->isSilent()) {
         header('HTTP/1.1 ' . $response->getStatusCode() . ' ' . $response->getStatusMessage());
         foreach ($response->getHeaders() as $name => $value) {
             header($name . ': ' . $value);
         }
     }
     if ($response->getStatusCode() < 299) {
         $startRange = 0;
         $endRange = filesize($response->getContent()) - 1;
         try {
             $contentRange = $response->getHeader('Content-Range');
             if (preg_match('/^bytes ([0-9]+)-([0-9]+)\\/([0-9]+)$/', $contentRange, $match)) {
                 $startRange = (int) $match[1];
                 $endRange = (int) $match[2];
             }
         } catch (HeaderNotFoundException $e) {
             //skipp
         }
         $buffer = 1024 * 8;
         $file = @fopen($response->getContent(), 'rb');
         fseek($file, $startRange);
         while (!feof($file) && ($p = ftell($file)) <= $endRange) {
             if ($p + $buffer > $endRange) {
                 $buffer = $endRange - $p + 1;
             }
             set_time_limit(0);
             echo fread($file, $buffer);
             flush();
         }
         fclose($file);
     }
 }
Exemplo n.º 2
0
 /**
  *
  * @param Environment $environment
  */
 public function __construct(Environment $environment)
 {
     if (!$environment->isSilent()) {
         session_start();
         $this->id = session_id();
     } else {
         //test
         $this->id = mt_rand(1, 10000000);
     }
 }
Exemplo n.º 3
0
 /**
  *
  * @param Request $request
  * @param Response $response
  */
 public function render(Request $request, Response $response)
 {
     if ($this->environment->getName() !== 'test') {
         header('HTTP/1.1 ' . $response->getStatusCode() . ' ' . $response->getStatusMessage());
         foreach ($response->getHeaders() as $name => $value) {
             header($name . ': ' . $value);
         }
     }
     echo (string) $response->getContent();
 }
Exemplo n.º 4
0
 /**
  *
  * @param Environment $environment
  * @param EventManager $eventManager
  * @param string $driver
  * @param string $username
  * @param string $password
  * @param string $dbname
  * @param string $host
  * @param int $port
  */
 public function __construct(Environment $environment, EventManager $eventManager, $driver, $username, $password, $dbname, $host, $port)
 {
     $paths = array();
     $eventHandler = new EventHandler($eventManager);
     $dbParams = array('driver' => $driver, 'user' => $username, 'password' => $password, 'dbname' => $dbname, 'host' => $host, 'charset' => 'utf8');
     $config = Setup::createAnnotationMetadataConfiguration($paths, $environment->isDebug(), $environment->getCachePath());
     $this->entityManager = EntityManager::create($dbParams, $config);
     $this->entityManager->getEventManager()->addEventListener(array('onFlush'), $eventHandler);
     $this->entityManager->getEventManager()->addEventListener(array('postFlush'), $eventHandler);
 }
Exemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function execute(\Exception $exception)
 {
     error_log($exception->getMessage() . " " . $exception->getFile() . "(" . $exception->getLine() . ")");
     if (!$this->environment->isDebug()) {
         return;
     }
     $presenter = new HTTPErrorPresenter($this->environment);
     $response = new Response();
     $response->setContent($exception);
     $request = new EmptyRequest('', null);
     $presenter->render($request, $response);
 }
Exemplo n.º 6
0
 /**
  *
  * @param Response $response
  */
 private function setHeaders(Response $response)
 {
     if ($this->environment->getName() === 'test') {
         return;
     }
     header('HTTP/1.1 ' . $response->getStatusCode() . ' ' . $response->getStatusMessage());
     header('Content-type: application/json');
     foreach ($response->getHeaders() as $name => $value) {
         if ($name != 'content-type') {
             header($name . ': ' . $value);
         }
     }
 }
Exemplo n.º 7
0
 /**
  * Render view.
  *
  * @param string $template
  * @param mixed[] $data
  * @return string
  */
 public function render($template, $data = [])
 {
     $loader = new \Twig_Loader_Filesystem($this->environment->getRootPath() . '/template');
     $twig = new \Twig_Environment($loader);
     $extensionDir = $this->environment->getSrcPath() . '/Twig/Extension';
     if (file_exists($extensionDir)) {
         $hDir = opendir($extensionDir);
         while ($file = readdir($hDir)) {
             if ($file == '.' || $file == '..') {
                 continue;
             }
             $objName = '\\Twig\\Extension\\' . pathinfo($file, PATHINFO_FILENAME);
             $twig->addExtension(new $objName($data));
         }
         closedir($hDir);
     }
     return $twig->render($template, $data);
 }
Exemplo n.º 8
0
 /**
  * Support status code 4XX-5XX.
  *
  * @param Request $request
  * @param Response $response
  */
 private function displayError(Request $request, Response $response)
 {
     $exception = $response->getContent();
     $data = array('statusCode' => $response->getStatusCode(), 'message' => $exception->getMessage(), 'exception' => get_class($exception), 'file' => $exception->getFile(), 'line' => $exception->getLine());
     if ($this->environment->isDebug()) {
         echo $this->twig->render('error.twig', $data);
     } else {
         echo $this->twig->render($response->getStatusCode() . '.twig');
     }
 }
Exemplo n.º 9
0
 /**
  * Init framework config
  */
 private function initConfig()
 {
     //config structure
     $structureConfig = new ConfigBuilder();
     $xmlReader = new XmlFileReader($this->environment->getConfigPath() . '/structure.xml');
     $structureConfig->addReader($xmlReader);
     $structureNode = new ConfigBuilderNode('structure');
     $structureNode->addAttribute('class');
     $structureConfig->addNode($structureNode);
     $structureContainer = $structureConfig->parse();
     $xmlReader = new XmlFileReader($this->environment->getConfigPath() . '/' . $this->environment->getName() . '.xml');
     $importConfig = new ConfigBuilder();
     $importConfig->addReader($xmlReader);
     //config import
     $importNode = new ConfigBuilderNode('import');
     $importNode->addAttribute('file');
     $importConfig->addNode($importNode);
     $importContainer = $importConfig->parse();
     //config main
     $mainConfig = new ConfigBuilder();
     foreach ($importContainer->getNodes('import') as $importNode) {
         /**
          * @var ConfigContainer $importNode
          */
         $xmlReader = new XmlFileReader($this->environment->getConfigPath() . '/' . $importNode->getAttribute('file'));
         $mainConfig->addReader($xmlReader);
     }
     foreach ($structureContainer->getNodes('structure') as $structureNode) {
         /**
          * @var ConfigContainer $structureNode
          */
         $className = $structureNode->getAttribute('class');
         /**
          * @var Structure $structureObj
          */
         $structureObj = new $className();
         $structureObj->doConfig($mainConfig);
     }
     $this->config = new Config($mainConfig->parse());
 }
Exemplo n.º 10
0
 /**
  *
  * @return string
  */
 private function getPatternPath()
 {
     return $this->environment->getRootPath() . $this->patternPath;
 }