Skip to content

Commit

Permalink
Merge pull request #1491 from hydephp/improve-phar-support
Browse files Browse the repository at this point in the history
Improve support for running HydePHP through the Phar archive
  • Loading branch information
caendesilva authored Dec 12, 2023
2 parents 6c478b3 + b470292 commit e361c3e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 19 deletions.
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

0 comments on commit e361c3e

Please sign in to comment.