Skip to content

Commit 536db47

Browse files
aazsamirbrendt
andauthored
feat(core): support booting in phar (#1672)
Co-authored-by: Brent Roose <[email protected]>
1 parent 1602654 commit 536db47

File tree

13 files changed

+53
-16
lines changed

13 files changed

+53
-16
lines changed

packages/console/src/Highlight/TempestConsoleLanguage/Injections/FileInjection.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Tempest\Highlight\Injection;
99
use Tempest\Highlight\ParsedInjection;
1010
use Tempest\Highlight\Themes\TerminalStyle;
11+
use Tempest\Support\Filesystem;
1112

1213
use function Tempest\root_path;
1314
use function Tempest\Support\str;
@@ -21,9 +22,9 @@ public function parse(string $content, Highlighter $highlighter): ParsedInjectio
2122
pattern: '/(?<match>\<file=(?<quote>[\"\'])(?<file>.+)\k<quote>\s*\/?>)/',
2223
callback: function (array $matches) {
2324
$href = $matches['file'];
24-
$exists = realpath($href) !== false;
25+
$exists = Filesystem\normalize_path($href) !== null;
2526
$file = $exists
26-
? str(realpath($href))->replace('\\', '/')->stripStart(root_path())->stripStart('/')
27+
? str(Filesystem\normalize_path($href))->replace('\\', '/')->stripStart(root_path())->stripStart('/')
2728
: $href;
2829

2930
return TerminalStyle::UNDERLINE((string) $file);

packages/core/src/Commands/DiscoveryStatusCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Tempest\Core\DiscoveryCache;
1111
use Tempest\Core\DiscoveryCacheStrategy;
1212
use Tempest\Core\Kernel;
13+
use Tempest\Support\Filesystem;
1314

1415
use function Tempest\root_path;
1516
use function Tempest\Support\str;
@@ -62,7 +63,7 @@ public function __invoke(
6263
$this->console->writeln();
6364

6465
foreach ($this->kernel->discoveryLocations as $discoveryLocation) {
65-
$path = str(realpath($discoveryLocation->path))
66+
$path = str(Filesystem\normalize_path($discoveryLocation->path))
6667
->replaceStart(root_path(), '.')
6768
->toString();
6869

packages/core/src/DiscoveryConfig.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Tempest\Core;
44

5+
use Tempest\Support\Filesystem;
6+
57
final class DiscoveryConfig
68
{
79
private array $skipDiscovery = [];
@@ -25,9 +27,9 @@ public function skipPaths(string ...$paths): self
2527
foreach ($paths as $path) {
2628
$path = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path);
2729

28-
$realpath = realpath($path);
30+
$realpath = Filesystem\normalize_path($path);
2931

30-
if ($realpath === false) {
32+
if ($realpath === null) {
3133
continue;
3234
}
3335

packages/core/src/FrameworkKernel.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Tempest\EventBus\EventBus;
1919
use Tempest\Process\GenericProcessExecutor;
2020
use Tempest\Router\Exceptions\HttpExceptionHandler;
21+
use Tempest\Support\Filesystem;
2122

2223
final class FrameworkKernel implements Kernel
2324
{
@@ -34,14 +35,20 @@ public function __construct(
3435
/** @var \Tempest\Discovery\DiscoveryLocation[] $discoveryLocations */
3536
public array $discoveryLocations = [],
3637
?Container $container = null,
38+
?string $internalStorage = null,
3739
) {
3840
$this->container = $container ?? $this->createContainer();
41+
42+
if ($internalStorage !== null) {
43+
$this->internalStorage = $internalStorage;
44+
}
3945
}
4046

4147
public static function boot(
4248
string $root,
4349
array $discoveryLocations = [],
4450
?Container $container = null,
51+
?string $internalStorage = null,
4552
): self {
4653
if (! defined('TEMPEST_START')) {
4754
define('TEMPEST_START', value: hrtime(true));
@@ -51,6 +58,7 @@ public static function boot(
5158
root: $root,
5259
discoveryLocations: $discoveryLocations,
5360
container: $container,
61+
internalStorage: $internalStorage,
5462
)
5563
->validateRoot()
5664
->loadEnv()
@@ -68,7 +76,7 @@ public static function boot(
6876

6977
public function validateRoot(): self
7078
{
71-
$root = realpath($this->root);
79+
$root = Filesystem\normalize_path($this->root);
7280

7381
if (! is_dir($root)) {
7482
throw new RuntimeException('The specified root directory is not valid.');
@@ -175,7 +183,7 @@ public function loadConfig(): self
175183

176184
public function registerInternalStorage(): self
177185
{
178-
$path = $this->root . '/.tempest';
186+
$path = isset($this->internalStorage) ? $this->internalStorage : $this->root . '/.tempest';
179187

180188
if (! is_dir($path)) {
181189
if (file_exists($path)) {
@@ -189,7 +197,7 @@ public function registerInternalStorage(): self
189197
throw CouldNotRegisterInternalStorage::noPermission($path);
190198
}
191199

192-
$this->internalStorage = realpath($path);
200+
$this->internalStorage = Filesystem\normalize_path($path);
193201

194202
return $this;
195203
}

packages/core/src/Kernel/LoadConfig.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Tempest\Core\ConfigCache;
99
use Tempest\Core\Kernel;
1010
use Tempest\Support\Arr\MutableArray;
11+
use Tempest\Support\Filesystem;
1112
use Tempest\Support\Str;
1213

1314
/** @internal */
@@ -85,10 +86,10 @@ public function find(): array
8586
*/
8687
private function scan(string $path, MutableArray $configPaths): void
8788
{
88-
$input = realpath($path);
89+
$input = Filesystem\normalize_path($path);
8990

9091
// Make sure the path is valid
91-
if ($input === false) {
92+
if ($input === null) {
9293
return;
9394
}
9495

packages/core/src/Kernel/LoadDiscoveryClasses.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Tempest\Discovery\DiscoveryLocation;
1717
use Tempest\Discovery\SkipDiscovery;
1818
use Tempest\Reflection\ClassReflector;
19+
use Tempest\Support\Filesystem;
1920
use Throwable;
2021

2122
/** @internal */
@@ -105,10 +106,10 @@ private function discover(array $discoveries): void
105106
*/
106107
private function scan(DiscoveryLocation $location, array $discoveries, string $path): void
107108
{
108-
$input = realpath($path);
109+
$input = Filesystem\normalize_path($path);
109110

110111
// Make sure the path is valid
111-
if ($input === false) {
112+
if ($input === null) {
112113
return;
113114
}
114115

packages/core/src/Tempest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ public static function boot(
1212
?string $root = null,
1313
/** @var \Tempest\Discovery\DiscoveryLocation[] $discoveryLocations */
1414
array $discoveryLocations = [],
15+
?string $internalStorage = null,
1516
): Container {
1617
$root ??= getcwd();
1718

1819
// Kernel
1920
return FrameworkKernel::boot(
2021
root: $root,
2122
discoveryLocations: $discoveryLocations,
23+
internalStorage: $internalStorage,
2224
)->container;
2325
}
2426
}

packages/discovery/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"minimum-stability": "dev",
66
"require": {
77
"php": "^8.4",
8-
"tempest/reflection": "dev-main"
8+
"tempest/reflection": "dev-main",
9+
"tempest/support": "dev-main"
910
},
1011
"autoload": {
1112
"psr-4": {

packages/discovery/src/DiscoveryLocation.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Tempest\Discovery;
66

7+
use Tempest\Support\Filesystem;
8+
79
final class DiscoveryLocation
810
{
911
public readonly string $namespace;
@@ -18,7 +20,7 @@ public function __construct(
1820
string $path,
1921
) {
2022
$this->namespace = $namespace;
21-
$this->path = realpath(rtrim($path, '\\/'));
23+
$this->path = Filesystem\normalize_path(rtrim($path, '\\/'));
2224
}
2325

2426
public function isVendor(): bool

packages/support/src/Filesystem/functions.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,3 +503,15 @@ function read_symbolic_link(string $path): string
503503

504504
return $result;
505505
}
506+
507+
/**
508+
* Returns the real path for the specified $path or null if it doesn't exist.
509+
*/
510+
function normalize_path(string $path): ?string
511+
{
512+
if (class_exists(\Phar::class) && \Phar::running(false) !== '' && str_starts_with($path, 'phar:')) {
513+
return $path;
514+
}
515+
516+
return realpath($path) ?: null;
517+
}

0 commit comments

Comments
 (0)