From 1e223e2f6d0aeb093f985a9eb7c4162909d97151 Mon Sep 17 00:00:00 2001 From: Abdulrahman Date: Sat, 14 Sep 2019 13:35:19 +0300 Subject: [PATCH] fixes & updated to support laravel 6.0 --- composer.json | 11 ++++++----- src/Analyze.php | 24 +++++++++++++++--------- src/Commands/BladeAudit.php | 35 ++++++++++++++++++++++++----------- 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/composer.json b/composer.json index 3dc7c4c..c045f3d 100644 --- a/composer.json +++ b/composer.json @@ -12,13 +12,14 @@ } ], "require": { - "illuminate/console": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0", - "illuminate/view": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0", - "illuminate/filesystem": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0", - "illuminate/support": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|~6.0" + "php": "~7.1", + "illuminate/console": "~5.5|^6.0", + "illuminate/view": "~5.5|^6.0", + "illuminate/filesystem": "~5.5|^6.0", + "illuminate/support": "~5.5|^6.0" }, "require-dev": { - "phpunit/phpunit": "^6.1|^8.0" + "phpunit/phpunit": "~6.5" }, "autoload": { "psr-4": { diff --git a/src/Analyze.php b/src/Analyze.php index ef6b48e..e4a3f26 100644 --- a/src/Analyze.php +++ b/src/Analyze.php @@ -5,8 +5,6 @@ use Illuminate\Support\Str; use Illuminate\Support\Collection; use Illuminate\Container\Container; -use Illuminate\Contracts\View\View; -use Illuminate\Contracts\View\Factory; use Illuminate\View\Compilers\BladeCompiler; @@ -15,14 +13,13 @@ class Analyze /** @var \Illuminate\View\Compilers\BladeCompiler */ protected $compiler; - /** @var \Illuminate\View\FileViewFinder */ - protected $finder; - protected $code; /** @var \Illuminate\Support\Collection */ protected $directives; + protected $viewsPaths; + protected $viewInfo; protected $nestedLevels; protected $directivesInfo; @@ -33,7 +30,9 @@ public function __construct() $app = Container::getInstance(); $this->compiler = $app->make(BladeCompiler::class); - $this->finder = $app->make(Factory::class)->getFinder(); + $this->viewsPaths = array_map(function($path) { + return realpath($path) ?: $path; + }, $app->config['view']['paths']); } /** @@ -46,9 +45,16 @@ public static function view($viewName) public function analyze($viewName) { - try { - $viewPath = $this->finder->find($viewName); - } catch (\InvalidArgumentException $e) { + $viewPath = ''; + $viewName = str_replace('.', '/', $viewName).'.blade.php'; + + foreach ((array) $this->viewsPaths as $path) { + if (file_exists($path.'/'.$viewName)) { + $viewPath = $path.'/'.$viewName; + } + } + + if(empty($viewPath)) { return false; } diff --git a/src/Commands/BladeAudit.php b/src/Commands/BladeAudit.php index 7ebd376..499b1bb 100644 --- a/src/Commands/BladeAudit.php +++ b/src/Commands/BladeAudit.php @@ -6,8 +6,9 @@ use Illuminate\Support\Str; use Awssat\BladeAudit\Analyze; use Illuminate\Console\Command; -use Illuminate\Support\Collection; +use Illuminate\Contracts\Container\Container; use Illuminate\Filesystem\Filesystem; +use Illuminate\Support\Collection; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\TableCell; @@ -17,16 +18,21 @@ class BladeAudit extends Command protected $description = 'Extensive information of a blade view'; + protected $viewsPaths; /** @var \Illuminate\Filesystem\Filesystem */ protected $filesystem; protected $allViewsResult; - public function __construct(Filesystem $filesystem) + public function __construct(Container $app) { parent::__construct(); - $this->filesystem = $filesystem; + $this->viewsPaths = array_map(function($path) { + return realpath($path) ?: $path; + }, $app->config['view']['paths']); + + $this->filesystem = $app->make(Filesystem::class); } /** @@ -118,7 +124,7 @@ protected function outputOneView($result) (new Table($this->output)) ->setHeaders([ - [new TableCell('Audit Notes', ['colspan' => 2])], + [new TableCell('Notes', ['colspan' => 2])], ]) ->setStyle('compact') ->setRows( @@ -202,7 +208,7 @@ protected function outputAllViews() (new Table($this->output)) ->setHeaders([ - [new TableCell('Audit Notes: ('.$view.')', ['colspan' => 2])], + [new TableCell('Notes: ('.$view.')', ['colspan' => 2])], ]) ->setStyle('compact') ->setRows( @@ -221,14 +227,21 @@ protected function outputAllViews() */ protected function getAllViews() { - return Collection::wrap( - $this->filesystem->allFiles(resource_path('views')) - )->map(function ($file) { - return str_replace( + return Collection::wrap($this->viewsPaths) + ->map(function($path) { + return $this->filesystem->allFiles($path) ?? []; + }) + ->flatten() + ->map(function ($file) { + if (Str::endsWith($file, '.blade.php')) { + return str_replace( [DIRECTORY_SEPARATOR, '.blade.php'], ['.', ''], Str::after($file, resource_path('views') . DIRECTORY_SEPARATOR) - ); - }); + ); + } + return null; + }) + ->filter(); } }