Skip to content

Commit 1e0e85f

Browse files
committed
Add custom loaders
1 parent bd07e1a commit 1e0e85f

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

index.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use TweakPHP\Client\Cli;
34
use TweakPHP\Client\Loader;
45

56
require __DIR__.'/vendor/autoload.php';
@@ -11,7 +12,8 @@
1112
exit(1);
1213
}
1314

14-
$loader = Loader::load($arguments[1]);
15+
$customLoader = Cli::getArgument('loader');
16+
$loader = Loader::load($arguments[1], $customLoader);
1517

1618
if ($loader === null) {
1719
echo 'Invalid path'.PHP_EOL;

src/Cli.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace TweakPHP\Client;
4+
5+
class Cli
6+
{
7+
public static function getArgument(string $argument): string
8+
{
9+
$arguments = $_SERVER['argv'] ?? [];
10+
11+
foreach ($arguments as $arg) {
12+
if (strpos($arg, "--$argument=") === 0) {
13+
return substr($arg, strlen("--$argument="));
14+
}
15+
}
16+
17+
return '';
18+
}
19+
}

src/Loader.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@ class Loader
1414
/**
1515
* @return null|LoaderInterface
1616
*/
17-
public static function load(string $path)
17+
public static function load(string $path, ?string $loaderPath = null)
1818
{
19+
if ($loaderPath !== null) {
20+
$loaderClass = self::getLoaderClassFromPath($loaderPath);
21+
22+
if ($loaderClass !== null) {
23+
return new $loaderClass($path);
24+
}
25+
}
26+
1927
if (LaravelLoader::supports($path)) {
2028
return new LaravelLoader($path);
2129
}
@@ -38,4 +46,25 @@ public static function load(string $path)
3846

3947
return null;
4048
}
49+
50+
private static function getLoaderClassFromPath(string $path)
51+
{
52+
if (! file_exists($path)) {
53+
return null;
54+
}
55+
56+
$declaredClassesBefore = get_declared_classes();
57+
58+
require_once $path;
59+
60+
$declaredClassesAfter = get_declared_classes();
61+
62+
$newClasses = array_diff($declaredClassesAfter, $declaredClassesBefore);
63+
64+
if (empty($newClasses)) {
65+
return null;
66+
}
67+
68+
return reset($newClasses);
69+
}
4170
}

0 commit comments

Comments
 (0)