diff --git a/src/Dispatch.php b/src/Dispatch.php index dace92f..63cf0a7 100644 --- a/src/Dispatch.php +++ b/src/Dispatch.php @@ -53,9 +53,12 @@ class Dispatch * @var ConfigProvider */ protected $_config; - protected $_aliases = []; protected $_projectRoot; + + protected $_aliases = []; protected $_componentAliases = []; + protected $_vendorAliases = []; + protected $_vendorReverseAliases = []; /** * @var ClassLoader */ @@ -155,6 +158,18 @@ public function getVendorPath($vendor, $package) return Path::system($this->_projectRoot, self::VENDOR_DIR, $vendor, $package); } + public function getVendorOptions($vendor, $package) + { + return $this->_vendorReverseAliases[$vendor][$package] ?? null; + } + + public function addVendorAlias($vendor, $package, $alias) + { + $this->_vendorAliases[$alias] = [$vendor, $package]; + $this->_vendorReverseAliases[$vendor][$package] = $alias; + return $this; + } + public function addAlias($alias, $path) { $this->_aliases[$alias] = $path; @@ -203,7 +218,16 @@ public function handleRequest(Request $request): Response $manager = ResourceManager::alias(array_shift($pathParts)); break; case ResourceManager::MAP_VENDOR: - $manager = ResourceManager::vendor(array_shift($pathParts), array_shift($pathParts)); + $vendor = array_shift($pathParts); + if(isset($this->_vendorAliases[$vendor])) + { + [$vendor, $package] = $this->_vendorAliases[$vendor]; + } + else + { + $package = array_shift($pathParts); + } + $manager = ResourceManager::vendor($vendor, $package); break; case ResourceManager::MAP_PUBLIC: $manager = ResourceManager::public(); diff --git a/src/ResourceManager.php b/src/ResourceManager.php index edc9226..8e50c72 100644 --- a/src/ResourceManager.php +++ b/src/ResourceManager.php @@ -43,6 +43,7 @@ class ResourceManager protected $_type = self::MAP_RESOURCES; protected $_mapOptions = []; + protected $_uriPrefix; protected $_baseUri; protected $_componentPath; protected $_options = []; @@ -76,7 +77,7 @@ public function getBaseUri() if($this->_baseUri === null) { $this->_baseUri = Dispatch::instance() ? Dispatch::instance()->getBaseUri() : ''; - $this->_baseUri = Path::url($this->_baseUri, $this->_type, implode('/', $this->_mapOptions)); + $this->_baseUri = Path::url($this->_baseUri, $this->_type, $this->_uriPrefix ?? implode('/', $this->_mapOptions)); } return $this->_baseUri; } @@ -118,7 +119,9 @@ public function setResourceStore(ResourceStore $store) public static function vendor($vendor, $package, $options = []) { - return new static(self::MAP_VENDOR, [$vendor, $package], $options); + $rm = new static(self::MAP_VENDOR, [$vendor, $package], $options); + $rm->_uriPrefix = Dispatch::instance() ? Dispatch::instance()->getVendorOptions($vendor, $package) : null; + return $rm; } public static function alias($alias, $options = []) diff --git a/tests/DispatchTest.php b/tests/DispatchTest.php index 2ec7af1..34109aa 100644 --- a/tests/DispatchTest.php +++ b/tests/DispatchTest.php @@ -72,6 +72,13 @@ public function testHandle() $response = $dispatch->handleRequest($request); $this->assertContains('body{background:orange}', $response->getContent()); + $dispatch->addVendorAlias('packaged', 'dispatch', 'pdsp'); + $uri = ResourceManager::vendor('packaged', 'dispatch')->getResourceUri('css/vendor.css'); + self::assertContains('v/pdsp', $uri); + $request = Request::create($uri); + $response = $dispatch->handleRequest($request); + $this->assertContains('body{background:orange}', $response->getContent()); + Dispatch::instance()->config()->addItem('ext.css', 'sourcemap', true); $uri = ResourceManager::vendor('packaged', 'dispatch')->getResourceUri('css/vendor.css'); $request = Request::create($uri);