Skip to content

Commit

Permalink
Improve bundled autoloader
Browse files Browse the repository at this point in the history
  • Loading branch information
Riimu committed Jul 15, 2017
1 parent 8cbaa9d commit 6d3a159
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 25 deletions.
8 changes: 2 additions & 6 deletions examples/convert.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
<?php

if ($argc < 4) {
echo "Usage: convert <number> <source-base> <target-base> [precision]" . PHP_EOL;
echo 'Usage: php convert.php <number> <source-base> <target-base> [precision]' . PHP_EOL;
die;
}

require __DIR__ . '/../src/Converter.php';
require __DIR__ . '/../src/NumberBase.php';
require __DIR__ . '/../src/DecimalConverter.php';
require __DIR__ . '/../src/ReplaceConverter.php';
require __DIR__ . '/../src/BaseConverter.php';
require __DIR__ . '/../src/autoload.php';

$source = new Riimu\Kit\BaseConversion\NumberBase(is_numeric($argv[2]) ? (int) $argv[2] : $argv[2]);
$target = new Riimu\Kit\BaseConversion\NumberBase(is_numeric($argv[3]) ? (int) $argv[3] : $argv[3]);
Expand Down
14 changes: 5 additions & 9 deletions examples/efficiency.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
<?php

if ($argc < 3) {
echo "Usage efficiency.php <repeats> <length> [source-base] [target-base]" . PHP_EOL;
echo 'Usage php efficiency.php <repeats> <length> [source-base] [target-base]' . PHP_EOL;
exit;
}

require __DIR__ . '/../src/Converter.php';
require __DIR__ . '/../src/NumberBase.php';
require __DIR__ . '/../src/DecimalConverter.php';
require __DIR__ . '/../src/ReplaceConverter.php';
require __DIR__ . '/../src/BaseConverter.php';
require __DIR__ . '/../src/autoload.php';

echo "Test for efficiency of different algorithms available:" . PHP_EOL;
echo 'Test for efficiency of different algorithms available:' . PHP_EOL;

$sbase = isset($argv[3]) ? (ctype_digit($argv[3]) ? intval($argv[3]) : $argv[3]) : 2;
$tbase = isset($argv[4]) ? (ctype_digit($argv[4]) ? intval($argv[4]) : $argv[4]) : 16;
Expand Down Expand Up @@ -75,6 +71,6 @@ function ticker () {

echo "\nReplace Conversion:\n\n";

$doTrial('Riimu\Kit\BaseConversion\ReplaceConverter');
$doTrial('Riimu\Kit\BaseConversion\DecimalConverter');
$doTrial(\Riimu\Kit\BaseConversion\ReplaceConverter::class);
$doTrial(\Riimu\Kit\BaseConversion\DecimalConverter::class);

8 changes: 2 additions & 6 deletions examples/test.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<?php

require __DIR__ . '/../src/Converter.php';
require __DIR__ . '/../src/NumberBase.php';
require __DIR__ . '/../src/DecimalConverter.php';
require __DIR__ . '/../src/ReplaceConverter.php';
require __DIR__ . '/../src/BaseConverter.php';
require __DIR__ . '/../src/autoload.php';

$converter = new Riimu\Kit\BaseConversion\BaseConverter(10, 16);
echo $converter->convert('42') . PHP_EOL; // Will output '2A'
Expand Down Expand Up @@ -32,4 +28,4 @@
echo $converter->convert('0.A7') . PHP_EOL; // Outputs '0.101100101010000110'

$converter = new Riimu\Kit\BaseConversion\BaseConverter(10, '0123456789abcdef');
echo $converter->convert('42'); // Will output '2a'
echo $converter->convert('42'); // Will output '2a'
10 changes: 6 additions & 4 deletions src/autoload.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

// Autoloader for loading the library when composer is not available
namespace Riimu\Kit\BaseConversion;

// Bundled autoloader provided as an optional alternative to composer autoloader
spl_autoload_register(function ($class) {
if (strncmp($class, $ns = 'Riimu\\Kit\\BaseConversion\\', strlen($ns)) === 0) {
$file = substr_replace($class, '\\', 0, strlen($ns)) . '.php';
$path = __DIR__ . str_replace('\\', DIRECTORY_SEPARATOR, $file);
if (strncmp($class, __NAMESPACE__, strlen(__NAMESPACE__)) === 0) {
$path = __DIR__ . strtr(substr($class, strlen(__NAMESPACE__)), ['\\' => DIRECTORY_SEPARATOR]) . '.php';

if (file_exists($path)) {
require $path;
}
Expand Down

0 comments on commit 6d3a159

Please sign in to comment.