-
Notifications
You must be signed in to change notification settings - Fork 0
02. Command namespace
Bladed package provides utilities to extend blade syntax with so called
"command namespaces" or command providers. In terms of php code
command providers are classes that are inherited from
LaravelCommode\Bladed\Commands\ABladedCommand
and registered in
bladed manager afterwards.
All command providers have access to current view environment and
laravel's IoC-container. Command providers are registered in laravel's IoC
as singletons, so they they keep their states from one view to another. If your
command provider needs dependency injection you can feel free to override
__construct()
method, but don't forget to call parent's one:
<?php
namespace MyApp\BladeCommands;
use Illuminate\Foundation\Application;
use LaravelCommode\Bladed\Commands\ABladedCommand;
use MyApp\System\Security\Interfaces\ISecurityUser;
class Security extends ABladedCommand
{
/**
* @var ISecurityUser
*/
private $user;
public function __construct(Application $app, ISecurityUser $user = null)
{
parent::__construct($app);
$this->user = $user;
}
public function hasRole($role)
{
return /** ... **/;
}
}
First we will create our command provider class. Let's say we need some utilities to output php values into javascript variables to use it at the client side. I know that it's not the best practice, but it's a good example of command provider usage.
Let's create our command provider:
<?php
namespace MyApp\BladedCommands;
use LaravelCommode\Bladed\Commands\ABladedCommand;
class JS extends ABladedCommand
{
public function wrapScript($content)
{
return "<script type='text/javascript'>{$content}</script>";
}
protected function makeVariable($varName, $value, $enforceGlobal = false)
{
return ($enforceGlobal ? "window['{$varName}']": "var {$varName}")
. " = {$this->castType($value)};";
}
protected function castType($value)
{
switch(gettype($value))
{
case 'string': return '"' . addslashes($value) . '"';
case 'boolean': return $value ? 'true' : 'false';
case 'NULL': return 'null';
case 'array': case 'object': return json_encode($value);
}
return $value;
}
public function makeVar($varName, $value, $wrapScript = true, $globalize = false)
{
$contents = $this->makeVariable($varName, $value, $globalize);
return $wrapScript ? $this->wrapScript($contents) : $contents;
}
public function makeVars($varValues, $wrapScript = true, $globalize = false)
{
$contents = "";
foreach($varValues as $varName => $value)
{
$contents .= $this->makeVariable($varName, $value, $globalize);
}
return $wrapScript ? $this->wrapScript($contents) : $contents;
}
}
Now, let's go and create a service provider that would be responsible for
registering of out command providers. I'm going to use
LaravelCommode\Common\GhostService\GhostService
as base service provider,
since I need to require LaravelCommode\Bladed\BladedServiceProvider
as a
dependency and I don't want to create a mass in my app.php
config file:
<?php
namespace MyApp\ServiceProviders;
use LaravelCommode\Common\GhostService\GhostService;
use LaravelCommode\Bladed\Interfaces\IBladedManager;
class BladedExtensionsServiceProvider extends GhostService
{
protected function uses()
{
return [
'LaravelCommode\Bladed\BladedServiceProvider'
];
}
protected function launching()
{
$this->with('commode.bladed', function (IBladedManager $manager) {
$manager->registerCommandNamespace('js', 'MyApp\BladedCommands\JS');
});
}
protected function regestering() { }
}
And finally we are free to use it in our *.blade.php
views:
@js.makeVars(['uniqid' => uniqid()]) @>
@js.makeVar(['errors' => $errors]) @>