示例#1
0
 /**
  * Execute the command.
  *
  * @param  InputInterface  $input
  * @param  OutputInterface $output
  *
  * @return void
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $baseDir = $input->getArgument('base');
     if ($baseDir === null) {
         $baseDir = $this->_projectBase;
     }
     $baseDir = rtrim($baseDir, '/') . '/';
     $extensions = AssetResponse::getExtensions();
     $pattern = '*.' . implode(',*.', $extensions);
     $fileList = $this->globRecursive($baseDir . '{' . $pattern . '}', GLOB_BRACE);
     $hashMap = [];
     $data = '';
     foreach ($fileList as $file) {
         $key = str_replace($baseDir, '', $file);
         $hash = ResourceGenerator::getFileHash($file);
         $hashMap[$key] = $hash;
         $data .= "{$key} = {$hash}\n";
     }
     $outputFile = $input->getOption('output');
     $filename = Path::build($baseDir, $outputFile);
     if (!file_exists($filename) || is_writable($filename)) {
         file_put_contents($filename, $data);
         $output->writeln("Written " . count($fileList) . " file hash keys to " . $filename);
     } else {
         $output->writeln("Failed writing to {$filename}");
     }
 }
示例#2
0
 public function assetProvider()
 {
     $attempt = [];
     $resp = new \Packaged\Dispatch\AssetResponse();
     foreach (\Packaged\Dispatch\AssetResponse::getExtensions() as $ext) {
         $attempt[] = [$ext, $resp->assetByExtension($ext)];
     }
     return $attempt;
 }
示例#3
0
 public function testCustomType()
 {
     $exts = \Packaged\Dispatch\AssetResponse::getExtensions();
     $this->assertFalse(array_search('mock', $exts));
     $builder = new \Packaged\Dispatch\AssetResponse();
     $this->assertInstanceOf('\\Packaged\\Dispatch\\Assets\\UnknownAsset', $builder->assetByExtension('mock'));
     \Packaged\Dispatch\AssetResponse::addAssetType('mock', new MockAssetType());
     $asset = $builder->assetByExtension('mock');
     $this->assertInstanceOf('\\MockAssetType', $asset);
     $this->assertEquals('mock', $asset->getExtension());
     $this->assertEquals('mock/asset', $asset->getContentType());
 }
示例#4
0
 /**
  * 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);
 }