/** * 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}"); } }
/** * Generate the URI for the provided details * * @param $type * @param $lookup * @param $domain * @param $path * @param $file * * @return null|string */ public function generateUriPath($type, $lookup, $domain, $path, $file) { $parts = []; //Include the map type $parts[] = $type; //When lookup parts are avilable, e.g. vendor/package, include them if (is_array($lookup)) { foreach ($lookup as $lookupPart) { $parts[] = $lookupPart; } } $parts[] = static::hashDomain($domain); //If not path is available, assume you are requesting the base path if (empty($path)) { $partHash = 'b'; } else { //Build the hashable path $partHash = $this->_mapper->hashDirectoryArray(ValueAs::arr(explode('/', $path))); } $parts[] = $partHash; $baseDir = $this->getBasePath($this->_mapper, $type, (array) $lookup); $filePath = Path::build($baseDir, $path, $file); $fileHash = $this->_dispatcher->getFileHash($filePath); if ($fileHash === null) { //File hash doesnt exist in the cache, so lets look it up $fullPath = Path::build($this->_dispatcher->getBaseDirectory(), $filePath); $fileHash = ResourceGenerator::getFileHash($fullPath); if (!$fileHash) { //If we cant get a hash of the file, its unlikely it exists return null; } //Cache the entry, to optimise should the same resource be re-requested $this->_dispatcher->addFileHashEntry($filePath, $fileHash); } $parts[] = substr($fileHash, 0, 7); //Include the file extension $parts[] = $file; return implode('/', $parts); }