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

Repack - Add local box.json support #318

Merged
merged 1 commit into from Mar 10, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

* Deprecate `Context::withPath()` in favor of `Context::withCurrentDirectory()`
* Deprecate `path` argument in `capture()`, `exit_code()`, `run()`, `with()` in favor of `currentDirectory`
* Add a way to merge an application `box.json` config file used by `castor:repack`command

## 0.14.0 (2024-03-08)

Expand Down
5 changes: 5 additions & 0 deletions doc/going-further/extending-castor/repack.md
Expand Up @@ -39,6 +39,11 @@ vendor/bin/castor repack --help
> So ensure to have the less files possible in the directory where you run the
> repack task to avoid including useless files in the phar.

> [!NOTE]
> If a `box.json` file exists in your application directory,
> it will be merged with the config file used by Castor.
> None of this keys `base-path`, `main`, `alias` or `output` keys can be defined in your application box config.

## Going further

Packaging your Castor app as a phar simplifies distribution but requires PHP
Expand Down
18 changes: 18 additions & 0 deletions src/Console/Command/RepackCommand.php
Expand Up @@ -101,6 +101,24 @@ class RepackedApplication extends Application
...$boxConfig['files'] ?? [],
];

if (file_exists($appBoxConfigFile = PathHelper::getRoot() . '/box.json')) {
$appBoxConfig = json_decode((string) file_get_contents($appBoxConfigFile), true, 512, \JSON_THROW_ON_ERROR);

if (
\array_key_exists('base-path', $appBoxConfig)
|| \array_key_exists('main', $appBoxConfig)
|| \array_key_exists('alias', $appBoxConfig)
|| \array_key_exists('output', $appBoxConfig)
) {
throw new \RuntimeException('Application box config could not contains one of this keys: base-path, main, alias or output.');
}

$boxConfig = array_merge_recursive(
$boxConfig,
$appBoxConfig,
);
}

file_put_contents('.box.json', json_encode($boxConfig, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES));
file_put_contents('.main.php', $main);

Expand Down