Skip to content

Commit fe2ad0c

Browse files
authored
Merge pull request #566 from flightphp/v2-loader
Added ability to load classes with _ in them
2 parents 3fda2b4 + 43300d5 commit fe2ad0c

8 files changed

Lines changed: 81 additions & 24 deletions

File tree

flight/core/Loader.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ class Loader
2525
*/
2626
protected array $classes = [];
2727

28+
/**
29+
* If this is disabled, classes can load with underscores
30+
*/
31+
protected static bool $v2ClassLoading = true;
32+
2833
/**
2934
* Class instances.
3035
*
@@ -190,14 +195,14 @@ public static function autoload(bool $enabled = true, $dirs = []): void
190195
*/
191196
public static function loadClass(string $class): void
192197
{
193-
$classFile = str_replace(['\\', '_'], '/', $class) . '.php';
198+
$replace_chars = self::$v2ClassLoading === true ? ['\\', '_'] : ['\\'];
199+
$classFile = str_replace($replace_chars, '/', $class) . '.php';
194200

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

198204
if (file_exists($filePath)) {
199205
require_once $filePath;
200-
201206
return;
202207
}
203208
}
@@ -220,4 +225,17 @@ public static function addDirectory($dir): void
220225
}
221226
}
222227
}
228+
229+
230+
/**
231+
* Sets the value for V2 class loading.
232+
*
233+
* @param bool $value The value to set for V2 class loading.
234+
*
235+
* @return void
236+
*/
237+
public static function setV2ClassLoading(bool $value): void
238+
{
239+
self::$v2ClassLoading = $value;
240+
}
223241
}

flight/util/ReturnTypeWillChange.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/EngineTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,8 +751,14 @@ public function testContainerDicePdoWrapperTestBadParams() {
751751
$engine->route('/container', Container::class.'->testThePdoWrapper');
752752
$engine->request()->url = '/container';
753753

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

757763
$engine->start();
758764
}

tests/LoaderTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,17 @@ public function getDirectories()
152152
__DIR__ . '/classes'
153153
], $loader->getDirectories());
154154
}
155+
156+
public function testV2ClassLoading()
157+
{
158+
$loader = new class extends Loader {
159+
public static function getV2ClassLoading()
160+
{
161+
return self::$v2ClassLoading;
162+
}
163+
};
164+
$this->assertTrue($loader::getV2ClassLoading());
165+
$loader::setV2ClassLoading(false);
166+
$this->assertFalse($loader::getV2ClassLoading());
167+
}
155168
}

tests/run_all_tests.sh

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
#!/bin/bash
22

3-
# Run all tests
4-
composer lint
5-
composer beautify
6-
composer phpcs
7-
composer test-coverage
8-
xdg-open http://localhost:8000
9-
composer test-server
3+
php_versions=("php7.4" "php8.0" "php8.1" "php8.2" "php8.3")
4+
5+
count=${#php_versions[@]}
6+
7+
8+
echo "Prettifying code first"
9+
vendor/bin/phpcbf --standard=phpcs.xml
10+
11+
set -e
12+
for ((i = 0; i < count; i++)); do
13+
if type "${php_versions[$i]}" &> /dev/null; then
14+
echo "Running tests for ${php_versions[$i]}"
15+
echo " ${php_versions[$i]} vendor/bin/phpunit"
16+
${php_versions[$i]} vendor/bin/phpunit
17+
18+
echo "Running PHPStan"
19+
echo " ${php_versions[$i]} vendor/bin/phpstan"
20+
${php_versions[$i]} vendor/bin/phpstan
21+
22+
echo "Running PHPCS"
23+
echo " ${php_versions[$i]} vendor/bin/phpcs --standard=phpcs.xml -n"
24+
${php_versions[$i]} vendor/bin/phpcs --standard=phpcs.xml -n
25+
fi
26+
done

tests/server/LayoutMiddleware.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public function before()
8383
<li><a href="/わたしはひとです/ええ">UTF8 URL w/ Param</a></li>
8484
<li><a href="/dice">Dice Container</a></li>
8585
<li><a href="/no-container">No Container Registered</a></li>
86+
<li><a href="/Pascal_Snake_Case">Pascal_Snake_Case</a></li>
8687
</ul>
8788
HTML;
8889
echo '<div id="container">';

tests/server/Pascal_Snake_Case.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
class Pascal_Snake_Case // phpcs:ignore
6+
{
7+
public function doILoad() // phpcs:ignore
8+
{
9+
echo 'Yes, I load!!!';
10+
}
11+
}

tests/server/index.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
declare(strict_types=1);
44

5+
use flight\core\Loader;
56
use flight\database\PdoWrapper;
67
use tests\classes\Container;
78
use tests\classes\ContainerDefault;
@@ -18,10 +19,8 @@
1819
Flight::set('flight.content_length', false);
1920
Flight::set('flight.views.path', './');
2021
Flight::set('flight.views.extension', '.phtml');
21-
//Flight::set('flight.v2.output_buffering', true);
22-
23-
require_once 'LayoutMiddleware.php';
24-
require_once 'OverwriteBodyMiddleware.php';
22+
Loader::setV2ClassLoading(false);
23+
Flight::path(__DIR__);
2524

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

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

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

0 commit comments

Comments
 (0)