Skip to content

Commit 251753a

Browse files
committed
Fix autoloading for tools/cli-toolkit when being included into projects.
1 parent ea39bf6 commit 251753a

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

docs/changelog.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22

33
This change log references the repository changes and releases, which respect [semantic versioning](https://semver.org).
44

5-
## v2.0.1
5+
## v2.1.0
6+
7+
### New features
8+
9+
1. [AutoloadDetector.php](../tools/cli-toolkit/Classes/AutoloadDetector.php) is added for
10+
[init.php](../tools/cli-toolkit/init.php).
11+
12+
Previously all built-in scripts ([cli-toolkit](../tools/cli-toolkit)) could not be launched without calling
13+
`composer install` additionally inside the library directory.
14+
And from now on your main project's `vendor/autoload.php` path should be detectable.
615

716
### Patches
817

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MagicPush\CliToolkit\Tools\CliToolkit\Classes;
6+
7+
class AutoloadDetector {
8+
public static function detectAndRequire(): void {
9+
$currentDirPath = __DIR__;
10+
$detectedPath = null;
11+
while (true) {
12+
$autoloaderPath = $currentDirPath . '/vendor/autoload.php';
13+
if (file_exists($autoloaderPath)) {
14+
require_once $autoloaderPath;
15+
16+
return;
17+
}
18+
19+
$previousDirPath = $currentDirPath;
20+
$currentDirPath = dirname($previousDirPath);
21+
// We can't go higher than a filesystem's top:
22+
if ($currentDirPath === $previousDirPath) {
23+
return;
24+
}
25+
}
26+
}
27+
}

tools/cli-toolkit/init.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
declare(strict_types=1);
44

5-
require_once __DIR__ . '/../../vendor/autoload.php';
5+
use MagicPush\CliToolkit\Tools\CliToolkit\Classes\AutoloadDetector;
6+
7+
require_once __DIR__ . '/Classes/AutoloadDetector.php';
8+
9+
AutoloadDetector::detectAndRequire();
610

711
mb_internal_encoding('UTF-8');
812
setlocale(LC_ALL, 'en_US.UTF-8');

0 commit comments

Comments
 (0)