Skip to content

Commit

Permalink
refactor: add type-hints, remove un-used codes
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhquochan committed Feb 14, 2023
1 parent 768b287 commit bd327a5
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 209 deletions.
2 changes: 2 additions & 0 deletions config/twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
|--------------------------------------------------------------------------
|
| Add supported view file extensions. E.g: *.twig, *.html.twig, *.css.twig...
|
*/

'file_extensions' => [
Expand All @@ -19,6 +20,7 @@
|--------------------------------------------------------------------------
|
| Enabled twig extensions.
|
*/

'extensions' => [
Expand Down
36 changes: 12 additions & 24 deletions src/Console/TwigViewClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,33 @@
namespace DinhQuocHan\Twig\Console;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use RuntimeException;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand('view:clear', 'Clear all compiled view files')]
class TwigViewClearCommand extends Command
{
/** @var string */
protected $name = 'view:clear';

/** @var string */
protected $description = 'Clear all compiled view files';

/** @var \Illuminate\Filesystem\Filesystem */
protected $files;

public function __construct(Filesystem $files)
{
parent::__construct();

$this->files = $files;
}

public function handle()
public function handle(): int
{
$files = $this->laravel['files'];
$path = $this->laravel['config']['view.compiled'];

if (! $path) {
throw new RuntimeException('View path not found.');
}

foreach ($this->files->glob("{$path}/*") as $view) {
if ($this->files->isDirectory($view)) {
$this->files->deleteDirectory($view);
foreach ($files->glob("{$path}/*") as $view) {
if ($files->isDirectory($view)) {
$files->deleteDirectory($view);
continue;
}
if ($this->files->isFile($view)) {
$this->files->delete($view);
if ($files->isFile($view)) {
$files->delete($view);
}
}

$this->info('Compiled twig views cleared!');
$this->components->info('Compiled blade & twig views cleared!');

return self::SUCCESS;
}
}
8 changes: 1 addition & 7 deletions src/Extensions/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@

use Illuminate\Support\Str;
use Twig\Extension\AbstractExtension;
use Twig\Extension\ExtensionInterface;
use Twig\TwigFunction;

class Arr extends AbstractExtension implements ExtensionInterface
class Arr extends AbstractExtension
{
/**
* Get functions.
*
* @return \Twig\TwigFunction[]
*/
public function getFunctions()
{
return [
Expand Down
23 changes: 3 additions & 20 deletions src/Extensions/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,15 @@

use Illuminate\Contracts\Auth\Factory;
use Twig\Extension\AbstractExtension;
use Twig\Extension\ExtensionInterface;
use Twig\TwigFunction;

class Auth extends AbstractExtension implements ExtensionInterface
class Auth extends AbstractExtension
{
/**
* @var \Illuminate\Contracts\Auth\Factory
*/
protected $auth;

/**
* Create a new auth extension.
*
* @param \Illuminate\Contracts\Auth\Factory
*/
public function __construct(Factory $auth)
public function __construct(protected Factory $auth)
{
$this->auth = $auth;
}

/**
* Get functions.
*
* @return \Twig\TwigFunction[]
*/
public function getFunctions()
public function getFunctions(): array
{
return [
new TwigFunction('auth', [$this->auth, 'guard']),
Expand Down
21 changes: 2 additions & 19 deletions src/Extensions/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,14 @@

use Illuminate\Contracts\Config\Repository;
use Twig\Extension\AbstractExtension;
use Twig\Extension\ExtensionInterface;
use Twig\TwigFunction;

class Config extends AbstractExtension implements ExtensionInterface
class Config extends AbstractExtension
{
/**
* @var \Illuminate\Contracts\Config\Repository
*/
protected $config;

/**
* Create a new config extension.
*
* @param \Illuminate\Contracts\Config\Repository
*/
public function __construct(Repository $config)
public function __construct(protected Repository $config)
{
$this->config = $config;
}

/**
* Get functions.
*
* @return \Twig\TwigFunction[]
*/
public function getFunctions()
{
return [
Expand Down
10 changes: 2 additions & 8 deletions src/Extensions/Dump.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@
namespace DinhQuocHan\Twig\Extensions;

use Twig\Extension\AbstractExtension;
use Twig\Extension\ExtensionInterface;
use Twig\TwigFunction;

class Dump extends AbstractExtension implements ExtensionInterface
class Dump extends AbstractExtension
{
/**
* Get functions.
*
* @return \Twig\TwigFunction[]
*/
public function getFunctions()
public function getFunctions(): array
{
return [
new TwigFunction('dump', 'dump', ['is_safe' => ['html']]),
Expand Down
23 changes: 3 additions & 20 deletions src/Extensions/Gate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,15 @@

use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Twig\Extension\AbstractExtension;
use Twig\Extension\ExtensionInterface;
use Twig\TwigFunction;

class Gate extends AbstractExtension implements ExtensionInterface
class Gate extends AbstractExtension
{
/**
* @var \Illuminate\Contracts\Auth\Access\Gate
*/
protected $gate;

/**
* Create a new gate extension.
*
* @param \Illuminate\Contracts\Auth\Access\Gate
*/
public function __construct(GateContract $gate)
public function __construct(protected GateContract $gate)
{
$this->gate = $gate;
}

/**
* Get functions.
*
* @return \Twig\TwigFunction[]
*/
public function getFunctions()
public function getFunctions(): array
{
return [
new TwigFunction('can', [$this->gate, 'check']),
Expand Down
8 changes: 1 addition & 7 deletions src/Extensions/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@
namespace DinhQuocHan\Twig\Extensions;

use Twig\Extension\AbstractExtension;
use Twig\Extension\ExtensionInterface;
use Twig\TwigFilter;
use Twig\TwigFunction;

class Path extends AbstractExtension implements ExtensionInterface
class Path extends AbstractExtension
{
/**
* Get functions.
*
* @return \Twig\TwigFilter[]
*/
public function getFilters()
{
return [
Expand Down
23 changes: 3 additions & 20 deletions src/Extensions/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,15 @@

use Illuminate\Http\Request as BaseRequest;
use Twig\Extension\AbstractExtension;
use Twig\Extension\ExtensionInterface;
use Twig\TwigFunction;

class Request extends AbstractExtension implements ExtensionInterface
class Request extends AbstractExtension
{
/**
* @var \Illuminate\Http\Request
*/
protected $request;

/**
* Create a new request extension.
*
* @param \Illuminate\Http\Request
*/
public function __construct(BaseRequest $request)
public function __construct(protected BaseRequest $request)
{
$this->request = $request;
}

/**
* Get functions.
*
* @return \Twig\TwigFunction[]
*/
public function getFunctions()
public function getFunctions(): array
{
return [
new TwigFunction('request', [$this->request, 'input']),
Expand Down
23 changes: 3 additions & 20 deletions src/Extensions/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,15 @@

use Illuminate\Contracts\Session\Session as SessionContract;
use Twig\Extension\AbstractExtension;
use Twig\Extension\ExtensionInterface;
use Twig\TwigFunction;

class Session extends AbstractExtension implements ExtensionInterface
class Session extends AbstractExtension
{
/**
* @var \Illuminate\Contracts\Session\Session
*/
protected $session;

/**
* Create a new session extension.
*
* @param \Illuminate\Contracts\Session\Session
*/
public function __construct(SessionContract $session)
public function __construct(protected SessionContract $session)
{
$this->session = $session;
}

/**
* Get functions.
*
* @return \Twig\TwigFunction[]
*/
public function getFunctions()
public function getFunctions(): array
{
return [
new TwigFunction('session', [$this->session, 'get']),
Expand Down
17 changes: 3 additions & 14 deletions src/Extensions/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@
namespace DinhQuocHan\Twig\Extensions;

use Twig\Extension\AbstractExtension;
use Twig\Extension\ExtensionInterface;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Illuminate\Support\Str as IlluminateStr;

class Str extends AbstractExtension implements ExtensionInterface
class Str extends AbstractExtension
{
/**
* Get functions.
*
* @return \Twig\TwigFilter[]
*/
public function getFilters()
public function getFilters(): array
{
return [
new TwigFilter('__', '__'),
Expand All @@ -31,12 +25,7 @@ public function getFilters()
];
}

/**
* Get functions.
*
* @return \Twig\TwigFunction[]
*/
public function getFunctions()
public function getFunctions(): array
{
return [
new TwigFunction('__', '__'),
Expand Down
17 changes: 3 additions & 14 deletions src/Extensions/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@
namespace DinhQuocHan\Twig\Extensions;

use Twig\Extension\AbstractExtension;
use Twig\Extension\ExtensionInterface;
use Twig\TwigFilter;
use Twig\TwigFunction;

class Translator extends AbstractExtension implements ExtensionInterface
class Translator extends AbstractExtension
{
/**
* Get functions.
*
* @return \Twig\TwigFilter[]
*/
public function getFilters()
public function getFilters(): array
{
return [
new TwigFilter('__', '__', ['pre_escape' => 'html', 'is_safe' => ['html']]),
Expand All @@ -23,12 +17,7 @@ public function getFilters()
];
}

/**
* Get functions.
*
* @return \Twig\TwigFunction[]
*/
public function getFunctions()
public function getFunctions(): array
{
return [
new TwigFunction('__', '__', ['pre_escape' => 'html', 'is_safe' => ['html']]),
Expand Down
Loading

0 comments on commit bd327a5

Please sign in to comment.