From 13c92b26837445dcebd863792bfc4a7eca53d963 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 12 Dec 2023 16:27:30 +0100 Subject: [PATCH 1/4] Update PharSupport vendor path method to support more packages --- packages/framework/src/Foundation/PharSupport.php | 7 +------ packages/framework/tests/Feature/PharSupportTest.php | 11 ----------- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/packages/framework/src/Foundation/PharSupport.php b/packages/framework/src/Foundation/PharSupport.php index 7f76b02f0d4..7117df5b884 100644 --- a/packages/framework/src/Foundation/PharSupport.php +++ b/packages/framework/src/Foundation/PharSupport.php @@ -6,7 +6,6 @@ use Hyde\Hyde; use Phar; -use BadMethodCallException; use function dirname; use function is_dir; @@ -47,10 +46,6 @@ public static function hasVendorDirectory(): bool public static function vendorPath(string $path = '', string $package = 'framework'): string { - if ($package !== 'framework') { - throw new BadMethodCallException('Cannot use vendorPath() outside of the framework package when running from a Phar archive.'); - } - - return rtrim(str_replace('/', DIRECTORY_SEPARATOR, rtrim(dirname(__DIR__, 2).'/'.$path, '/\\')), '/\\'); + return rtrim(str_replace('/', DIRECTORY_SEPARATOR, rtrim(dirname(__DIR__, 3).'/'.$package.'/'.$path, '/\\')), '/\\'); } } diff --git a/packages/framework/tests/Feature/PharSupportTest.php b/packages/framework/tests/Feature/PharSupportTest.php index 8bca9b57805..f6e46f32241 100644 --- a/packages/framework/tests/Feature/PharSupportTest.php +++ b/packages/framework/tests/Feature/PharSupportTest.php @@ -4,7 +4,6 @@ namespace Hyde\Framework\Testing\Feature; -use BadMethodCallException; use Hyde\Foundation\PharSupport; use Hyde\Hyde; use Hyde\Testing\TestCase; @@ -65,16 +64,6 @@ public function test_vendor_path_can_run_in_phar_with_path_argument() $this->assertEquals($this->replaceSlashes(Hyde::path("{$this->getBaseVendorPath()}/framework/file.php")), Hyde::vendorPath('file.php')); } - public function test_vendor_path_can_run_in_phar_with_package_argument_but_throws() - { - PharSupport::mock('running', true); - PharSupport::mock('hasVendorDirectory', false); - - $this->expectException(BadMethodCallException::class); - - Hyde::vendorPath(package: 'realtime-compiler'); - } - protected function getBaseVendorPath(): string { // Monorepo support for symlinked packages directory From f2350e3f52ff5fb893585e8e9a7c9d8b3177929f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 12 Dec 2023 17:33:05 +0100 Subject: [PATCH 2/4] Update path helper to not format Phar paths This should resolve a lot of the issues we're having with the standalone, due to Phar paths being formatted like `'H:\monorepo/phar://foo'`, when they should be unchanged (`'phar://foo'`) --- packages/framework/src/Foundation/Kernel/Filesystem.php | 4 ++++ .../framework/tests/Feature/Foundation/FilesystemTest.php | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/packages/framework/src/Foundation/Kernel/Filesystem.php b/packages/framework/src/Foundation/Kernel/Filesystem.php index dcc8f79ed73..883663d3f31 100644 --- a/packages/framework/src/Foundation/Kernel/Filesystem.php +++ b/packages/framework/src/Foundation/Kernel/Filesystem.php @@ -56,6 +56,10 @@ public function path(string $path = ''): string return $this->getBasePath(); } + if (str_starts_with($path, 'phar://')) { + return $path; + } + $path = unslash($this->pathToRelative($path)); return path_join($this->getBasePath(), $path); diff --git a/packages/framework/tests/Feature/Foundation/FilesystemTest.php b/packages/framework/tests/Feature/Foundation/FilesystemTest.php index 30400cee8ff..a21c3ccd041 100644 --- a/packages/framework/tests/Feature/Foundation/FilesystemTest.php +++ b/packages/framework/tests/Feature/Foundation/FilesystemTest.php @@ -115,6 +115,11 @@ public function testPathMethodResolvesAlreadyAbsolutePathsUsingHelperWithTrailin $this->assertSame($this->filesystem->path('foo'), $this->filesystem->path($this->filesystem->path('foo/'))); } + public function testPathMethodDoesNotModifyPharPaths() + { + $this->assertSame('phar://foo', $this->filesystem->path('phar://foo')); + } + public function testHydePathMethodExists() { $this->assertTrue(method_exists(HydeKernel::class, 'path')); From 628b628bd0a4512bc1a5dfff7450f4081035faab Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 12 Dec 2023 17:50:50 +0100 Subject: [PATCH 3/4] Update dashboard to load scripts directly from Phar Update dashboard to inject scripts directly from archive when running in Phar --- .../realtime-compiler/src/Http/DashboardController.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/realtime-compiler/src/Http/DashboardController.php b/packages/realtime-compiler/src/Http/DashboardController.php index 15d30dacd67..08e15fb85a8 100644 --- a/packages/realtime-compiler/src/Http/DashboardController.php +++ b/packages/realtime-compiler/src/Http/DashboardController.php @@ -13,6 +13,7 @@ use Hyde\Pages\MarkdownPost; use Desilva\Microserve\Request; use Desilva\Microserve\Response; +use Hyde\Foundation\PharSupport; use Hyde\Pages\Concerns\HydePage; use Hyde\Pages\DocumentationPage; use Hyde\Support\Models\RouteKey; @@ -261,7 +262,11 @@ public function isInteractive(): bool public function getScripts(): string { - return file_get_contents(__DIR__.'/../../resources/dashboard.js'); + if (PharSupport::running()) { + return file_get_contents('phar://hyde.phar/vendor/hyde/realtime-compiler/resources/dashboard.js'); + } + + return file_get_contents(Hyde::vendorPath('resources/dashboard.js', 'realtime-compiler')); } public function getFlash(string $key, $default = null): ?string From b47029289e08389016b1bc6871f21657a9210e15 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 12 Dec 2023 17:53:05 +0100 Subject: [PATCH 4/4] Update RELEASE_NOTES.md --- RELEASE_NOTES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index e3d1b60364c..5e075dcdba1 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -22,7 +22,7 @@ This serves two purposes: - for now removed features. ### Fixed -- for any bug fixes. +- Improved support for running HydePHP through the Phar archive in https://github.com/hydephp/develop/pull/1491 ### Security - in case of vulnerabilities.