Skip to content

Commit 7f0d8d0

Browse files
authored
Vendor Aliases (#29)
1 parent ae57ea7 commit 7f0d8d0

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

src/Dispatch.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,12 @@ class Dispatch
5353
* @var ConfigProvider
5454
*/
5555
protected $_config;
56-
protected $_aliases = [];
5756
protected $_projectRoot;
57+
58+
protected $_aliases = [];
5859
protected $_componentAliases = [];
60+
protected $_vendorAliases = [];
61+
protected $_vendorReverseAliases = [];
5962
/**
6063
* @var ClassLoader
6164
*/
@@ -155,6 +158,18 @@ public function getVendorPath($vendor, $package)
155158
return Path::system($this->_projectRoot, self::VENDOR_DIR, $vendor, $package);
156159
}
157160

161+
public function getVendorOptions($vendor, $package)
162+
{
163+
return $this->_vendorReverseAliases[$vendor][$package] ?? null;
164+
}
165+
166+
public function addVendorAlias($vendor, $package, $alias)
167+
{
168+
$this->_vendorAliases[$alias] = [$vendor, $package];
169+
$this->_vendorReverseAliases[$vendor][$package] = $alias;
170+
return $this;
171+
}
172+
158173
public function addAlias($alias, $path)
159174
{
160175
$this->_aliases[$alias] = $path;
@@ -203,7 +218,16 @@ public function handleRequest(Request $request): Response
203218
$manager = ResourceManager::alias(array_shift($pathParts));
204219
break;
205220
case ResourceManager::MAP_VENDOR:
206-
$manager = ResourceManager::vendor(array_shift($pathParts), array_shift($pathParts));
221+
$vendor = array_shift($pathParts);
222+
if(isset($this->_vendorAliases[$vendor]))
223+
{
224+
[$vendor, $package] = $this->_vendorAliases[$vendor];
225+
}
226+
else
227+
{
228+
$package = array_shift($pathParts);
229+
}
230+
$manager = ResourceManager::vendor($vendor, $package);
207231
break;
208232
case ResourceManager::MAP_PUBLIC:
209233
$manager = ResourceManager::public();

src/ResourceManager.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ResourceManager
4343

4444
protected $_type = self::MAP_RESOURCES;
4545
protected $_mapOptions = [];
46+
protected $_uriPrefix;
4647
protected $_baseUri;
4748
protected $_componentPath;
4849
protected $_options = [];
@@ -76,7 +77,7 @@ public function getBaseUri()
7677
if($this->_baseUri === null)
7778
{
7879
$this->_baseUri = Dispatch::instance() ? Dispatch::instance()->getBaseUri() : '';
79-
$this->_baseUri = Path::url($this->_baseUri, $this->_type, implode('/', $this->_mapOptions));
80+
$this->_baseUri = Path::url($this->_baseUri, $this->_type, $this->_uriPrefix ?? implode('/', $this->_mapOptions));
8081
}
8182
return $this->_baseUri;
8283
}
@@ -118,7 +119,9 @@ public function setResourceStore(ResourceStore $store)
118119

119120
public static function vendor($vendor, $package, $options = [])
120121
{
121-
return new static(self::MAP_VENDOR, [$vendor, $package], $options);
122+
$rm = new static(self::MAP_VENDOR, [$vendor, $package], $options);
123+
$rm->_uriPrefix = Dispatch::instance() ? Dispatch::instance()->getVendorOptions($vendor, $package) : null;
124+
return $rm;
122125
}
123126

124127
public static function alias($alias, $options = [])

tests/DispatchTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ public function testHandle()
7272
$response = $dispatch->handleRequest($request);
7373
$this->assertContains('body{background:orange}', $response->getContent());
7474

75+
$dispatch->addVendorAlias('packaged', 'dispatch', 'pdsp');
76+
$uri = ResourceManager::vendor('packaged', 'dispatch')->getResourceUri('css/vendor.css');
77+
self::assertContains('v/pdsp', $uri);
78+
$request = Request::create($uri);
79+
$response = $dispatch->handleRequest($request);
80+
$this->assertContains('body{background:orange}', $response->getContent());
81+
7582
Dispatch::instance()->config()->addItem('ext.css', 'sourcemap', true);
7683
$uri = ResourceManager::vendor('packaged', 'dispatch')->getResourceUri('css/vendor.css');
7784
$request = Request::create($uri);

0 commit comments

Comments
 (0)