Skip to content

Commit

Permalink
Fixed compatibility with PHP7 renaming class using reserved name (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
billmn authored and barryvdh committed May 24, 2016
1 parent f52184b commit 3749926
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/Extension/Laravel/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Twig_Extension;
use Twig_SimpleFunction;
use Illuminate\Html\FormBuilder;
use Illuminate\Support\Str;
use Illuminate\Support\Str as IlluminateStr;

/**
* Access Laravels form builder in your Twig templates.
Expand Down Expand Up @@ -54,7 +54,7 @@ public function getFunctions()
'form_*',
function ($name) {
$arguments = array_slice(func_get_args(), 1);
$name = Str::camel($name);
$name = IlluminateStr::camel($name);

return call_user_func_array([$this->form, $name], $arguments);
},
Expand Down
4 changes: 2 additions & 2 deletions src/Extension/Laravel/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Twig_Extension;
use Twig_SimpleFunction;
use Illuminate\Html\HtmlBuilder;
use Illuminate\Support\Str;
use Illuminate\Support\Str as IlluminateStr;

/**
* Access Laravels html builder in your Twig templates.
Expand Down Expand Up @@ -58,7 +58,7 @@ public function getFunctions()
'html_*',
function ($name) {
$arguments = array_slice(func_get_args(), 1);
$name = Str::camel($name);
$name = IlluminateStr::camel($name);

return call_user_func_array([$this->html, $name], $arguments);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
use Twig_Extension;
use Twig_SimpleFunction;
use Twig_SimpleFilter;
use Illuminate\Support\Str;
use Illuminate\Support\Str as IlluminateStr;

/**
* Access Laravels string class in your Twig templates.
*/
class String extends Twig_Extension
class Str extends Twig_Extension
{
/**
* @var string|object
Expand Down Expand Up @@ -66,7 +66,7 @@ public function getFunctions()
'str_*',
function ($name) {
$arguments = array_slice(func_get_args(), 1);
$name = Str::camel($name);
$name = IlluminateStr::camel($name);

return call_user_func_array([$this->callback, $name], $arguments);
}
Expand All @@ -87,7 +87,7 @@ public function getFilters()
'str_*',
function ($name) {
$arguments = array_slice(func_get_args(), 1);
$name = Str::camel($name);
$name = IlluminateStr::camel($name);

return call_user_func_array([$this->callback, $name], $arguments);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Extension/Laravel/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Twig_Extension;
use Twig_SimpleFunction;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\Str;
use Illuminate\Support\Str as IlluminateStr;

/**
* Access Laravels url class in your Twig templates.
Expand Down Expand Up @@ -60,7 +60,7 @@ public function getFunctions()
'url_*',
function ($name) {
$arguments = array_slice(func_get_args(), 1);
$name = Str::camel($name);
$name = IlluminateStr::camel($name);

return call_user_func_array([$this->url, $name], $arguments);
}
Expand Down
25 changes: 24 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function boot()
$this->registerOptions();
$this->registerLoaders();
$this->registerEngine();
$this->registerAliases();

$this->app['view']->addExtension(
$this->app['twig.extension'],
Expand All @@ -57,6 +58,16 @@ function () {
);
}

/**
* Check if we are running on PHP 7.
*
* @return bool
*/
protected function isRunningOnPhp7()
{
return version_compare(PHP_VERSION, '7.0-dev', '>=');
}

/**
* Register console command bindings.
*
Expand Down Expand Up @@ -210,7 +221,7 @@ function () {
},
true
);

$this->app->alias('twig', 'TwigBridge\Bridge');

$this->app->bindIf('twig.compiler', function () {
Expand All @@ -226,6 +237,18 @@ function () {
});
}

/**
* Register aliases for classes that had to be renamed because of reserved names in PHP7.
*
* @return void
*/
protected function registerAliases()
{
if (!$this->isRunningOnPhp7()) {
class_alias('TwigBridge\Extension\Laravel\Str', 'TwigBridge\Extension\Laravel\String');
}
}

/**
* Get the services provided by the provider.
*
Expand Down
2 changes: 1 addition & 1 deletion src/config/extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
'TwigBridge\Extension\Laravel\Html',
'TwigBridge\Extension\Laravel\Input',
'TwigBridge\Extension\Laravel\Session',
'TwigBridge\Extension\Laravel\String',
'TwigBridge\Extension\Laravel\Str',
'TwigBridge\Extension\Laravel\Translator',
'TwigBridge\Extension\Laravel\Url',

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use TwigBridge\Tests\Base;
use Mockery as m;
use TwigBridge\Extension\Laravel\String;
use TwigBridge\Extension\Laravel\Str;

class StringTest extends Base
class StrTest extends Base
{
protected $customFilters = [
'camel_case',
Expand Down Expand Up @@ -92,6 +92,6 @@ public function testWildcardFilters()

protected function getString()
{
return new String;
return new Str;
}
}

0 comments on commit 3749926

Please sign in to comment.