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

added 5.1 missing create*Matcher functions #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 129 additions & 98 deletions src/Crhayes/BladePartials/ViewServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,103 +2,134 @@

class ViewServiceProvider extends \Illuminate\View\ViewServiceProvider {

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
parent::register();

$this->registerBladeExtensions();
}

/**
* Register the view environment.
*
* @return void
*/
public function registerFactory()
{
$this->app->bindShared('view', function($app)
{
// Next we need to grab the engine resolver instance that will be used by the
// environment. The resolver will be used by an environment to get each of
// the various engine implementations such as plain PHP or Blade engine.
$resolver = $app['view.engine.resolver'];

$finder = $app['view.finder'];

$env = new Factory($resolver, $finder, $app['events']);

// We will also set the container instance on this view environment since the
// view composers may be classes registered in the container, which allows
// for great testable, flexible composers for the application developer.
$env->setContainer($app);

$env->share('app', $app);

return $env;
});
}

/**
* Register custom blade extensions.
*
* @return void
*/
protected function registerBladeExtensions()
{
$blade = $this->app['view']->getEngineResolver()->resolve('blade')->getCompiler();

$blade->extend(function($value, $compiler)
{
$pattern = $compiler->createOpenMatcher('partial');

return preg_replace(
$pattern,
'$1<?php $__env->renderPartial$2, get_defined_vars(), function($file, $vars) use ($__env) {
$vars = array_except($vars, array(\'__data\', \'__path\'));
extract($vars); ?>',
$value
);
});

$blade->extend(function($value, $compiler)
{
$pattern = $compiler->createPlainMatcher('endpartial');

return preg_replace($pattern, '$1<?php echo $__env->make($file, $vars)->render(); }); ?>$2', $value);
});

$blade->extend(function($value, $compiler)
{
$pattern = $compiler->createMatcher('block');

return preg_replace($pattern, '$1<?php $__env->startBlock$2; ?>', $value);
});

$blade->extend(function($value, $compiler)
{
$pattern = $compiler->createPlainMatcher('endblock');

return preg_replace($pattern, '$1<?php $__env->stopBlock(); ?>$2', $value);
});

$blade->extend(function($value, $compiler)
{
$pattern = $compiler->createMatcher('render');

return preg_replace($pattern, '$1<?php echo $__env->renderBlock$2; ?>', $value);
});
}
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
parent::register();

$this->registerBladeExtensions();
}

/**
* Register the view environment.
*
* @return void
*/
public function registerFactory()
{
$this->app->bindShared('view', function($app)
{
// Next we need to grab the engine resolver instance that will be used by the
// environment. The resolver will be used by an environment to get each of
// the various engine implementations such as plain PHP or Blade engine.
$resolver = $app['view.engine.resolver'];

$finder = $app['view.finder'];

$env = new Factory($resolver, $finder, $app['events']);

// We will also set the container instance on this view environment since the
// view composers may be classes registered in the container, which allows
// for great testable, flexible composers for the application developer.
$env->setContainer($app);

$env->share('app', $app);

return $env;
});
}

/**
* Register custom blade extensions.
*
* @return void
*/
protected function registerBladeExtensions()
{
$blade = $this->app['view']->getEngineResolver()->resolve('blade')->getCompiler();

$blade->extend(function($value, $compiler)
{
$pattern = $this->createOpenMatcher('partial');

return preg_replace(
$pattern,
'$1<?php $__env->renderPartial$2, get_defined_vars(), function($file, $vars) use ($__env) {
$vars = array_except($vars, array(\'__data\', \'__path\'));
extract($vars); ?>',
$value
);
});

$blade->extend(function($value, $compiler)
{
$pattern = $this->createPlainMatcher('endpartial');

return preg_replace($pattern, '$1<?php echo $__env->make($file, $vars)->render(); }); ?>$2', $value);
});

$blade->extend(function($value, $compiler)
{
$pattern = $this->createMatcher('block');

return preg_replace($pattern, '$1<?php $__env->startBlock$2; ?>', $value);
});

$blade->extend(function($value, $compiler)
{
$pattern = $this->createPlainMatcher('endblock');

return preg_replace($pattern, '$1<?php $__env->stopBlock(); ?>$2', $value);
});

$blade->extend(function($value, $compiler)
{
$pattern = $this->createMatcher('render');

return preg_replace($pattern, '$1<?php echo $__env->renderBlock$2; ?>', $value);
});
}

/* Get the regular expression for a generic Blade function.
*
* @param string $function
* @return string
*/
public function createMatcher($function)
{
return '/(?<!\w)(\s*)@'.$function.'(\s*\(.*\))/';
}

/**
* Get the regular expression for a generic Blade function.
*
* @param string $function
* @return string
*/
public function createOpenMatcher($function)
{
return '/(?<!\w)(\s*)@'.$function.'(\s*\(.*)\)/';
}
/**
* Create a plain Blade matcher.
*
* @param string $function
* @return string
*/
public function createPlainMatcher($function)
{
return '/(?<!\w)(\s*)@'.$function.'(\s*)/';
}

}