Skip to content

Commit

Permalink
Merge pull request #566 from flightphp/v2-loader
Browse files Browse the repository at this point in the history
Added ability to load classes with _ in them
  • Loading branch information
n0nag0n committed Mar 30, 2024
2 parents 3fda2b4 + 43300d5 commit fe2ad0c
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 24 deletions.
22 changes: 20 additions & 2 deletions flight/core/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class Loader
*/
protected array $classes = [];

/**
* If this is disabled, classes can load with underscores
*/
protected static bool $v2ClassLoading = true;

/**
* Class instances.
*
Expand Down Expand Up @@ -190,14 +195,14 @@ public static function autoload(bool $enabled = true, $dirs = []): void
*/
public static function loadClass(string $class): void
{
$classFile = str_replace(['\\', '_'], '/', $class) . '.php';
$replace_chars = self::$v2ClassLoading === true ? ['\\', '_'] : ['\\'];
$classFile = str_replace($replace_chars, '/', $class) . '.php';

foreach (self::$dirs as $dir) {
$filePath = "$dir/$classFile";

if (file_exists($filePath)) {
require_once $filePath;

return;
}
}
Expand All @@ -220,4 +225,17 @@ public static function addDirectory($dir): void
}
}
}


/**
* Sets the value for V2 class loading.
*
* @param bool $value The value to set for V2 class loading.
*
* @return void
*/
public static function setV2ClassLoading(bool $value): void
{
self::$v2ClassLoading = $value;
}
}
9 changes: 0 additions & 9 deletions flight/util/ReturnTypeWillChange.php

This file was deleted.

10 changes: 8 additions & 2 deletions tests/EngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -751,8 +751,14 @@ public function testContainerDicePdoWrapperTestBadParams() {
$engine->route('/container', Container::class.'->testThePdoWrapper');
$engine->request()->url = '/container';

$this->expectException(ErrorException::class);
$this->expectExceptionMessageMatches("/Passing null to parameter/");
// php 7.4 will throw a PDO exception, but php 8 will throw an ErrorException
if(version_compare(PHP_VERSION, '8.0.0', '<')) {
$this->expectException(PDOException::class);
$this->expectExceptionMessageMatches("/invalid data source name/");
} else {
$this->expectException(ErrorException::class);
$this->expectExceptionMessageMatches("/Passing null to parameter/");
}

$engine->start();
}
Expand Down
13 changes: 13 additions & 0 deletions tests/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,17 @@ public function getDirectories()
__DIR__ . '/classes'
], $loader->getDirectories());
}

public function testV2ClassLoading()
{
$loader = new class extends Loader {
public static function getV2ClassLoading()
{
return self::$v2ClassLoading;
}
};
$this->assertTrue($loader::getV2ClassLoading());
$loader::setV2ClassLoading(false);
$this->assertFalse($loader::getV2ClassLoading());
}
}
31 changes: 24 additions & 7 deletions tests/run_all_tests.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
#!/bin/bash

# Run all tests
composer lint
composer beautify
composer phpcs
composer test-coverage
xdg-open http://localhost:8000
composer test-server
php_versions=("php7.4" "php8.0" "php8.1" "php8.2" "php8.3")

count=${#php_versions[@]}


echo "Prettifying code first"
vendor/bin/phpcbf --standard=phpcs.xml

set -e
for ((i = 0; i < count; i++)); do
if type "${php_versions[$i]}" &> /dev/null; then
echo "Running tests for ${php_versions[$i]}"
echo " ${php_versions[$i]} vendor/bin/phpunit"
${php_versions[$i]} vendor/bin/phpunit

echo "Running PHPStan"
echo " ${php_versions[$i]} vendor/bin/phpstan"
${php_versions[$i]} vendor/bin/phpstan

echo "Running PHPCS"
echo " ${php_versions[$i]} vendor/bin/phpcs --standard=phpcs.xml -n"
${php_versions[$i]} vendor/bin/phpcs --standard=phpcs.xml -n
fi
done
1 change: 1 addition & 0 deletions tests/server/LayoutMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public function before()
<li><a href="/わたしはひとです/ええ">UTF8 URL w/ Param</a></li>
<li><a href="/dice">Dice Container</a></li>
<li><a href="/no-container">No Container Registered</a></li>
<li><a href="/Pascal_Snake_Case">Pascal_Snake_Case</a></li>
</ul>
HTML;
echo '<div id="container">';
Expand Down
11 changes: 11 additions & 0 deletions tests/server/Pascal_Snake_Case.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

class Pascal_Snake_Case // phpcs:ignore
{
public function doILoad() // phpcs:ignore
{
echo 'Yes, I load!!!';
}
}
8 changes: 4 additions & 4 deletions tests/server/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use flight\core\Loader;
use flight\database\PdoWrapper;
use tests\classes\Container;
use tests\classes\ContainerDefault;
Expand All @@ -18,10 +19,8 @@
Flight::set('flight.content_length', false);
Flight::set('flight.views.path', './');
Flight::set('flight.views.extension', '.phtml');
//Flight::set('flight.v2.output_buffering', true);

require_once 'LayoutMiddleware.php';
require_once 'OverwriteBodyMiddleware.php';
Loader::setV2ClassLoading(false);
Flight::path(__DIR__);

Flight::group('', function () {

Expand Down Expand Up @@ -147,6 +146,7 @@
Flight::set('test_me_out', 'You got it boss!'); // used in /no-container route
Flight::route('/no-container', ContainerDefault::class . '->testUi');
Flight::route('/dice', Container::class . '->testThePdoWrapper');
Flight::route('/Pascal_Snake_Case', Pascal_Snake_Case::class . '->doILoad');
}, [ new LayoutMiddleware() ]);

// Test 9: JSON output (should not output any other html)
Expand Down

0 comments on commit fe2ad0c

Please sign in to comment.