diff --git a/bin/blade b/bin/blade index 3695fa6..8feea41 100755 --- a/bin/blade +++ b/bin/blade @@ -11,7 +11,7 @@ use Symfony\Component\Console\ConsoleEvents; use Symfony\Component\EventDispatcher\EventDispatcher; // Create App -$application = new Application('Blade CLI', '1.0.1'); +$application = new Application('Blade CLI', '1.1.0'); $dispatcher = new EventDispatcher(); //Add compile command diff --git a/composer.json b/composer.json index ed4e958..a35eb22 100644 --- a/composer.json +++ b/composer.json @@ -23,8 +23,8 @@ }, "scripts": { "build": "mkdir -p dist && php ./phar-composer.phar build . dist", - "test": "php bin/blade test/index.blade.php --data=test/variables.json --data=test/variables.php --data='{\"nameString\":\"John Doe\"}'", - "build:test": "php dist/blade-cli.phar test/index.blade.php --data=test/variables.json --data=test/variables.php --data='{\"nameString\":\"John Doe\"}'", + "test": "php bin/blade test/index.blade.php --data=test/variables.json --data=test/variables.php --data='{\"nameString\":\"John Doe\"}' --extend=test/extensions.php", + "build:test": "php dist/blade-cli.phar test/index.blade.php --data=test/variables.json --data=test/variables.php --data='{\"nameString\":\"John Doe\"}' --extend=test/extensions.php", "build:install": "sudo cp dist/blade-cli.phar /usr/bin/blade && sudo chmod +x /usr/bin/blade" }, "bin": ["bin/blade"] diff --git a/src/Command/Compile.php b/src/Command/Compile.php index cec814d..a5ab3b1 100644 --- a/src/Command/Compile.php +++ b/src/Command/Compile.php @@ -56,6 +56,13 @@ protected function configure() 'txt' ); + $this->addOption( + 'extend', + null, + InputOption::VALUE_REQUIRED, + 'This option accepts a path to a PHP file with user code to extend the compiler by using $compiler->extend()' + ); + $this->addOption( 'dynamic-base', null, @@ -70,6 +77,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $dataPaths = $input->getOption('data'); $outputDir = $input->getOption('output-dir'); $outputExt = $input->getOption('output-ext'); + $extend = $input->getOption('extend'); $baseDirs = $input->getOption('base-dir'); $dynamicBase = $input->getOption('dynamic-base'); @@ -124,6 +132,10 @@ protected function execute(InputInterface $input, OutputInterface $output) $cacheDir = sys_get_temp_dir().'/blade/views'; $blade = new Compiler($cacheDir, $baseDirs); + if (file_exists($extend)) { + includeExtensions($blade, $extend); + } + // Loop through all templates foreach ($templates as $template) { // Compile template @@ -140,7 +152,11 @@ protected function execute(InputInterface $input, OutputInterface $output) $dynamicBase = ($dynamicBase == '.') ? getcwd() : $dynamicBase; $dynamicDirs = array_merge($baseDirs, [$dynamicBase]); - $blade = new Compiler($cacheDir, $dynamicDirs); + $bladeDyn = new Compiler($cacheDir, $dynamicDirs); + + if (file_exists($extend)) { + includeExtensions($bladeDyn, $extend); + } $compiled = $blade->render($reference, $data); } @@ -267,3 +283,7 @@ protected function writeFile($path, $content) { } } } + +function includeExtensions(&$compiler, $path) { + include $path; +} diff --git a/test/extensions.php b/test/extensions.php new file mode 100644 index 0000000..c0b40bd --- /dev/null +++ b/test/extensions.php @@ -0,0 +1,5 @@ +extend('no', function ($expression) { + return ""; +}); diff --git a/test/index.blade.php b/test/index.blade.php index 3ff4e8d..7fd1d1e 100644 --- a/test/index.blade.php +++ b/test/index.blade.php @@ -14,4 +14,7 @@ JSON string variables are loaded? {{ isset($nameString) ? 'yes: name = '.$nameString : 'no' }} +Extensions are loaded? +@no("yes") + @endsection