/** * 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; }
/** * Initializes this storage. * * The given umask means "which permissions to revoke", e.g. if directory permissions * are 0777 and umask is 0002 then the actual directory permissions are 0775. * * Tries to create the given directory in case it does not exist already. * * @param string $directory The directory to store the files in * @param string $fileExtension The file extension of the stored files * @param int $umask The umask to apply to the permissions of created directories and files * @throws \Ableron\Core\Exception\SystemException In case the given directory is not writable */ public function __construct(string $directory, string $fileExtension = '', int $umask = 02) { parent::__construct(); // check whether directory is writable PreconditionUtil::checkDirectoryIsWritable($directory, true, 0777 & ~$umask); // set directory, file extension and umask $this->directory = FileUtil::normalizePath(realpath($directory)); $this->fileExtension = $fileExtension; $this->umask = $umask; }
/** * 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(); }); }
/** * Returns the file where to save the compiled version of the given source template. * * @param string $sourceTemplateFile The source template file for which to get the file of the compiled version * @throws \Ableron\Core\Exception\SystemException * @return string */ public function getCompiledTemplateFile($sourceTemplateFile) { // make sure "./" and "../" are handled correctly $sourceTemplateFile = StringUtil::contains($sourceTemplateFile, './') ? FileUtil::normalizePath(realpath($sourceTemplateFile)) : $sourceTemplateFile; // extract template information from path (module, area and template name) if (!preg_match('#/Modules/(?<module>[^/]+)(?<path>(/[^/]+)+?)/(?<name>[^/]+)\\.tpl$#', $sourceTemplateFile, $templateInfo)) { throw new SystemException(sprintf('Template file has invalid path: %s (Pattern of valid template file: .../<module>/**/<name>.tpl, e.g. **/app/Modules/Core/Pages/Backend/Templates/IndexPage.tpl)', $sourceTemplateFile), 0, E_USER_ERROR, __FILE__, __LINE__); } // build and return file name of the compiled template return FileUtil::getTempFileName(sprintf('tpl/%s/%s/%s', $templateInfo['module'], $templateInfo['path'], Application::getI18nHandler()->getLocale()->getLocaleCode()), '', $templateInfo['name'], 'php'); }
/** * Tests whether normalizePath() works correctly. * * @dataProvider dataProviderTestNormalizePath */ public function testNormalizePath($path, $expectedResult, $isAbsolutePath) { $this->assertSame($expectedResult, FileUtil::normalizePath($path)); }