public function testRenderByFullPath()
 {
     $this->expectOutputString('view: index/index');
     $viewPath = FileFullPathBuilder::build('views' . DIRECTORY_SEPARATOR . 'index' . DIRECTORY_SEPARATOR . 'index.html.php');
     $tpl = new OpenView();
     $tpl->render($viewPath);
 }
Пример #2
0
 /**
  * @param int $statusCode
  * @param string $statusReasonPhrase
  * @param object $error
  * @param string $outputFormat
  * @return void
  */
 public function render($statusCode, $statusReasonPhrase, $error, $outputFormat = null)
 {
     $rootPath = Config::getString('hyperframework.web.error_view.root_path', '');
     if ($rootPath === '') {
         $rootPath = Config::getString('hyperframework.web.view.root_path', '');
         if ($rootPath === '') {
             $rootPath = 'views' . DIRECTORY_SEPARATOR . '_error';
         } else {
             $rootPath = FilePathCombiner::combine($rootPath, '_error');
         }
     }
     $files = [ViewPathBuilder::build($statusCode, $outputFormat), ViewPathBuilder::build('default', $outputFormat)];
     $rootPath = FileFullPathBuilder::build($rootPath);
     $path = null;
     foreach ($files as $file) {
         $file = FilePathCombiner::combine($rootPath, $file);
         if (file_exists($file)) {
             $path = $file;
             break;
         }
     }
     if ($path === null) {
         Response::setHeader('Content-Type: text/plain; charset=utf-8');
         echo $statusCode;
         if ((string) $statusReasonPhrase !== '') {
             echo ' ', $statusReasonPhrase;
         }
     } else {
         $view = ViewFactory::createView(['status_code' => $statusCode, 'status_reason_pharse' => $statusReasonPhrase, 'error' => $error]);
         $view->render($path);
     }
 }
 /**
  * @param string $path
  * @param int $mode
  * @return bool
  */
 public static function make($path, $mode = 0755)
 {
     $fullPath = FileFullPathBuilder::build($path);
     if (is_dir($fullPath) === false) {
         try {
             if (mkdir($fullPath, $mode, true) === false) {
                 if (is_dir($fullPath) === false) {
                     throw new RuntimeException("Failed to create directory '{$fullPath}'.");
                 }
             } else {
                 return true;
             }
         } catch (ErrorException $e) {
             if (is_dir($directory) === false) {
                 throw $e;
             }
         }
     }
     return false;
 }
Пример #4
0
 /**
  * @param string $filePath
  * @param string $fileMode
  * @param string $lockType
  * @param Closure $callback
  * @return mixed
  */
 public static function run($filePath, $fileMode, $lockType, $callback)
 {
     $fullPath = FileFullPathBuilder::build($filePath);
     DirectoryMaker::make(dirname($fullPath));
     $handle = fopen($fullPath, $fileMode);
     if ($handle === false) {
         throw new RuntimeException("Failed to open or create file '{$fullPath}'.");
     }
     try {
         if (flock($handle, $lockType) === false) {
             throw new RuntimeException("Failed to lock file '{$fullPath}'.");
         }
         try {
             return $callback($handle);
         } finally {
             flock($handle, LOCK_UN);
         }
     } finally {
         fclose($handle);
     }
 }
 public function testBuildByEmptyPath()
 {
     $this->assertSame('/root', FileFullPathBuilder::build(''));
 }
Пример #6
0
 /**
  * @return string
  */
 private function getRootPath()
 {
     if ($this->rootPath === null) {
         $path = Config::getString('hyperframework.web.view.root_path', '');
         if ($path === '') {
             $path = 'views';
         }
         $this->rootPath = FileFullPathBuilder::build($path);
     }
     return $this->rootPath;
 }