/** * Replace source path with correct one * * This operation can check for cache, can rewrite paths, add additional filters and much more * * @param string $originalResource Initial resource to include * @param string $originalDir Path to the directory from where include was called for resolving relative resources * * @return string Transformed path to the resource */ public static function rewrite($originalResource, $originalDir = '') { static $appDir, $cacheDir, $debug, $usePrebuiltCache; if (!$appDir) { extract(self::$options, EXTR_IF_EXISTS); $usePrebuiltCache = self::$options['features'] & Features::PREBUILT_CACHE; } $resource = (string) $originalResource; if ($resource['0'] !== '/') { $resource = PathResolver::realpath($resource, $checkExistence = true) ?: PathResolver::realpath("{$originalDir}/{$resource}", $checkExistence = true) ?: $originalResource; } // If the cache is disabled, then use on-fly method if (!$cacheDir || $debug) { return self::PHP_FILTER_READ . self::$filterName . "/resource=" . $resource; } $newResource = str_replace($appDir, $cacheDir, $resource); // Trigger creation of cache, this will create a cache file with $newResource name if (!$usePrebuiltCache && !file_exists($newResource)) { // Workaround for https://github.com/facebook/hhvm/issues/2485 $file = fopen($resource, 'r'); stream_filter_append($file, self::$filterName); stream_get_contents($file); fclose($file); } return $newResource; }
/** * Creates metadata object from stream * * @param resource $stream Instance of stream * @param string $source Source code or null * @throws \InvalidArgumentException for invalid stream */ public function __construct($stream, $source = null) { if (!is_resource($stream)) { throw new InvalidArgumentException("Stream should be valid resource"); } $metadata = stream_get_meta_data($stream); if ($source) { $metadata['source'] = $source; } if (preg_match('/resource=(.+)$/', $metadata['uri'], $matches)) { $metadata['uri'] = PathResolver::realpath($matches[1]); } parent::__construct($metadata, ArrayObject::ARRAY_AS_PROPS); }
/** * Normalizes options for the kernel * * @param array $options List of options * * @return array */ protected function normalizeOptions(array $options) { $options = array_replace($this->getDefaultOptions(), $options); $options['appDir'] = PathResolver::realpath($options['appDir']); $options['cacheDir'] = PathResolver::realpath($options['cacheDir']); $options['includePaths'] = PathResolver::realpath($options['includePaths']); $options['excludePaths'] = PathResolver::realpath($options['excludePaths']); return $options; }
/** * Replace source path with correct one * * This operation can check for cache, can rewrite paths, add additional filters and much more * * @param string $originalResource Initial resource to include * @param string $originalDir Path to the directory from where include was called for resolving relative resources * * @return string Transformed path to the resource */ public static function rewrite($originalResource, $originalDir = '') { static $appDir, $cacheDir, $debug; if (!$appDir) { extract(self::$options, EXTR_IF_EXISTS); } $resource = (string) $originalResource; if ($resource['0'] !== '/') { $shouldCheckExistence = true; $resource = PathResolver::realpath($resource, $shouldCheckExistence) ?: PathResolver::realpath("{$originalDir}/{$resource}", $shouldCheckExistence) ?: $originalResource; } $cachedResource = self::$cachePathManager->getCachePathForResource($resource); // If the cache is disabled or no cache yet, then use on-fly method if (!$cacheDir || $debug || !file_exists($cachedResource)) { return self::PHP_FILTER_READ . self::$filterName . "/resource=" . $resource; } return $cachedResource; }
/** * Normalizes options for the kernel * * @param array $options List of options * * @return array */ protected function normalizeOptions(array $options) { $options = array_replace($this->getDefaultOptions(), $options); if ($options['cacheDir']) { $options['excludePaths'][] = $options['cacheDir']; } $options['excludePaths'][] = __DIR__ . '/../'; $options['appDir'] = PathResolver::realpath($options['appDir']); $options['cacheDir'] = PathResolver::realpath($options['cacheDir']); $options['cacheFileMode'] = (int) $options['cacheFileMode']; $options['includePaths'] = PathResolver::realpath($options['includePaths']); $options['excludePaths'] = PathResolver::realpath($options['excludePaths']); return $options; }