Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve support for running HydePHP through the Phar archive #1491

Merged
merged 4 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
4 changes: 4 additions & 0 deletions packages/framework/src/Foundation/Kernel/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 1 addition & 6 deletions packages/framework/src/Foundation/PharSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Hyde\Hyde;
use Phar;
use BadMethodCallException;

use function dirname;
use function is_dir;
Expand Down Expand Up @@ -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, '/\\')), '/\\');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
11 changes: 0 additions & 11 deletions packages/framework/tests/Feature/PharSupportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Hyde\Framework\Testing\Feature;

use BadMethodCallException;
use Hyde\Foundation\PharSupport;
use Hyde\Hyde;
use Hyde\Testing\TestCase;
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion packages/realtime-compiler/src/Http/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading