/**
  * Returns the absolute path for the given template file path based on the given base directory.
  *
  * In case the given template file path starts with a slash ("/"), the template file is considered
  * to be relative to the module directory.
  * Otherwise the given template file path is considered to be relative to the given base path.
  *
  * @param string $templateFilePath Path of the template file to return the absolute path for
  * @param string $basePath Base path used to determine the absolute path for the given template file
  * @return string
  */
 public static function getAbsoluteTemplateFilePath($templateFilePath, $basePath)
 {
     // determine template directory
     $templateDirectory = StringUtil::startsWith($templateFilePath, '/') ? ABLERON_MODULE_DIR : FileUtil::normalizePath($basePath) . '/';
     // return absolute template file path
     return $templateDirectory . $templateFilePath;
 }
Beispiel #2
0
 public function testAppend()
 {
     $tempFile = new File(FileUtil::getTempFileName());
     $this->assertTrue($tempFile->append('foobar'));
     $this->assertSame('foobar', $tempFile->getContent());
     $this->assertTrue($tempFile->append('bazfoobar'));
     $this->assertSame('foobarbazfoobar', $tempFile->getContent());
 }
 /**
  * Checks whether the given directory exists and is writable.
  *
  * @param string $directory The directory to check
  * @param bool $createIfNotExists If TRUE, the directory will be created
  * @param int $directoryPermissions The permissions to apply to the directory when being created
  * @throws \Ableron\Core\Exception\SystemException In case the directory is not writable
  * @return void
  */
 public static function checkDirectoryIsWritable(string $directory, bool $createIfNotExists = true, int $directoryPermissions = 0775)
 {
     // create directory if needed
     if ($createIfNotExists && !FileUtil::createDirectoryIfNotExists($directory, $directoryPermissions)) {
         throw new SystemException(sprintf('Directory "%s" does not exist and could not be created', $directory), 0, E_USER_ERROR, __FILE__, __LINE__);
     }
     // check whether directory is writable
     if (!is_writable($directory)) {
         throw new SystemException(sprintf('Directory "%s" is not writable', $directory), 0, E_USER_ERROR, __FILE__, __LINE__);
     }
 }
 /**
  * Returns a directory iterator for the file storage.
  *
  * @return \Iterator
  */
 protected function getDirectoryIterator()
 {
     return FileUtil::getRecursiveChildFirstDirectoryIterator($this->directory);
 }
 /**
  * Returns the environment variable SCRIPT_FILENAME which represents the
  * absolute pathname of the currently executing script.
  *
  * @see http://stackoverflow.com/questions/5001326/php-strange-document-root
  * @return mixed
  */
 public static function getScriptFileName()
 {
     return CacheUtil::getFromCache(self::getCache(), __FUNCTION__, function () {
         if (self::isCliRequest()) {
             return self::getScriptName();
         }
         $scriptName = FileUtil::normalizePath(dirname(self::getScriptName()));
         while (!StringUtil::endsWith(ABLERON_ROOT_DIR, $scriptName) && $scriptName !== '/') {
             $scriptName = FileUtil::normalizePath(dirname($scriptName));
         }
         return ($scriptName === '/' ? ABLERON_ROOT_DIR : StringUtil::getSubstring(ABLERON_ROOT_DIR, 0, StringUtil::getLastIndexOf(ABLERON_ROOT_DIR, $scriptName))) . self::getScriptName();
     });
 }
 /**
  * Removes the installer.
  *
  * @return void
  */
 private function removeInstaller()
 {
     FileUtil::removeDirectory(ABLERON_INSTALL_DIR);
 }
 /**
  * Renders the given template file and returns the output.
  *
  * In case the template needs to be compiled first, triggers compilation and
  * writes the compiled template to a temporary file.
  *
  * Throws an exception in case an error occurred during rendering.
  *
  * @param string $sourceTemplateFile The file containing the source template to render
  * @param array $variables Variables which shall be available within the template during rendering
  * @param bool $sandboxMode If TRUE, only given variables will be available in the rendered template
  * @param \Ableron\Core\Template\TemplateRenderer The template renderer to use (if not set, a new template renderer will be instantiated for the job)
  * @throws \Ableron\Core\Exception\SystemException
  * @return string
  */
 public function render($sourceTemplateFile, $variables = array(), $sandboxMode = false, $renderer = null)
 {
     // get file where to save the compiled version of the given template
     $compiledTemplateFile = $this->getCompiledTemplateFile($sourceTemplateFile);
     // compile template if necessary
     if (!$this->isCompiled($sourceTemplateFile, $compiledTemplateFile)) {
         // compile template
         $compiledTemplate = $this->getCompiler()->compile($sourceTemplateFile, file_get_contents($sourceTemplateFile));
         // write compiled template to file
         if (!FileUtil::writeFile($compiledTemplateFile, $compiledTemplate)) {
             throw new SystemException(sprintf('Unable to write compiled template to file "%s"', $compiledTemplateFile), 0, E_USER_WARNING, __FILE__, __LINE__);
         }
     }
     // get renderer
     if ($renderer === null) {
         $renderer = new TemplateRenderer($this);
     }
     // render template and return result
     return $renderer->render($compiledTemplateFile, pathinfo($sourceTemplateFile, PATHINFO_DIRNAME), $sandboxMode ? $variables : array_merge($this->getVariables()->toArray(), $variables));
 }
