Skip to content

Commit

Permalink
Added --extend
Browse files Browse the repository at this point in the history
  • Loading branch information
nsrosenqvist committed Mar 15, 2017
1 parent 509b26f commit 7313e37
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bin/blade
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -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"]
Expand Down
22 changes: 21 additions & 1 deletion src/Command/Compile.php
Expand Up @@ -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,
Expand All @@ -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');

Expand Down Expand Up @@ -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
Expand All @@ -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);
}
Expand Down Expand Up @@ -267,3 +283,7 @@ protected function writeFile($path, $content) {
}
}
}

function includeExtensions(&$compiler, $path) {
include $path;
}
5 changes: 5 additions & 0 deletions test/extensions.php
@@ -0,0 +1,5 @@
<?php

$compiler->extend('no', function ($expression) {
return "<?php echo $expression; ?>";
});
3 changes: 3 additions & 0 deletions test/index.blade.php
Expand Up @@ -14,4 +14,7 @@
JSON string variables are loaded?
{{ isset($nameString) ? 'yes: name = '.$nameString : 'no' }}

Extensions are loaded?
@no("yes")

@endsection

0 comments on commit 7313e37

Please sign in to comment.