Skip to content

Commit

Permalink
Require fixed path resource directories
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Mar 22, 2019
1 parent 669c900 commit e0321be
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 37 deletions.
1 change: 0 additions & 1 deletion src/Component/DispatchableComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@

interface DispatchableComponent
{
public function getResourceDirectory();
}
60 changes: 49 additions & 11 deletions src/Dispatch.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
namespace Packaged\Dispatch;

use Packaged\Dispatch\Component\DispatchableComponent;
use Composer\Autoload\ClassLoader;
use Packaged\Dispatch\Resources\AbstractDispatchableResource;
use Packaged\Dispatch\Resources\AbstractResource;
use Packaged\Dispatch\Resources\DispatchableResource;
use Packaged\Dispatch\Resources\ResourceFactory;
use Packaged\Helpers\Path;
use ReflectionClass;
use RuntimeException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -48,12 +48,17 @@ public static function destroy()
protected $_aliases = [];
protected $_projectRoot;
protected $_componentAliases = [];
/**
* @var ClassLoader
*/
protected $_classLoader;

public function __construct($projectRoot, $baseUri = null)
public function __construct($projectRoot, $baseUri = null, ClassLoader $loader = null)
{
$this->_projectRoot = $projectRoot;
$this->_resourceStore = new ResourceStore();
$this->_baseUri = $baseUri;
$this->_classLoader = $loader;
}

public function getResourcesPath()
Expand Down Expand Up @@ -124,6 +129,7 @@ public function handleRequest(Request $request): Response
$manager = ResourceManager::public();
break;
case ResourceManager::MAP_COMPONENT:

$len = array_shift($pathParts);
$class = '';
for($i = 0; $i < $len; $i++)
Expand All @@ -138,18 +144,19 @@ public function handleRequest(Request $request): Response
$class .= '\\' . $part;
}
}
if(class_exists($class))

if(!empty($class))
{
$reflected = new ReflectionClass($class);
if($reflected->implementsInterface(DispatchableComponent::class))
try
{
$manager = ResourceManager::componentPath($this->componentClassResourcePath($class));
}
catch(RuntimeException $e)
{
$component = $reflected->newInstanceWithoutConstructor();
if($component instanceof DispatchableComponent)
{
$manager = ResourceManager::component($component);
}
//Class Loader not available
}
}

if(!isset($manager))
{
return Response::create("Component Not Found", 404);
Expand Down Expand Up @@ -186,6 +193,37 @@ public function handleRequest(Request $request): Response
return ResourceFactory::create($resource);
}

public function componentClassResourcePath($class)
{
$loader = $this->_getClassLoader();
if($loader instanceof ClassLoader)
{
$file = $loader->findFile(ltrim($class, '\\'));
if(!$file)
{
throw new RuntimeException("Unable to load class");
}
return Path::system(dirname(realpath($file)), '_resources');
}
throw new RuntimeException("No Class Loader Defined");
}

protected function _getClassLoader()
{
if($this->_classLoader === null)
{
foreach(spl_autoload_functions() as list($loader))
{
if($loader instanceof ClassLoader)
{
$this->_classLoader = $loader;
break;
}
}
}
return $this->_classLoader;
}

public function store()
{
return $this->_resourceStore;
Expand Down
18 changes: 14 additions & 4 deletions src/ResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ResourceManager
protected $_baseUri = [];
/** @var DispatchableComponent */
protected $_component;
protected $_componentPath;
protected $_options = [];

public function __construct($type, array $mapOptions = [], array $options = [])
Expand Down Expand Up @@ -86,17 +87,25 @@ public static function external()
return new static(self::MAP_EXTERNAL, []);
}

public static function componentPath($path)
{
$manager = new static(self::MAP_COMPONENT);
$manager->_componentPath = $path;
return $manager;
}

public static function component(DispatchableComponent $component)
{
$dispatch = Dispatch::instance();
if($component instanceof FixedClassComponent)
{
$class = $component->getComponentClass();
$fullClass = $component->getComponentClass();
}
else
{
$class = get_class($component);
$fullClass = get_class($component);
}
$class = $fullClass;
if($dispatch)
{
$maxPrefix = $maxAlias = '';
Expand All @@ -118,6 +127,7 @@ public static function component(DispatchableComponent $component)
array_unshift($parts, count($parts));
$manager = new static(self::MAP_COMPONENT, $parts);
$manager->_component = $component;
$manager->_componentPath = $dispatch->componentClassResourcePath($fullClass);
return $manager;
}

Expand Down Expand Up @@ -169,9 +179,9 @@ public function getFilePath($relativePath)
{
return Path::system(Dispatch::instance()->getAliasPath($this->_mapOptions[0]), $relativePath);
}
else if($this->_type == self::MAP_COMPONENT && $this->_component)
else if($this->_type == self::MAP_COMPONENT)
{
return Path::system($this->_component->getResourceDirectory(), $relativePath);
return Path::system($this->_componentPath, $relativePath);
}
throw new \Exception("invalid map type");
}
Expand Down
9 changes: 6 additions & 3 deletions tests/DispatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +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\Child\ChildComponent;
use Packaged\Dispatch\Tests\TestComponents\DemoComponent\DemoComponent;
use Packaged\Dispatch\Tests\TestComponents\DemoComponent\ResourcedDemoComponent;
use Packaged\Helpers\Path;
Expand Down Expand Up @@ -92,6 +92,9 @@ public function testStore()
Dispatch::destroy();
}

/**
* @throws \Exception
*/
public function testComponent()
{
$dispatch = new Dispatch(Path::system(__DIR__, '_root'));
Expand All @@ -116,7 +119,7 @@ public function testComponent()
$request = Request::create('/' . $manager->getResourceUri('style.css'));
$response = $dispatch->handleRequest($request);
$this->assertEquals(200, $response->getStatusCode());
$this->assertContains('body{color:orange}', $response->getContent());
$this->assertContains('body{color:red}', $response->getContent());

//Required for testing correct namespace validation
Dispatch::instance()->addComponentAlias('\Packaged\Dispatch\Tests\TestComponents\DemoComponent', 'DC');
Expand All @@ -132,6 +135,6 @@ public function testComponent()

$manager = ResourceManager::component(new ChildComponent());
$uri = $manager->getResourceUri('style.css');
$this->assertEquals('c/2/_/AbstractComponent/a4197ed8/style.css', $uri);
$this->assertEquals('c/2/_/AbstractComponent/b1451a76/style.css', $uri);
}
}
6 changes: 0 additions & 6 deletions tests/TestComponents/AbstractComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@
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');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Packaged\Dispatch\Tests\TestComponents\DemoComponent;
namespace Packaged\Dispatch\Tests\TestComponents\DemoComponent\Child;

use Packaged\Dispatch\Tests\TestComponents\AbstractComponent;

Expand Down
5 changes: 0 additions & 5 deletions tests/TestComponents/DemoComponent/DemoComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,4 @@

class DemoComponent extends UiComponent
{
public function getResourceDirectory()
{
return __DIR__;
}

}
6 changes: 0 additions & 6 deletions tests/TestComponents/DemoComponent/ResourcedDemoComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,9 @@

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

class ResourcedDemoComponent extends UiComponent
{
public function getResourceDirectory()
{
return Path::system(__DIR__, 'resources');
}

protected function _requireResources(ResourceManager $manager)
{
$manager->requireCss('style.css');
Expand Down
3 changes: 3 additions & 0 deletions tests/TestComponents/_resources/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
color: blue;
}

0 comments on commit e0321be

Please sign in to comment.