1
0
mirror of https://github.com/halaxa/json-machine.git synced 2025-01-17 13:08:16 +01:00

add a stand alone PSR-4 spec loader

This commit is contained in:
Smith 2022-02-11 18:28:13 +01:00
parent 3d8f0eed79
commit a677ed0c4a
2 changed files with 46 additions and 3 deletions

41
Autoloader.php Normal file
View File

@ -0,0 +1,41 @@
<?php
/**
* 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
*
* i.e. require_once('/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
*/
spl_autoload_register(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;
}
});

View File

@ -543,14 +543,16 @@ json string at a time and then parse that using `Items::fromString()`... If even
there's probably no solution yet via JSON Machine. A feature is planned which will enable you to iterate
any structure fully recursively and strings will be served as streams.
<a name="installation"></a>
## Installation
```bash
composer require halaxa/json-machine
```
or clone or download this repository (not recommended because of no autoloading).
or clone or download this repository and add the following to your bootstrap file:
```
require_once('/path/to/json-machine/Autoloader.php');
```
The Autoloader.php file provides the same auto loading functionality as the Composer install.
<a name="development"></a>
## Development