Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to load classes with _ in them #566

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually caused php8.1+ to fail because it thought the ReturnTypeWillChange class was an attribute class....so it failed.

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', '<')) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is highly unusual, but the behavior in PDO actually changed inbetween 7.4 and 8.0 to throw different error messages.

$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