1
0
mirror of https://github.com/halaxa/json-machine.git synced 2025-01-29 10:28:09 +01:00

Autoloading class in autoloader.php to track code coverage

This commit is contained in:
Filip Halaxa 2022-02-16 14:04:14 +01:00
parent e15463bb38
commit 534176fb53
5 changed files with 60 additions and 51 deletions

View File

@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## master
### Added
- Autoloading without Composer. Thanks @a-sync
- Autoloading without Composer. Thanks @a-sync.
<br>

View File

@ -554,7 +554,7 @@ composer require halaxa/json-machine
### Without Composer
Clone or download this repository and add the following to your bootstrap file:
```php
spl_autoload_register(require '/path/to/json-machine/autoloader.php');
spl_autoload_register(require '/path/to/json-machine/src/autoloader.php');
```
<a name="development"></a>

View File

@ -1,44 +0,0 @@
<?php
declare(strict_types=1);
/**
* A simple PSR-4 spec auto loader to allow json-machine to function the same as if it were loaded via Composer.
*
* To use this just include this file in your script and the JsonMachine namespace will be made available
*
* Usage: spl_autoload_register(require '/path/to/json-machine/autoloader.php');
*
* See: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
*
* @param string $class the fully-qualified class name
*
* @return void
*/
return function ($class) {
// project-specific namespace prefix
$prefix = 'JsonMachine\\';
// base directory for the namespace prefix
$base_dir = __DIR__.DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR;
// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}
// get the relative class name
$relative_class = substr($class, $len);
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir.str_replace('\\', '/', $relative_class).'.php';
// if the file exists, require it
if (file_exists($file)) {
require $file;
}
};

52
src/autoloader.php Normal file
View File

@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace JsonMachine;
/**
* A simple PSR-4 spec auto loader to allow json-machine to function the same as if it were loaded via Composer.
*
* To use this just include this file in your script and the JsonMachine namespace will be made available
*
* Usage: spl_autoload_register(require '/path/to/json-machine/src/autoloader.php');
*
* See: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
*
* @param string $class the fully-qualified class name
*
* @return void
*/
class Autoloading
{
static public function autoloader($class)
{
// project-specific namespace prefix
$prefix = 'JsonMachine\\';
// base directory for the namespace prefix
$base_dir = __DIR__ . DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR;
// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}
// get the relative class name
$relative_class = substr($class, $len);
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
// if the file exists, require it
if (file_exists($file)) {
require $file;
}
}
}
return [Autoloading::class, 'autoloader'];

View File

@ -5,16 +5,19 @@ declare(strict_types=1);
namespace JsonMachineTest;
/**
* @coversNothing
* @covers \JsonMachine\Autoloading
*/
class autoloaderTest extends \PHPUnit_Framework_TestCase
class AutoloadingTest extends \PHPUnit_Framework_TestCase
{
public function testAutoloaderLoadsClass()
{
$dummyFile = $this->createAutoloadableClass();
register_shutdown_function(function () use ($dummyFile) {
@unlink($dummyFile);
});
$autoloadersBackup = $this->unregisterCurrentAutoloaders();
$autoloader = require __DIR__.'/../../autoloader.php';
$autoloader = require __DIR__ . '/../../src/autoloader.php';
spl_autoload_register($autoloader);
$autoloaded = class_exists('JsonMachine\\AutoloadStub');
@ -23,8 +26,6 @@ class autoloaderTest extends \PHPUnit_Framework_TestCase
$this->registerPreviousAutoloaders($autoloadersBackup);
$this->assertTrue($autoloaded);
@unlink($dummyFile);
}
private function createAutoloadableClass(): string