Skip to content

Commit

Permalink
Fixed Class Components
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Jan 29, 2019
1 parent 919dabc commit 2e0cbd1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/Component/FixedClassComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace Packaged\Dispatch\Component;

interface FixedClassComponent
{
public function getComponentClass(): string;
}
10 changes: 9 additions & 1 deletion src/ResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Packaged\Dispatch;

use Packaged\Dispatch\Component\DispatchableComponent;
use Packaged\Dispatch\Component\FixedClassComponent;
use Packaged\Helpers\Path;
use Packaged\Helpers\Strings;

Expand Down Expand Up @@ -71,7 +72,14 @@ public static function external()
public static function component(DispatchableComponent $component)
{
$dispatch = Dispatch::instance();
$class = get_class($component);
if($component instanceof FixedClassComponent)
{
$class = $component->getComponentClass();
}
else
{
$class = get_class($component);
}
if($dispatch)
{
$maxPrefix = $maxAlias = '';
Expand Down
7 changes: 6 additions & 1 deletion tests/DispatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Packaged\Dispatch\Dispatch;
use Packaged\Dispatch\ResourceManager;
use Packaged\Dispatch\ResourceStore;
use Packaged\Dispatch\Tests\TestComponents\DemoComponent\ChildComponent;
use Packaged\Dispatch\Tests\TestComponents\DemoComponent\DemoComponent;
use Packaged\Dispatch\Tests\TestComponents\DemoComponent\ResourcedDemoComponent;
use Packaged\Helpers\Path;
Expand Down Expand Up @@ -118,8 +119,8 @@ public function testComponent()
$this->assertContains('body{color:orange}', $response->getContent());

//Required for testing correct namespace validation
Dispatch::instance()->addComponentAlias('\Packaged\Dispatch\Tests\TestComponents\DemoComponents', 'DCRC');
Dispatch::instance()->addComponentAlias('\Packaged\Dispatch\Tests\TestComponents\DemoComponent', 'DC');
Dispatch::instance()->addComponentAlias('\Packaged\Dispatch\Tests\TestComponents\DemoComponents', 'DCRC');
$manager = ResourceManager::component(new DemoComponent());
$uri = $manager->getResourceUri('style.css');
$this->assertEquals('c/2/_DC/DemoComponent/a4197ed8/style.css', $uri);
Expand All @@ -128,5 +129,9 @@ public function testComponent()
$response = $dispatch->handle($request);
$this->assertEquals(404, $response->getStatusCode());
$this->assertContains('Component Not Found', $response->getContent());

$manager = ResourceManager::component(new ChildComponent());
$uri = $manager->getResourceUri('style.css');
$this->assertEquals('c/2/_/AbstractComponent/a4197ed8/style.css', $uri);
}
}
26 changes: 26 additions & 0 deletions tests/TestComponents/AbstractComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Packaged\Dispatch\Tests\TestComponents;

use Packaged\Dispatch\Component\FixedClassComponent;
use Packaged\Dispatch\Component\UiComponent;
use Packaged\Dispatch\ResourceManager;
use Packaged\Helpers\Path;

abstract class AbstractComponent extends UiComponent implements FixedClassComponent
{
public function getResourceDirectory()
{
return Path::system(__DIR__, 'DemoComponent');
}

protected function _requireResources(ResourceManager $manager)
{
$manager->requireCss('style.css');
}

public function getComponentClass(): string
{
return self::class;
}

}
9 changes: 9 additions & 0 deletions tests/TestComponents/DemoComponent/ChildComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Packaged\Dispatch\Tests\TestComponents\DemoComponent;

use Packaged\Dispatch\Tests\TestComponents\AbstractComponent;

class ChildComponent extends AbstractComponent
{

}

0 comments on commit 2e0cbd1

Please sign in to comment.