Пример #1
0
 /**
  * Return an asset source file path.
  *
  * A system of fallback enables file overriding. It will look for the template :
  *      - in the current template in directory /modules/{module code}/
  *      - in the module in the current template if it exists
  *      - in the module in the default template
  *
  * @param  string $source a module code, or or SmartyParser::TEMPLATE_ASSETS_KEY
  * @param  string $templateName a template name, or false to use the current template
  * @param  string $fileName the filename
  * @param  ParserInterface $parserInterface the current template parser
  *
  * @return mixed the path to directory containing the file, or null if the file doesn't exists.
  */
 public function resolveAssetSourcePath($source, $templateName, $fileName, ParserInterface $parserInterface)
 {
     $filePath = null;
     $templateDefinition = $parserInterface->getTemplateDefinition(false);
     // Get all possible directories to search
     $paths = $this->getPossibleAssetSources($parserInterface->getTemplateDirectories($templateDefinition->getType()), $templateName ?: $templateDefinition->getName(), $source);
     // Normalize path separator if required (e.g., / becomes \ on windows)
     $fileName = $this->fixPathSeparator($fileName);
     /* Absolute paths are not allowed. This may be a mistake, such as '/assets/...' instead of 'assets/...'. Forgive it.  */
     $fileName = ltrim($fileName, DS);
     /* Navigating in the server's directory tree is not allowed :) */
     if (preg_match('!\\.\\.\\' . DS . '!', $fileName)) {
         // This time, we will not forgive.
         throw new \InvalidArgumentException("Relative paths are not allowed in assets names.");
     }
     // Find the first occurrence of the file in the directories lists
     foreach ($paths as $path) {
         if ($this->filesExist($path, $fileName)) {
             // Got it !
             $filePath = $path;
             break;
         }
     }
     return $filePath;
 }