]> BookStack Code Mirror - system-cli/blob - src/Services/AppLocator.php
Bumped version
[system-cli] / src / Services / AppLocator.php
1 <?php declare(strict_types=1);
2
3 namespace Cli\Services;
4
5 use Phar;
6
7 class AppLocator
8 {
9     public static function search(string $directory = ''): string
10     {
11         $directoriesToSearch = $directory ? [Paths::resolve($directory)] : [
12             getcwd(),
13             static::getCliDirectory(),
14         ];
15
16         foreach ($directoriesToSearch as $directory) {
17             if ($directory && static::isProbablyAppDirectory($directory)) {
18                 return $directory;
19             }
20         }
21
22         return '';
23     }
24
25     public static function require(string $directory = ''): string
26     {
27         $dir = static::search($directory);
28
29         if (!$dir) {
30             throw new \Exception('Could not find a valid BookStack installation');
31         }
32
33         return $dir;
34     }
35
36     public static function getVersion(string $directory): string
37     {
38         $versionPath = $directory . DIRECTORY_SEPARATOR . 'version';
39         return trim(file_get_contents($versionPath));
40     }
41
42     protected static function getCliDirectory(): string
43     {
44         $scriptDir = dirname(__DIR__);
45
46         if (str_starts_with($scriptDir, 'phar://')) {
47             return dirname(Phar::running(false));
48         }
49
50         return dirname($scriptDir);
51     }
52
53     protected static function isProbablyAppDirectory(string $directory): bool
54     {
55         return file_exists($directory . DIRECTORY_SEPARATOR . 'version')
56             && file_exists($directory . DIRECTORY_SEPARATOR . 'package.json');
57     }
58 }