Skip to content

Commit

Permalink
Vendor Aliases (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Jun 22, 2021
1 parent ae57ea7 commit 7f0d8d0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
28 changes: 26 additions & 2 deletions src/Dispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ class Dispatch
* @var ConfigProvider
*/
protected $_config;
protected $_aliases = [];
protected $_projectRoot;

protected $_aliases = [];
protected $_componentAliases = [];
protected $_vendorAliases = [];
protected $_vendorReverseAliases = [];
/**
* @var ClassLoader
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 5 additions & 2 deletions src/ResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ResourceManager

protected $_type = self::MAP_RESOURCES;
protected $_mapOptions = [];
protected $_uriPrefix;
protected $_baseUri;
protected $_componentPath;
protected $_options = [];
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 = [])
Expand Down
7 changes: 7 additions & 0 deletions tests/DispatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7f0d8d0

Please sign in to comment.