/** * Create the response for the given path * * @param $path * @param Request $request * * @return Response */ public function getResponseForPath($path, Request $request) { if (empty($path)) { //What resources do you expect to find with no path? return $this->invalidUrlResponse(); } $pathInfo = pathinfo($path); //Every dispatch request needs an extension if (empty($pathInfo['extension'])) { return $this->invalidUrlResponse(); } $response = new AssetResponse(); //Grab the correct asset for the requesting extension $asset = $response->assetByExtension($pathInfo['extension']); //Load the options $options = ValueAs::arr($this->_config->getItem($pathInfo['extension'] . '_config'), null); if ($options !== null) { $asset->setOptions($options); } //Lookup the full path on the filesystem $dirMapper = new DirectoryMapper($this->_baseDirectory, $this->_config); $directory = $dirMapper->urlToPath($pathInfo['dirname']); $filePath = Path::build($directory, $pathInfo['basename']); //Do not minify files ending in .min.ext if (substr($pathInfo['filename'], -4) == '.min') { $asset->setOption('minify', false); } //If the asset does not exist on disk, return a not found error if ($directory === null || !file_exists($filePath)) { return $this->notFoundResponse($path); } //Give the asset its file content $asset->setContent(file_get_contents($filePath)); if ($asset instanceof IDispatchableAsset) { //Set the asset manager $asset->setWorkingDirectory(realpath($directory)); $asset->setAssetManager(AssetManager::buildFromUri($path)); } //Create and return the response return $response->createResponse($asset, $request); }
/** * Get the base directory for the type/lookup info provided * * @param DirectoryMapper $mapper * @param $type * @param array $lookup * * @return null|string */ public function getBasePath(DirectoryMapper $mapper, $type, array $lookup) { $cache = $type . '-' . implode('.', $lookup); //If the path is cached, return it if (isset($this->_baseHash[$cache])) { return $this->_baseHash[$cache]; } $parts = array_merge([$type], $lookup); switch ($type) { case DirectoryMapper::MAP_ALIAS: $this->_baseHash[$cache] = $mapper->aliasPath($parts); break; case DirectoryMapper::MAP_SOURCE: $this->_baseHash[$cache] = $mapper->sourcePath(); break; case DirectoryMapper::MAP_ASSET: $this->_baseHash[$cache] = $mapper->assetPath(); break; case DirectoryMapper::MAP_VENDOR: $this->_baseHash[$cache] = $mapper->vendorPath($parts); break; } //Return the cache return isset($this->_baseHash[$cache]) ? $this->_baseHash[$cache] : null; }