Passed
Push — master ( 8444bd...a0a668 )
by Caen
03:19 queued 12s
created

PharSupport::running()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation;
6
7
use Hyde\Hyde;
8
use Phar;
9
use BadMethodCallException;
10
use function dirname;
11
use function is_dir;
12
use function rtrim;
13
use function str_replace;
14
15
/**
16
 * Provides experimental support for running the HydeCLI in a standalone Phar archive.
17
 *
18
 * @experimental
19
 *
20
 * @internal
21
 *
22
 * @see \Hyde\Framework\Testing\Feature\PharSupportTest
23
 */
24
class PharSupport
25
{
26
    /** @var array<string, bool> */
27
    protected static array $mocks = [];
28
29
    public static function mock(string $method, bool $value): void
30
    {
31
        self::$mocks[$method] = $value;
32
    }
33
34
    public static function clearMocks(): void
35
    {
36
        self::$mocks = [];
37
    }
38
39
    public static function running(): bool
40
    {
41
        return self::$mocks['running'] ?? Phar::running() !== '';
42
    }
43
44
    public static function hasVendorDirectory(): bool
45
    {
46
        return self::$mocks['hasVendorDirectory'] ?? is_dir(Hyde::path('vendor'));
47
    }
48
49
    public static function vendorPath(string $path = '', string $package = 'framework'): string
50
    {
51
        if ($package !== 'framework') {
52
            throw new BadMethodCallException('Cannot use vendorPath() outside of the framework package when running from a Phar archive.');
53
        }
54
55
        return rtrim(str_replace('/', DIRECTORY_SEPARATOR, rtrim(dirname(__DIR__, 2).'/'.$path, '/\\')), '/\\');
56
    }
57
}
58