## Getting Started
### Minimal Setup
1. **Install the Bundle**:
```bash
composer require efrane/phar-builder-bundle
Register in config/bundles.php:
EFrane\PharBuilder\Bundle\PharBuilderBundle::class => ['all' => true],
First Use Case: CLI Command for PHAR Creation
Create a custom command extending EFrane\PharBuilder\Command\AbstractPharCommand:
use EFrane\PharBuilder\Command\AbstractPharCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
namespace App\Command;
class BuildAppPhar extends AbstractPharCommand
{
protected function configure(): void
{
$this->setName('app:build-phar');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->buildPhar(
'app.phar',
['src/', 'vendor/autoload.php'],
['--web-root', 'public']
);
return Command::SUCCESS;
}
}
Run with:
php bin/console app:build-phar
Key Configuration
Check config/packages/phar_builder.yaml for default settings (e.g., stub templates, excluded files).
Dynamic File Inclusion
Use addFiles() with glob patterns or arrays:
$this->buildPhar('app.phar', [
'src/**/*.php',
'config/packages/*.yaml',
'public/assets/**/*'
]);
Stub Customization Override the default PHAR stub by:
stub.txt in config/packages/phar_builder/ or$this->buildPhar('app.phar', [], [], 'custom-stub.txt');
Dependency Management Exclude unnecessary files (e.g., tests, dev dependencies):
$this->buildPhar('app.phar', ['src/', 'vendor/'], [
'--exclude' => 'tests/*',
'--exclude' => 'vendor/*/Tests/*'
]);
Web Root Handling For web-accessible PHARs, specify the web root:
$this->buildPhar('app.phar', ['src/', 'public/'], [
'--web-root', 'public'
]);
Post-Build Hooks
Extend AbstractPharCommand to run scripts after PHAR creation:
protected function postBuild(string $pharPath): void
{
$this->getOutput()->writeln(sprintf('PHAR created at %s', $pharPath));
// Example: Sign the PHAR or upload it
}
Environment-Specific Builds Use Symfony’s environment-aware services to conditionally include files:
$files = $this->getParameter('kernel.environment') === 'prod'
? ['src/', 'public/prod-assets/']
: ['src/', 'public/dev-assets/'];
composer.json:
{
"scripts": {
"build-phar": "php bin/console app:build-phar"
}
}
- run: php bin/console app:build-phar
Alpha Stage Limitations
PHAR Security Risks
// Example: Sanitize stub content
$stub = file_get_contents('stub.txt');
if (strpos($stub, 'eval(') !== false) {
throw new \RuntimeException('Stub contains unsafe eval() call.');
}
.env, composer.lock) are excluded:
$this->buildPhar('app.phar', [], [
'--exclude' => '.env',
'--exclude' => 'composer.lock'
]);
Dependency Conflicts
phar.io/phar and symfony/console. Conflicts may arise with other PHAR-related packages.replace or conflict directives.Web Root Misconfiguration
--web-root flags can break PHAR execution. Verify the path is relative to the PHAR’s root.File Permissions
chmod +x app.phar
$this->setDebug(true);
$this->buildPhar('app.phar', [], ['--dry-run']);
$this->logger->info('Building PHAR with files:', ['files' => $files]);
Custom Builders
Extend EFrane\PharBuilder\Builder\BuilderInterface for specialized logic (e.g., signing, compression):
namespace App\Builder;
use EFrane\PharBuilder\Builder\BuilderInterface;
class CustomBuilder implements BuilderInterface
{
public function build(string $pharPath, array $files, array $options): void
{
// Custom logic (e.g., add a signature)
}
}
Register as a service and pass to buildPhar() via options:
$this->buildPhar('app.phar', [], [], [], new CustomBuilder());
Stub Templates Override the default stub by:
stub.txt in config/packages/phar_builder/ or# config/services.yaml
services:
App\Stub\DynamicStubGenerator:
arguments:
$environment: '%kernel.environment%'
Pre/Post-Build Events
Subscribe to Symfony events (e.g., console.command) to hook into the build lifecycle:
use Symfony\Component\Console\Event\ConsoleCommandEvent;
$eventDispatcher->addListener(ConsoleCommandEvent::class, function (ConsoleCommandEvent $event) {
if ($event->getCommand()->getName() === 'app:build-phar') {
// Pre-build logic
}
});
stub.txt in:
config/packages/phar_builder/Resources/stubs/ directory (fallback).--web-root) are passed directly to phar.io/phar. Refer to its docs for supported flags.$this->buildPhar(
$this->getParameter('phar.output_dir').'/app.phar',
[$this->getParameter('kernel.project_dir').'/src/']
);
```markdown
### Pro Tips
1. **PHAR Signing**: Integrate with `webmozart/php-signature` to sign PHARs post-build:
```php
use Webmozart\PhpSignature\Signature;
protected function postBuild(string $pharPath): void
{
$signature = new Signature();
$signature->signFile($pharPath, 'private.key');
}
Versioned PHARs: Append Git hashes or timestamps to filenames:
$pharName = sprintf('app-%s.phar', exec('git rev-parse --short HEAD'));
$this->buildPhar($pharName, ['src/']);
Dependency Optimization: Use phar.io/phar's --exclude to strip dev dependencies:
$this->buildPhar('app.phar', [], [
'--exclude' => 'vendor/*/Tests/*',
'--exclude' => 'vendor/*/Resources/*'
]);
Cross-Platform Paths: Normalize paths for Windows/Linux compatibility:
How can I help you explore Laravel packages today?