Skip to content

Commit

Permalink
Corona: Inline filesystem lookup of controller in FrontController
Browse files Browse the repository at this point in the history
This removes the dependency on a FilesystemAccessObject.

Reviewed at https://reviews.lunr.nl/r/1082/
  • Loading branch information
pprkut committed Jan 15, 2024
1 parent 12f71aa commit baaae31
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 214 deletions.
31 changes: 15 additions & 16 deletions src/Lunr/Corona/FrontController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@

namespace Lunr\Corona;

use Lunr\Ray\FilesystemAccessObjectInterface;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RecursiveRegexIterator;
use RegexIterator;

/**
* Controller class
Expand All @@ -30,12 +33,6 @@ class FrontController
*/
protected $handler;

/**
* Instance of the FilesystemAccessObject class.
* @var FilesystemAccessObjectInterface
*/
protected $fao;

/**
* Registered lookup paths.
* @var array
Expand All @@ -51,15 +48,13 @@ class FrontController
/**
* Constructor.
*
* @param Request $request Instance of the Request class.
* @param RequestResultHandler $handler Instance of the RequestResultHandler class.
* @param FilesystemAccessObjectInterface $fao Instance of the FilesystemAccessObject class.
* @param Request $request Instance of the Request class.
* @param RequestResultHandler $handler Instance of the RequestResultHandler class.
*/
public function __construct($request, $handler, $fao)
public function __construct($request, $handler)
{
$this->request = $request;
$this->handler = $handler;
$this->fao = $fao;

$this->paths = [];
$this->routes = [];
Expand All @@ -72,7 +67,6 @@ public function __destruct()
{
unset($this->request);
unset($this->handler);
unset($this->fao);
unset($this->paths);
unset($this->routes);
}
Expand Down Expand Up @@ -113,7 +107,7 @@ public function add_routing_rule($call, $route = [])
*
* @return string $controller Fully qualified name of the responsible controller.
*/
public function get_controller($src, $list = [], $blacklist = TRUE)
public function get_controller(string $src, array $list = [], bool $blacklist = TRUE): string
{
$name = $this->request->controller . 'controller';

Expand All @@ -136,8 +130,13 @@ public function get_controller($src, $list = [], $blacklist = TRUE)
return '';
}

$name = str_replace('-', '', $name);
$matches = $this->fao->find_matches("/^.+\/$name.php/i", $src);
$name = str_replace('-', '', $name);

$directory = new RecursiveDirectoryIterator($src);
$iterator = new RecursiveIteratorIterator($directory);
$candidates = new RegexIterator($iterator, "/^.+\/$name.php/i", RecursiveRegexIterator::GET_MATCH);

$matches = array_keys(iterator_to_array($candidates));

if (empty($matches) === TRUE)
{
Expand Down
8 changes: 0 additions & 8 deletions src/Lunr/Corona/Tests/FrontControllerBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ public function testRequestResultHandlerPassedCorrectly(): void
$this->assertPropertySame('handler', $this->handler);
}

/**
* Test that the FilesystemAccessObject class was passed correctly.
*/
public function testFAOPassedCorrectly(): void
{
$this->assertPropertySame('fao', $this->fao);
}

/**
* Test that register_lookup_path() registers the passed path
*
Expand Down
93 changes: 14 additions & 79 deletions src/Lunr/Corona/Tests/FrontControllerGetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,14 @@ class FrontControllerGetTest extends FrontControllerTest
*/
public function testGetControllerReturnsFQCNForExistingController(): void
{
$dir = __DIR__;
$result = __DIR__ . '/Project/Package/FunctionController.php';
$fqcn = 'Project\\Package\\FunctionController';
$dir = TEST_STATICS . '/Corona/';
$fqcn = 'Project\\Package1\\FunctionController';

$this->request->expects($this->exactly(2))
->method('__get')
->with('controller')
->willReturn('function');

$this->fao->expects($this->once())
->method('find_matches')
->with('/^.+\/functioncontroller.php/i', $dir)
->willReturn([ $result ]);

$value = $this->class->get_controller($dir);

$this->assertEquals($fqcn, $value);
Expand All @@ -51,41 +45,12 @@ public function testGetControllerReturnsFQCNForExistingController(): void
*/
public function testGetControllerReturnsEmptyStringForNonExistingController(): void
{
$dir = __DIR__;

$this->request->expects($this->exactly(2))
->method('__get')
->with('controller')
->willReturn('function');

$this->fao->expects($this->once())
->method('find_matches')
->with('/^.+\/functioncontroller.php/i', $dir)
->willReturn([]);

$value = $this->class->get_controller($dir);

$this->assertEquals('', $value);
}

/**
* Test that get_controller() returns an empty string if finding caused error.
*
* @covers Lunr\Corona\FrontController::get_controller
*/
public function testGetControllerReturnsEmptyStringIfFindFailed(): void
{
$dir = __DIR__;
$dir = TEST_STATICS . '/Corona/';

$this->request->expects($this->exactly(2))
->method('__get')
->with('controller')
->willReturn('function');

$this->fao->expects($this->once())
->method('find_matches')
->with('/^.+\/functioncontroller.php/i', $dir)
->willReturn(FALSE);
->willReturn('method');

$value = $this->class->get_controller($dir);

Expand All @@ -99,16 +64,13 @@ public function testGetControllerReturnsEmptyStringIfFindFailed(): void
*/
public function testGetControllerReturnsEmptyStringIfNoControllerInfoAvailable(): void
{
$dir = __DIR__;
$dir = TEST_STATICS . '/Corona/';

$this->request->expects($this->exactly(1))
->method('__get')
->with('controller')
->willReturn(NULL);

$this->fao->expects($this->never())
->method('find_matches');

$value = $this->class->get_controller($dir);

$this->assertEquals('', $value);
Expand All @@ -121,19 +83,13 @@ public function testGetControllerReturnsEmptyStringIfNoControllerInfoAvailable()
*/
public function testGetControllerReturnsFirstMatchIfMultipleFound(): void
{
$dir = __DIR__;
$result = __DIR__ . '/Project/Package/FunctionController.php';
$fqcn = 'Project\\Package\\FunctionController';
$dir = TEST_STATICS . '/Corona/';
$fqcn = 'Project\\Package2\\FooController';

$this->request->expects($this->exactly(2))
->method('__get')
->with('controller')
->willReturn('function');

$this->fao->expects($this->once())
->method('find_matches')
->with('/^.+\/functioncontroller.php/i', $dir)
->willReturn([ $result, 'nr2' ]);
->willReturn('foo');

$value = $this->class->get_controller($dir);

Expand All @@ -147,16 +103,13 @@ public function testGetControllerReturnsFirstMatchIfMultipleFound(): void
*/
public function testGetBlacklistedControllerReturnsEmptyString(): void
{
$dir = __DIR__;
$dir = TEST_STATICS . '/Corona/';

$this->request->expects($this->exactly(2))
->method('__get')
->with('controller')
->willReturn('function');

$this->fao->expects($this->never())
->method('find_matches');

$value = $this->class->get_controller($dir, [ 'function' ]);

$this->assertSame('', $value);
Expand All @@ -169,16 +122,13 @@ public function testGetBlacklistedControllerReturnsEmptyString(): void
*/
public function testGetNotWhitelistedControllerReturnsEmptyString(): void
{
$dir = __DIR__;
$dir = TEST_STATICS . '/Corona/';

$this->request->expects($this->exactly(2))
->method('__get')
->with('controller')
->willReturn('function');

$this->fao->expects($this->never())
->method('find_matches');

$value = $this->class->get_controller($dir, [], FALSE);

$this->assertSame('', $value);
Expand All @@ -191,20 +141,14 @@ public function testGetNotWhitelistedControllerReturnsEmptyString(): void
*/
public function testGetWhitelistedControllerReturnsFQCNForExistingController(): void
{
$dir = __DIR__;
$result = __DIR__ . '/Project/Package/FunctionController.php';
$fqcn = 'Project\\Package\\FunctionController';
$dir = TEST_STATICS . '/Corona/';
$fqcn = 'Project\\Package1\\FunctionController';

$this->request->expects($this->exactly(2))
->method('__get')
->with('controller')
->willReturn('function');

$this->fao->expects($this->once())
->method('find_matches')
->with('/^.+\/functioncontroller.php/i', $dir)
->willReturn([ $result ]);

$value = $this->class->get_controller($dir, [ 'function' ], FALSE);

$this->assertEquals($fqcn, $value);
Expand All @@ -227,9 +171,6 @@ public function testGetControllerReturnsDefault($controller_name): void
->with('controller')
->willReturn($controller_name);

$this->fao->expects($this->never())
->method('find_matches');

$value = $this->class->get_controller($dir);

$this->assertEquals('', $value);
Expand All @@ -242,20 +183,14 @@ public function testGetControllerReturnsDefault($controller_name): void
*/
public function testGetControllerForDashesInController()
{
$dir = __DIR__;
$result = __DIR__ . '/Project/Package/AnonymousTapsController.php';
$fqcn = 'Project\\Package\\AnonymousTapsController';
$dir = TEST_STATICS . '/Corona/';
$fqcn = 'Project\\Package1\\AnonymousTapsController';

$this->request->expects($this->exactly(2))
->method('__get')
->with('controller')
->willReturn('anonymous-taps');

$this->fao->expects($this->once())
->method('find_matches')
->with('/^.+\/anonymoustapscontroller.php/i', $dir)
->willReturn([ $result ]);

$value = $this->class->get_controller($dir);

$this->assertEquals($fqcn, $value);
Expand Down
Loading

0 comments on commit baaae31

Please sign in to comment.