Skip to content

Commit e7d2964

Browse files
committed
Support multiple component aliases
1 parent 3877d32 commit e7d2964

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

src/Dispatch.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static function destroy()
4646

4747
protected $_aliases = [];
4848
protected $_projectRoot;
49-
protected $_componentsNamespace;
49+
protected $_componentAliases = [];
5050

5151
public function __construct($projectRoot, $baseUri = null)
5252
{
@@ -86,23 +86,15 @@ public function getBaseUri()
8686
return $this->_baseUri;
8787
}
8888

89-
/**
90-
* @return mixed
91-
*/
92-
public function getComponentsNamespace()
89+
public function addComponentAlias($namespace, $alias)
9390
{
94-
return $this->_componentsNamespace;
91+
$this->_componentAliases['_' . $alias] = $namespace;
92+
return $this;
9593
}
9694

97-
/**
98-
* @param mixed $componentsNs
99-
*
100-
* @return Dispatch
101-
*/
102-
public function setComponentsNamespace($componentsNs)
95+
public function getComponentAliases()
10396
{
104-
$this->_componentsNamespace = $componentsNs;
105-
return $this;
97+
return $this->_componentAliases;
10698
}
10799

108100
/**
@@ -136,9 +128,9 @@ public function handle(Request $request): Response
136128
for($i = 0; $i < $len; $i++)
137129
{
138130
$part = array_shift($pathParts);
139-
if($i == 0 && $part == '_')
131+
if($i == 0 && isset($this->_componentAliases[$part]))
140132
{
141-
$class = $this->getComponentsNamespace();
133+
$class = $this->_componentAliases[$part];
142134
}
143135
else
144136
{

src/ResourceManager.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,17 @@ public static function component(DispatchableComponent $component)
6868
$class = get_class($component);
6969
if($dispatch)
7070
{
71-
$prefix = Strings::commonPrefix($class, ltrim($dispatch->getComponentsNamespace(), '\\'));
72-
$class = str_replace($prefix, '_', $class);
71+
$maxPrefix = $maxAlias = '';
72+
foreach($dispatch->getComponentAliases() as $alias => $namespace)
73+
{
74+
$prefix = Strings::commonPrefix($class, ltrim($namespace, '\\'));
75+
if(strlen($prefix) > strlen($maxPrefix))
76+
{
77+
$maxPrefix = $prefix;
78+
$maxAlias = $alias;
79+
}
80+
}
81+
$class = str_replace($maxPrefix, $maxAlias, $class);
7382
}
7483
$parts = explode('\\', $class);
7584
array_unshift($parts, count($parts));

tests/DispatchTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function testComponent()
9797
Dispatch::bind($dispatch);
9898

9999
$component = new DemoComponent();
100-
Dispatch::instance()->setComponentsNamespace('\Packaged\Dispatch\Tests\TestComponents');
100+
Dispatch::instance()->addComponentAlias('\Packaged\Dispatch\Tests\TestComponents', '');
101101
$manager = ResourceManager::component($component);
102102
$uri = $manager->getResourceUri('style.css');
103103
$this->assertEquals('c/3/_/DemoComponent/DemoComponent/a4197ed8/style.css', $uri);
@@ -117,6 +117,11 @@ public function testComponent()
117117
$this->assertEquals(200, $response->getStatusCode());
118118
$this->assertContains('body{color:orange}', $response->getContent());
119119

120+
Dispatch::instance()->addComponentAlias('\Packaged\Dispatch\Tests\TestComponents\DemoComponent', 'DC');
121+
$manager = ResourceManager::component(new DemoComponent());
122+
$uri = $manager->getResourceUri('style.css');
123+
$this->assertEquals('c/2/_DC/DemoComponent/a4197ed8/style.css', $uri);
124+
120125
$request = Request::create('/c/3/_/MissingComponent/DemoComponent/a4197ed8/style.css');
121126
$response = $dispatch->handle($request);
122127
$this->assertEquals(404, $response->getStatusCode());

tests/ResourceManagerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testComponent()
5454
'c/6/Packaged/Dispatch/Tests/TestComponents/DemoComponent/DemoComponent/a4197ed8/style.css',
5555
$manager->getResourceUri('style.css')
5656
);
57-
Dispatch::instance()->setComponentsNamespace('\Packaged\Dispatch\Tests\TestComponents');
57+
Dispatch::instance()->addComponentAlias('\Packaged\Dispatch\Tests\TestComponents','');
5858
$manager = ResourceManager::component($component);
5959
$this->assertEquals(
6060
'c/3/_/DemoComponent/DemoComponent/a4197ed8/style.css',

0 commit comments

Comments
 (0)