/** * Execute the console command. * * @return void */ public function fire() { // Set directories $inputPath = base_path('resources/views'); $outputPath = storage_path('gettext'); // Create $outputPath or empty it if already exists if (File::isDirectory($outputPath)) { File::cleanDirectory($outputPath); } else { File::makeDirectory($outputPath); } // Configure BladeCompiler to use our own custom storage subfolder $compiler = new BladeCompiler(new Filesystem(), $outputPath); $compiled = 0; // Get all view files $allFiles = File::allFiles($inputPath); foreach ($allFiles as $f) { // Skip not blade templates $file = $f->getPathName(); if ('.blade.php' !== substr($file, -10)) { continue; } // Compile the view $compiler->compile($file); $compiled++; // Rename to human friendly $human = str_replace(DIRECTORY_SEPARATOR, '-', ltrim($f->getRelativePathname(), DIRECTORY_SEPARATOR)); File::move($outputPath . DIRECTORY_SEPARATOR . sha1($file) . '.php', $outputPath . DIRECTORY_SEPARATOR . $human); } if ($compiled) { $this->info("{$compiled} files compiled."); } else { $this->error('No .blade.php files found in ' . $inputPath); } }
/** * Add extra directives to the blade templating compiler. * * @param BladeCompiler $blade The compiler to extend * * @return void */ public static function registerDirectives(BladeCompiler $blade) { $keywords = ["namespace", "use"]; foreach ($keywords as $keyword) { $blade->directive($keyword, function ($parameter) use($keyword) { $parameter = trim($parameter, "()"); return "<?php {$keyword} {$parameter} ?>"; }); } $assetify = function ($file, $type) { $file = trim($file, "()"); if (in_array(substr($file, 0, 1), ["'", '"'], true)) { $file = trim($file, "'\""); } else { return "{{ {$file} }}"; } if (substr($file, 0, 1) !== "/") { $file = "/{$type}/{$file}"; } if (substr($file, (strlen($type) + 1) * -1) !== ".{$type}") { $file .= ".{$type}"; } return $file; }; $blade->directive("css", function ($parameter) use($assetify) { $file = $assetify($parameter, "css"); return "<link rel='stylesheet' type='text/css' href='{$file}'>"; }); $blade->directive("js", function ($parameter) use($assetify) { $file = $assetify($parameter, "js"); return "<script type='text/javascript' src='{$file}'></script>"; }); }
/** * Register the custom Blade directive * * @param \Illuminate\View\Compilers\BladeCompiler $compiler */ private function registerBladeDirective($compiler) { $compiler->directive('notifications', function () { $expression = 'Gloudemans\\Notify\\Notifications\\Notification'; return "<?php echo app('{$expression}')->render(); ?>"; }); }
/** * Template constructor. * * @param Container $container */ public function __construct(Container $container) { $settings = $container->get('settings'); $compiledPath = $settings['storagePath'] . DIRECTORY_SEPARATOR . 'views'; $resolver = new EngineResolver(); $resolver->register('blade', function () use($compiledPath, &$settings) { $bladeCompiler = new BladeCompiler(new Filesystem(), $compiledPath); // Add the @webroot directive. $bladeCompiler->directive('webroot', function ($expression) use(&$settings) { $segments = explode(',', preg_replace("/[\\(\\)\\\"\\']/", '', $expression)); $path = rtrim($settings['webrootBasePath'], '/') . '/' . ltrim($segments[0], '/'); $path = str_replace("'", "\\'", $path); return "<?= e('{$path}') ?>"; }); // Add the @route directive. $bladeCompiler->directive('route', function ($expression) use(&$settings) { $segments = explode(',', preg_replace("/[\\(\\)\\\"\\']/", '', $expression)); $path = rtrim($settings['routeBasePath'], '/') . '/' . ltrim($segments[0], '/'); $path = str_replace("'", "\\'", $path); return "<?= e('{$path}') ?>"; }); return new CompilerEngine($bladeCompiler); }); $finder = new FileViewFinder(new Filesystem(), [$settings['templatePath']]); $factory = new ViewFactory($resolver, $finder, new Dispatcher()); $this->factory = $factory; $this->container = $container; }
public function testStatements() { $this->bladedManager->expects($this->any())->method('getCommand')->will($this->returnValue($this)); $this->appMock->expects($this->any())->method('make')->will($this->returnCallback(function ($resolvable) { switch ($resolvable) { case 'view': return $this->viewMock; case 'laravel-commode.bladed': return $this->bladedManager; case BladedServiceProvider::PROVIDES_STRING_COMPILER: return $this->stringCompiler; default: var_dump($resolvable, 'in testStatements'); die; } })); foreach (['condition', 'statement', 'templates'] as $file) { $this->assertSame(bl_get_contents('templates/results' . ucfirst($file)), $this->bladeCompiler->compileString(bl_get_contents("templates/{$file}.blade._php"))); } $expect = bl_get_contents('templates/resultsIterators'); $compiled = $this->bladeCompiler->compileString(bl_get_contents("templates/iterators.blade._php")); preg_match_all('/(\\$count[\\d\\w]{1,})/is', $compiled, $countVars); $countVars = array_values($countVars[0]); preg_match_all('/(\\$key[\\d\\w]{1,})/is', $compiled, $keyVars); $keyVars = array_values($keyVars[0]); $countIteration = 0; $keyIteration = 0; $expect = preg_replace_callback('/\\{\\{countVar\\}\\}/is', function ($match) use($countVars, &$countIteration) { return $countVars[$countIteration++]; }, $expect); $expect = preg_replace_callback('/\\{\\{\\$key\\}\\}/is', function ($match) use($keyVars, &$keyIteration) { return $keyVars[$keyIteration++]; }, $expect); $this->assertSame($expect, $compiled); }
/** * {@inheritdoc} */ public static function fromString($string, Translations $translations, array $options = []) { $cachePath = empty($options['cachePath']) ? sys_get_temp_dir() : $options['cachePath']; $bladeCompiler = new BladeCompiler(new Filesystem(), $cachePath); $string = $bladeCompiler->compileString($string); PhpCode::fromString($string, $translations, $options); }
protected function registerSearchForm(Compiler $blade, $name) { $blade->extend(function ($view, $compiler) use($name) { $pattern = $compiler->createMatcher($name); $code = <<<'EOD' $1<?php if (isset($searchForm)) { echo $searchForm; } else{ $passed = array$2; if (count($passed) == 2) { $m = $passed[0]; $res = $passed[1]; } if (count($passed) == 1) { $m = $passed[0]; $res = isset($resource) ? $resource : null; } if (count($passed) == 0) { $m = isset($model) ? $model : null; $res = isset($resource) ? $resource : null; } echo Resource::searchForm($m, $res); } ?> EOD; return preg_replace($pattern, $code, $view); }); }
public function extendTo(BladeCompiler $blade) { $blade->directive('wploop', $this->start()); $blade->directive('wpempty', $this->ifempty()); $blade->directive('endwploop', $this->end()); $blade->directive('wpthe', $this->the()); }
/** * @wpend * * @param BladeCompiler $compiler The blade compiler to extend */ private function registerEndLoop($compiler) { $compiler->extend(function ($view, $comp) { $pattern = $comp->createPlainMatcher('endwploop'); $replacement = '$1<?php endif; wp_reset_postdata(); ?>'; return preg_replace($pattern, $replacement, $view); }); }
public function register() { $this->app->instance(__CLASS__, $this); $this->app->alias(__CLASS__, $this->containerName); $this->blade->directive('include_layout', function ($expr) { return "<?php echo app('{$this->containerName}')->renderLayout{$expr} ?>"; }); }
/** * Register Blade syntax for a specific component. * * @param string $method * @param string $namespace * @return void */ protected function registerTag($method, $namespace = '') { $this->blade->extend(function ($view, $compiler) use($method, $namespace) { $pattern = $compiler->createMatcher('component_' . $method); $replace = '$1<?php echo ' . $namespace . $method . '$2; ?>'; return preg_replace($pattern, $replace, $view); }); }
/** * Register blade syntax for a specific widget. * * @param string $name */ protected function registerBlade($name) { $this->blade->extend(function ($view, $compiler) use($name) { $pattern = $this->createMatcher($name); $replace = '$1<?php echo Widget::' . $name . '$2; ?>'; return preg_replace($pattern, $replace, $view); }); }
/** * @shortcode * * @param BladeCompiler $compiler The blade compiler to extend */ private function registerDoShortcode($compiler) { $compiler->extend(function ($view, $comp) { $pattern = $comp->createMatcher('shortcode'); $replacement = '$1<?php do_shortcode($2); ?> '; return preg_replace($pattern, $replacement, $view); }); }
/** * Get the evaluated contents of the file. */ public function render() { $viewContent = $this->buildBladeViewContent(); if ($this->isExpired()) { $this->filesystem->put($this->cached, $this->bladeCompiler->compileString($viewContent)); } $data = $this->getViewData(); return $this->engine->get($this->cached, $data); }
/** * Parses and compiles strings by using Blade Template System. * * @param string $str * @param array $data * @return string */ public static function compileBlade($str, $data = []) { $empty_filesystem_instance = new Filesystem(); $blade = new BladeCompiler($empty_filesystem_instance, 'datatables'); $parsed_string = $blade->compileString($str); ob_start() && extract($data, EXTR_SKIP); eval('?>' . $parsed_string); $str = ob_get_contents(); ob_end_clean(); return $str; }
public function testCompileViews() { $compiler = new BladeCompiler(new Filesystem(), $this->config['storage_path']); $this->generator->compileViews($this->config['paths'], $this->config['storage_path']); foreach ($this->config['paths'] as $path) { $files = glob(realpath($path) . '/{,**/}*.php', GLOB_BRACE); foreach ($files as $file) { $contents = $this->generator->parseToken($this->files->get($file)); $this->assertEquals($compiler->compileString($contents), $this->files->get($this->config['storage_path'] . "/" . md5($file) . ".php")); } } }
/** * render * * @param $string * @param array $vars * @return string */ public function render($string, array $vars = array()) { $this->files->put($this->tmpFilePath, $this->compiler->compileString($string)); if (is_array($vars) && !empty($vars)) { extract($vars); } ob_start(); include $this->tmpFilePath; $var = ob_get_contents(); ob_end_clean(); $this->files->delete($this->tmpFilePath); return $var; }
private function getContent() { $compiler = new BladeCompiler(new Filesystem(), "."); $template = $this->getTemplate(); $compiled = $compiler->compileString(' ?>' . $template); $up = trim($this->up, "\n"); $down = trim($this->down, "\n"); ob_start(); eval($compiled); $content = ob_get_contents(); ob_end_clean(); return $content; }
/** * Extend blade by continue and break directives. * * @param BladeCompiler $blade * * @return BladeCompiler */ private function addLoopControlDirectives(BladeCompiler $blade) { $blade->extend(function ($value) { $pattern = '/(?<!\\w)(\\s*)@continue\\s*\\(([^)]*)\\)/'; $replacement = '$1<?php if ($2) { continue; } ?>'; return preg_replace($pattern, $replacement, $value); }); $blade->extend(function ($value) { $pattern = '/(?<!\\w)(\\s*)@break\\s*\\(([^)]*)\\)/'; $replacement = '$1<?php if ($2) { break; } ?>'; return preg_replace($pattern, $replacement, $value); }); return $blade; }
/** * Add basic syntax highlighting. * * This just adds markup that is picked up by a javascript highlighting library. * * @param BladeCompiler $compiler */ protected function allowHighlightTag(BladeCompiler $compiler) { $compiler->directive('highlight', function ($expression) { if (is_null($expression)) { $expression = "('php')"; } return "<?php \$__env->startSection{$expression}; ?>"; }); $compiler->directive('endhighlight', function () { return <<<'HTML' <?php $last = $__env->stopSection(); echo '<pre><code class="language-', $last, '">', trim($__env->yieldContent($last)), '</code></pre>'; ?> HTML; }); }
/** * Register. * * @param \Illuminate\View\Compilers\BladeCompiler $compiler * * @return void */ public static function register(BladeCompiler $compiler) { $compiler->directive('money', function ($expression) { if (Str::startsWith($expression, '(')) { $expression = substr($expression, 1, -1); } return "<?php echo money({$expression}); ?>"; }); $compiler->directive('currency', function ($expression) { if (Str::startsWith($expression, '(')) { $expression = substr($expression, 1, -1); } return "<?php echo currency({$expression}); ?>"; }); }
/** * Adds all class methods prefixed with assert* as blade view directives * and assigns the $view and $blade class properties * * @param string $viewDirectoryPath The path to the directory containing test views */ public function addBladeViewTesting($viewDirectoryPath = null) { /** @var \Illuminate\View\Factory $view */ $view = $this->app['view']; // Add instance of current test class, this enable to call assert functions in views $view->share('testClassInstance', $this); // Lets make those assertions callable by fancy blade directives, nom nom nom $this->blade = $view->getEngineResolver()->resolve('blade')->getCompiler(); $this->blade->extend(function ($value) { return preg_replace('/@assert(\\w*)\\((.*)\\)/', "<?php \$testClassInstance->assert\$1(\$2); ?>", $value); }); if ($viewDirectoryPath !== null) { $view->addLocation($viewDirectoryPath); } }
/** * render * * @param $string * @param array $vars * @return string */ public function render($string, array $vars = array()) { $fileName = uniqid(time(), false); $this->generateDirectoryStructure(storage_path(), ['sebwite/stubs']); $path = storage_path("sebwite/stubs/{$fileName}"); $this->files->put($path, $this->compiler->compileString($string)); if (is_array($vars) && !empty($vars)) { extract($vars); } ob_start(); include $path; $var = ob_get_contents(); ob_end_clean(); $this->files->delete($path); return $var; }
/** * Turn $this->extend() into @extend(). */ protected function extend() { $this->compiler->extend(function ($view, $compiler) { $pattern = $compiler->createMatcher('extend'); return preg_replace($pattern, '$1<?php echo $_view->extend$2; ?>', $view); }); }
public function render($str, array $vars = []) { $__tmp_stub_file = Str::random() . uniqid(time(), false); !$this->fs->exists($this->cachePath) && $this->fs->makeDirectory($this->cachePath, 0755, true); $__tmp_stub_path = Path::join($this->cachePath, $__tmp_stub_file); $this->fs->put($__tmp_stub_path, $this->compiler->compileString($str)); $__env = $this->getViewFactory(); if (is_array($vars) && 0 !== count($vars)) { extract($vars); } ob_start(); include $__tmp_stub_path; $var = ob_get_contents(); ob_end_clean(); $this->fs->delete($__tmp_stub_path); return $var; }
/** * Compile views. * * @param array $paths * @param string $storagePath * @return $this */ public function compileViews(array $paths, $storagePath) { $this->makeDestination($storagePath); $compiler = new BladeCompiler($this->files, $storagePath); foreach ($paths as $path) { $files = $this->files->glob(realpath($path) . '/{,**/}*.php', GLOB_BRACE); foreach ($files as $file) { if (!Str::endsWith(strtolower($file), '.blade.php')) { continue; } $compiler->setPath($file); $contents = $this->parseToken($this->files->get($file)); $contents = $compiler->compileString($contents); $compiledPath = $compiler->getCompiledPath($compiler->getPath()); $this->files->put($compiledPath . '.php', $contents); } } return $this; }
/** * extend * * @param BladeCompiler $blade * @param string $name * @param callable $closure * * @return void */ public static function extend(BladeCompiler $blade, $name, $closure) { // For 5.0 after if (is_callable(array($blade, 'directive'))) { $blade->directive($name, $closure); return; } // For 4.x before $blade->extend(function ($view, BladeCompiler $compiler) use($name, $closure) { $pattern = $compiler->createMatcher($name); return preg_replace_callback($pattern, function ($matches) use($closure) { if (empty($matches[2])) { return $matches[0]; } return $matches[1] . $closure($matches[2]); }, $view); }); return; }
/** * Parses and compiles strings by using Blade Template System. * * @param string $str * @param array $data * @return string * @throws \Exception */ public static function compileBlade($str, $data = []) { if (view()->exists($str)) { return view($str, $data)->render(); } $empty_filesystem_instance = new Filesystem(); $blade = new BladeCompiler($empty_filesystem_instance, 'datatables'); $parsed_string = $blade->compileString($str); ob_start() && extract($data, EXTR_SKIP); try { eval('?>' . $parsed_string); } catch (\Exception $e) { ob_end_clean(); throw $e; } $str = ob_get_contents(); ob_end_clean(); return $str; }
public function __construct($filesystem, $cache_path, $config, $app) { // Get Current Blade Instance $blade = $app->make('blade.compiler'); parent::__construct($filesystem, $cache_path); $this->rawTags = $blade->getRawTags(); $this->contentTags = $blade->getContentTags(); $this->escapedTags = $blade->getEscapedContentTags(); $this->extensions = $blade->getExtensions(); $this->config = $config; }
public function fire() { $app = app(); $cache = $app['path.storage'] . '/views'; $compiler = new BladeCompiler($app['files'], $cache); // delete compiled $files = new \GlobIterator(TEMP_PATH . '/views/*'); foreach ($files as $file) { if ($file->getExtension() !== '.gitignore') { unlink($file); } } // generate new ones $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(VIEWS_PATH), \RecursiveIteratorIterator::SELF_FIRST); foreach ($files as $file) { if ($file->getExtension() === 'blade') { $compiler->compile($file->getPathname()); } } }