Beispiel #8
0
 /**
  * Tests whether removeDirectory() works correctly.
  *
  * @return void
  */
 public function testRemoveDirectory()
 {
     FileUtil::createDirectoryIfNotExists(ABLERON_TEMP_DIR . '/testRemoveDirectory/foo/bar/baz');
     $file = new File(ABLERON_TEMP_DIR . '/testRemoveDirectory/foo/bar/bar.txt');
     $file->append('foobar');
     $this->assertTrue(is_file($file->getPath()));
     FileUtil::removeDirectory(ABLERON_TEMP_DIR . '/testRemoveDirectory');
     $this->assertFalse(is_file($file->getPath()));
     $this->assertFalse(is_dir(ABLERON_TEMP_DIR . '/testRemoveDirectory'));
 }
 /**
  * Checks whether isCompiled works as expected.
  *
  * @return void
  */
 public function testIsCompiled()
 {
     // get template handler, template file and compiled template file
     $templateHandler = $this->getTemplateHandler();
     $templateFile = FileUtil::getTempFileName();
     $templateFileCompiled = FileUtil::getTempFileName();
     // no compiled file exists
     $this->assertFalse($templateHandler->isCompiled($templateFile, $templateFileCompiled));
     // compiled file is older than template file
     touch($templateFile, time());
     touch($templateFileCompiled, time() - 1000);
     $this->assertFalse($templateHandler->isCompiled($templateFile, $templateFileCompiled));
     // compiled file is valid
     touch($templateFileCompiled, time() + 1000);
     $this->assertTrue($templateHandler->isCompiled($templateFile, $templateFileCompiled));
     // compiled file and template have the same modification time
     touch($templateFile, 1);
     touch($templateFileCompiled, 1);
     $this->assertTrue($templateHandler->isCompiled($templateFile, $templateFileCompiled));
 }
Beispiel #10
0
 /**
  * Returns a string representation of the given stack trace.
  *
  * @param array $trace The stack trace to format
  * @return string
  */
 public static function getTraceAsString(array $trace)
 {
     $currentTracePosition = count($trace);
     $formattedTrace = array();
     foreach ($trace as $tracePoint) {
         $formattedTrace[] = sprintf('#%d %s: %s%s(%s)', $currentTracePosition--, isset($tracePoint['file']) ? sprintf('%s(%d)', FileUtil::getRelativeAbleronPath($tracePoint['file']), $tracePoint['line']) : '[internal function]', isset($tracePoint['class']) ? $tracePoint['class'] . $tracePoint['type'] : '', $tracePoint['function'], !empty($tracePoint['args']) ? StringUtil::buildPhpArgumentString($tracePoint['args']) : '');
     }
     return implode(StringUtil::CHAR_LINE_FEED, $formattedTrace);
 }