/** * 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); }
public function testTrigger() { $event = new \Packaged\Dispatch\DispatchEvent(); NewDispatcher::clear(); NewDispatcher::trigger($event); $this->assertNull($event->getResult()); $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); $request->headers->set('HOST', 'packaged.in'); $request->server->set('REQUEST_URI', '/'); $dispatcher = new \Packaged\Dispatch\Dispatch(new DummyKernel(), ['source_dir' => 'tests']); $dispatcher->handle($request); $event = new \Packaged\Dispatch\DispatchEvent(); $event->setFilename('test.css'); $event->setMapType(\Packaged\Dispatch\DirectoryMapper::MAP_SOURCE); $event->setPath('asset'); \Packaged\Dispatch\Dispatch::trigger($event); $expect = '//packaged.in/res/s/dfcbf/asc04e3/76d6c18/test.css'; $this->assertEquals($expect, $event->getResult()); }
/** * Generate a resource uri * * @param $filename * @param $path * @param $extension * * @return mixed */ public function getResourceUri($filename, $path = null, $extension = null) { //If no filename is sent, the resource is very unlikely to be valid if (empty($filename)) { return null; } if ($this->isExternalUrl($filename)) { return $filename; } if ($extension !== null) { if (substr($filename, -4) !== '.min') { $filename = [$filename . '.min.' . $extension, $filename . '.' . $extension]; } else { $filename = [$filename . '.' . $extension]; } } $event = new DispatchEvent(); $event->setLookupParts((array) $this->_lookupParts); $event->setMapType($this->_mapType); $event->setPath($path); foreach ((array) $filename as $fname) { $event->setFilename($fname); $result = Dispatch::trigger($event); if ($result !== null && $result->getResult() !== null) { return $result->getResult(); } } return null; }