From 4839134477a279aa89fdb3bd4acf5d96606ca7b5 Mon Sep 17 00:00:00 2001 From: Stefan Ninic Date: Sat, 23 Jun 2018 15:51:20 +0000 Subject: [PATCH] Apply fixes from StyleCI [ci skip] [skip ci] --- routes.php | 93 +++++++++---------- src/Http/Controllers/DeployController.php | 32 +++---- .../Front/Ajax/ClientScriptController.php | 39 +++----- .../Front/Ajax/ClientsController.php | 28 +++--- .../Front/Ajax/SettingsController.php | 92 +++++++----------- src/Jobs/DeployJob.php | 76 +++++++-------- src/LaravelDeployServiceProvider.php | 84 ++++++++--------- src/Utils/LogParser.php | 76 +++++++-------- 8 files changed, 237 insertions(+), 283 deletions(-) diff --git a/routes.php b/routes.php index 24e9f54..53d4064 100644 --- a/routes.php +++ b/routes.php @@ -3,53 +3,49 @@ * Created by PhpStorm. * User: kgbot * Date: 6/4/18 - * Time: 12:18 AM + * Time: 12:18 AM. */ - use KgBot\LaravelDeploy\Http\Middleware\IsValidToken; -Route::group( [ - 'prefix' => config( 'laravel-deploy.routes.prefix' ), - 'middleware' => array_merge( [ IsValidToken::class ], config( 'laravel-deploy.routes.middleware' ) ), +Route::group([ + 'prefix' => config('laravel-deploy.routes.prefix'), + 'middleware' => array_merge([IsValidToken::class], config('laravel-deploy.routes.middleware')), 'namespace' => 'KgBot\LaravelDeploy\Http\Controllers', ], function () { + Route::any('deploy', 'DeployController@request')->name('laravel-deploy.deploy.request'); +}); - Route::any( 'deploy', 'DeployController@request' )->name( 'laravel-deploy.deploy.request' ); -} ); - -/** +/* * Front-end routes */ -Route::group( [ +Route::group([ - 'prefix' => config( 'laravel-deploy.routes.prefix' ) . '/dashboard', - 'middleware' => array_merge( [ 'web', 'auth' ], config( 'laravel-deploy.front.routes.middleware' ) ), - 'namespace' => config( 'laravel-deploy.front.routes.namespace' ), + 'prefix' => config('laravel-deploy.routes.prefix').'/dashboard', + 'middleware' => array_merge(['web', 'auth'], config('laravel-deploy.front.routes.middleware')), + 'namespace' => config('laravel-deploy.front.routes.namespace'), ], function () { + Route::get('', 'DashboardController@index')->name('laravel-deploy.dashboard'); +}); - Route::get( '', 'DashboardController@index' )->name( 'laravel-deploy.dashboard' ); -} ); - -/** +/* * Ajax routes */ -Route::group( [ +Route::group([ - 'prefix' => config( 'laravel-deploy.routes.prefix' ) . '/ajax', - 'middleware' => array_merge( [ 'web', 'auth' ], config( 'laravel-deploy.front.routes.ajax.middleware' ) ), - 'namespace' => config( 'laravel-deploy.front.routes.ajax.namespace' ), + 'prefix' => config('laravel-deploy.routes.prefix').'/ajax', + 'middleware' => array_merge(['web', 'auth'], config('laravel-deploy.front.routes.ajax.middleware')), + 'namespace' => config('laravel-deploy.front.routes.ajax.namespace'), ], function () { + Route::post('/clients/{client}/status', 'ClientsController@changeStatus') + ->name('laravel-deploy.ajax.clients.status'); - Route::post( '/clients/{client}/status', 'ClientsController@changeStatus' ) - ->name( 'laravel-deploy.ajax.clients.status' ); + Route::post('/clients/{client}/auto-deploy', 'ClientsController@changeAutoDeploy') + ->name('laravel-deploy.ajax.clients.auto_deploy'); - Route::post( '/clients/{client}/auto-deploy', 'ClientsController@changeAutoDeploy' ) - ->name( 'laravel-deploy.ajax.clients.auto_deploy' ); + Route::resource('/clients', 'ClientsController', [ - Route::resource( '/clients', 'ClientsController', [ - - 'only' => [ 'index', 'store', 'update', 'destroy' ], + 'only' => ['index', 'store', 'update', 'destroy'], 'names' => [ 'index' => 'laravel-deploy.ajax.clients.index', @@ -57,34 +53,31 @@ 'update' => 'laravel-deploy.ajax.clients.update', 'destroy' => 'laravel-deploy.ajax.clients.destroy', ], - ] ); + ]); - /** + /* * Settings routes */ - Route::group( [ 'prefix' => 'settings' ], function () { - - Route::get( 'last-log', 'SettingsController@lastLog' )->name( 'laravel-deploy.ajax.settings.last_log' ); - Route::get( 'logs', 'SettingsController@allLogs' )->name( 'laravel-deploy.ajax.settings.logs' ); - Route::get( 'index', 'SettingsController@index' )->name( 'laravel-deploy.ajax.settings.index' ); + Route::group(['prefix' => 'settings'], function () { + Route::get('last-log', 'SettingsController@lastLog')->name('laravel-deploy.ajax.settings.last_log'); + Route::get('logs', 'SettingsController@allLogs')->name('laravel-deploy.ajax.settings.logs'); + Route::get('index', 'SettingsController@index')->name('laravel-deploy.ajax.settings.index'); - /** + /* * Deployments routes */ - Route::group( [ 'prefix' => 'deployments' ], function () { - - Route::post( 'deploy/now/{client}', 'SettingsController@deployNow' ) - ->name( 'laravel-deploy.ajax.settings.deployments.deploy_now' ); + Route::group(['prefix' => 'deployments'], function () { + Route::post('deploy/now/{client}', 'SettingsController@deployNow') + ->name('laravel-deploy.ajax.settings.deployments.deploy_now'); - /** + /* * Deployment script routes */ - Route::get( 'scripts/{client}', 'ClientScriptController@fetch' ) - ->name( 'laravel-deploy.ajax.settings.deployments.scripts.fetch' ); - - Route::post( 'scripts/{client}', 'ClientScriptController@save' ) - ->name( 'laravel-deploy.ajax.settings.deployments.scripts.save' ); - } ); - - } ); -} ); + Route::get('scripts/{client}', 'ClientScriptController@fetch') + ->name('laravel-deploy.ajax.settings.deployments.scripts.fetch'); + + Route::post('scripts/{client}', 'ClientScriptController@save') + ->name('laravel-deploy.ajax.settings.deployments.scripts.save'); + }); + }); +}); diff --git a/src/Http/Controllers/DeployController.php b/src/Http/Controllers/DeployController.php index 2a1b369..d05295d 100644 --- a/src/Http/Controllers/DeployController.php +++ b/src/Http/Controllers/DeployController.php @@ -3,38 +3,36 @@ * Created by PhpStorm. * User: kgbot * Date: 6/4/18 - * Time: 12:40 AM + * Time: 12:40 AM. */ namespace KgBot\LaravelDeploy\Http\Controllers; use Illuminate\Http\Request; -use KgBot\LaravelDeploy\Exceptions\UnableToReadScriptFile; -use KgBot\LaravelDeploy\Jobs\DeployJob; use KgBot\LaravelDeploy\Models\Client; +use KgBot\LaravelDeploy\Jobs\DeployJob; +use KgBot\LaravelDeploy\Exceptions\UnableToReadScriptFile; class DeployController extends BaseController { - public function request( Request $request ) + public function request(Request $request) { - $client = Client::where( [ - - [ 'token', $request->get( '_token' ) ], - [ 'active', true ], - [ 'auto_deploy', true ], - ] )->first(); + $client = Client::where([ - $filename = $client->script_source; - $script_file = base_path( $filename ); + ['token', $request->get('_token')], + ['active', true], + ['auto_deploy', true], + ])->first(); - if ( !file_exists( $script_file ) ) { + $filename = $client->script_source; + $script_file = base_path($filename); + if (! file_exists($script_file)) { throw new UnableToReadScriptFile(); - } - dispatch( new DeployJob( $client, $script_file ) )->onQueue( config( 'laravel-deploy.queue', 'default' ) ); + dispatch(new DeployJob($client, $script_file))->onQueue(config('laravel-deploy.queue', 'default')); - return response()->json( 'success' ); + return response()->json('success'); } -} \ No newline at end of file +} diff --git a/src/Http/Controllers/Front/Ajax/ClientScriptController.php b/src/Http/Controllers/Front/Ajax/ClientScriptController.php index 894ae65..17ce752 100644 --- a/src/Http/Controllers/Front/Ajax/ClientScriptController.php +++ b/src/Http/Controllers/Front/Ajax/ClientScriptController.php @@ -3,49 +3,40 @@ namespace KgBot\LaravelDeploy\Http\Controllers\Front\Ajax; use Illuminate\Http\Request; -use KgBot\LaravelDeploy\Http\Controllers\BaseController; use KgBot\LaravelDeploy\Models\Client; use League\Flysystem\FileNotFoundException; +use KgBot\LaravelDeploy\Http\Controllers\BaseController; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; class ClientScriptController extends BaseController { - public function fetch( Client $client ) + public function fetch(Client $client) { - $filepath = base_path( DIRECTORY_SEPARATOR . $client->script_source ); - - if ( file_exists( $filepath ) ) { + $filepath = base_path(DIRECTORY_SEPARATOR.$client->script_source); - $content = file_get_contents( $filepath ); - - return response()->json( compact( 'content' ) ); + if (file_exists($filepath)) { + $content = file_get_contents($filepath); + return response()->json(compact('content')); } else { - - throw new FileNotFoundException( 'We can\'t find deploy script defined for this client' ); + throw new FileNotFoundException('We can\'t find deploy script defined for this client'); } } - public function save( Client $client, Request $request ) + public function save(Client $client, Request $request) { - if ( $request->has( 'content' ) && $content = $request->get( 'content' ) ) { - - $filepath = base_path( DIRECTORY_SEPARATOR . $client->script_source ); + if ($request->has('content') && $content = $request->get('content')) { + $filepath = base_path(DIRECTORY_SEPARATOR.$client->script_source); - if ( file_exists( $filepath ) ) { - - file_put_contents( $filepath, $content ); - - return response()->json( 'success' ); + if (file_exists($filepath)) { + file_put_contents($filepath, $content); + return response()->json('success'); } else { - - throw new FileNotFoundException( 'We can\'t find deploy script defined for this client' ); + throw new FileNotFoundException('We can\'t find deploy script defined for this client'); } - } else { - - throw new BadRequestHttpException( 'Parameter content is required.' ); + throw new BadRequestHttpException('Parameter content is required.'); } } } diff --git a/src/Http/Controllers/Front/Ajax/ClientsController.php b/src/Http/Controllers/Front/Ajax/ClientsController.php index 7b71c2d..1b36bb1 100644 --- a/src/Http/Controllers/Front/Ajax/ClientsController.php +++ b/src/Http/Controllers/Front/Ajax/ClientsController.php @@ -2,9 +2,9 @@ namespace KgBot\LaravelDeploy\Http\Controllers\Front\Ajax; +use KgBot\LaravelDeploy\Models\Client; use KgBot\LaravelDeploy\Http\Controllers\BaseController; use KgBot\LaravelDeploy\Http\Requests\Clients\ClientRequest; -use KgBot\LaravelDeploy\Models\Client; class ClientsController extends BaseController { @@ -17,7 +17,7 @@ public function index() { $clients = Client::all(); - return response()->json( compact( 'clients' ) ); + return response()->json(compact('clients')); } /** @@ -27,11 +27,11 @@ public function index() * * @return \Illuminate\Http\Response */ - public function store( ClientRequest $request ) + public function store(ClientRequest $request) { - $client = Client::create( $request->all() ); + $client = Client::create($request->all()); - return response()->json( compact( 'client' ) ); + return response()->json(compact('client')); } /** @@ -42,11 +42,11 @@ public function store( ClientRequest $request ) * * @return \Illuminate\Http\Response */ - public function update( ClientRequest $request, Client $client ) + public function update(ClientRequest $request, Client $client) { - $client->update( $request->all() ); + $client->update($request->all()); - return response()->json( compact( 'client' ) ); + return response()->json(compact('client')); } /** @@ -56,24 +56,24 @@ public function update( ClientRequest $request, Client $client ) * * @return \Illuminate\Http\Response */ - public function destroy( Client $client ) + public function destroy(Client $client) { $client->delete(); - return response()->json( 'success' ); + return response()->json('success'); } - public function changeStatus( Client $client ) + public function changeStatus(Client $client) { $client->changeStatus(); - return response()->json( 'success' ); + return response()->json('success'); } - public function changeAutoDeploy( Client $client ) + public function changeAutoDeploy(Client $client) { $client->changeAutoDeploy(); - return response()->json( compact( $client ) ); + return response()->json(compact($client)); } } diff --git a/src/Http/Controllers/Front/Ajax/SettingsController.php b/src/Http/Controllers/Front/Ajax/SettingsController.php index 166f2a5..d74dd0a 100644 --- a/src/Http/Controllers/Front/Ajax/SettingsController.php +++ b/src/Http/Controllers/Front/Ajax/SettingsController.php @@ -2,78 +2,64 @@ namespace KgBot\LaravelDeploy\Http\Controllers\Front\Ajax; -use KgBot\LaravelDeploy\Http\Controllers\BaseController; -use KgBot\LaravelDeploy\Jobs\DeployJob; use KgBot\LaravelDeploy\Models\Client; +use KgBot\LaravelDeploy\Jobs\DeployJob; use KgBot\LaravelDeploy\Utils\LogParser; +use KgBot\LaravelDeploy\Http\Controllers\BaseController; class SettingsController extends BaseController { - /** - * Return last deploy log + * Return last deploy log. * * @return \Illuminate\Http\JsonResponse * @throws \Exception */ public function lastLog() { - $reader = $this->getLogs(); - if ( is_null( $reader ) ) { - - return response()->json( 'Log file path does not exist, check your configuration and try again.', 404 ); + if (is_null($reader)) { + return response()->json('Log file path does not exist, check your configuration and try again.', 404); } - if ( count( $reader ) ) { - - $log = $reader[ 0 ]; - - return response()->json( [ 'log' => $log ] ); + if (count($reader)) { + $log = $reader[0]; + return response()->json(['log' => $log]); } else { - - return response()->json( 'Log is empty, deploy something!!!', 404 ); + return response()->json('Log is empty, deploy something!!!', 404); } } /** - * Read deployment log file and return it's content + * Read deployment log file and return it's content. * * @return array|\KgBot\LaravelDeploy\Utils\LogParser|null * @throws \Exception */ protected function getLogs() { - $log_file_name = config( 'laravel-deploy.log_file_name', 'laravel-log' ); - $path = storage_path( 'logs/' . $log_file_name ); - - if ( file_exists( $path ) ) { - - $file = file_get_contents( $path ); - if ( $file ) { + $log_file_name = config('laravel-deploy.log_file_name', 'laravel-log'); + $path = storage_path('logs/'.$log_file_name); + if (file_exists($path)) { + $file = file_get_contents($path); + if ($file) { $reader = new LogParser(); - $reader = $reader->parse( $file ); + $reader = $reader->parse($file); return $reader; - } else { - - throw new \Exception( 'Couldn\'t read deployment log file.' ); + throw new \Exception('Couldn\'t read deployment log file.'); } - - } else { - - return null; - + return; } } /** - * Return collection of all deploy logs + * Return collection of all deploy logs. * * @return \Illuminate\Http\JsonResponse * @throws \Exception @@ -82,39 +68,34 @@ public function allLogs() { $reader = $this->getLogs(); - if ( is_null( $reader ) ) { - - return response()->json( 'Log file path does not exist, check your configuration and try again.', 404 ); + if (is_null($reader)) { + return response()->json('Log file path does not exist, check your configuration and try again.', 404); } - if ( count( $reader ) ) { - - return response()->json( [ 'logs' => collect( $reader ) ] ); - + if (count($reader)) { + return response()->json(['logs' => collect($reader)]); } else { - - return response()->json( 'Log is empty, deploy something!!!', 404 ); + return response()->json('Log is empty, deploy something!!!', 404); } } /** - * Start deployment from web dashboard + * Start deployment from web dashboard. * * @param \KgBot\LaravelDeploy\Models\Client $client * * @return \Illuminate\Http\JsonResponse */ - public function deployNow( Client $client ) + public function deployNow(Client $client) { + dispatch(new DeployJob($client, + base_path($client->script_source)))->onQueue(config('laravel-deploy.queue')); - dispatch( new DeployJob( $client, - base_path( $client->script_source ) ) )->onQueue( config( 'laravel-deploy.queue' ) ); - - return response()->json( 'success' ); + return response()->json('success'); } /** - * Open settings page of web dashboard + * Open settings page of web dashboard. * * @return \Illuminate\Http\JsonResponse */ @@ -125,18 +106,17 @@ public function index() */ $clients = Client::Active()->get(); - $clients = $clients->each( function ( $client ) { - - $enabled = ( $client->active ) ? 'Enabled' : 'Disabled'; + $clients = $clients->each(function ($client) { + $enabled = ($client->active) ? 'Enabled' : 'Disabled'; - return $client->text = $client->name . ' - ' . $enabled; - } ); + return $client->text = $client->name.' - '.$enabled; + }); $settings = [ - 'quick_deploy' => config( 'laravel-deploy.run_deploy' ), + 'quick_deploy' => config('laravel-deploy.run_deploy'), ]; - return response()->json( compact( 'clients', 'settings' ) ); + return response()->json(compact('clients', 'settings')); } } diff --git a/src/Jobs/DeployJob.php b/src/Jobs/DeployJob.php index 5212564..154f88e 100644 --- a/src/Jobs/DeployJob.php +++ b/src/Jobs/DeployJob.php @@ -2,26 +2,26 @@ namespace KgBot\LaravelDeploy\Jobs; +use Monolog\Logger; use Illuminate\Bus\Queueable; +use Monolog\Handler\StreamHandler; +use Monolog\Formatter\LineFormatter; +use Illuminate\Queue\SerializesModels; +use KgBot\LaravelDeploy\Models\Client; +use Symfony\Component\Process\Process; +use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; -use Illuminate\Queue\InteractsWithQueue; -use Illuminate\Queue\SerializesModels; use KgBot\LaravelDeploy\Events\LaravelDeployFailed; -use KgBot\LaravelDeploy\Events\LaravelDeployFinished; use KgBot\LaravelDeploy\Events\LaravelDeployStarted; -use KgBot\LaravelDeploy\Models\Client; -use Monolog\Formatter\LineFormatter; -use Monolog\Handler\StreamHandler; -use Monolog\Logger; -use Symfony\Component\Process\Process; +use KgBot\LaravelDeploy\Events\LaravelDeployFinished; class DeployJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public $timeout = 240; - public $tries = 1; + public $tries = 1; public $client; public $script_file; @@ -37,19 +37,18 @@ class DeployJob implements ShouldQueue protected $process; /** - * @var string $command Command to be executed + * @var string Command to be executed */ protected $command; - /** * Create a new job instance. * * @return void */ - public function __construct( Client $client, string $script_file ) + public function __construct(Client $client, string $script_file) { - $this->client = $client; + $this->client = $client; $this->script_file = $script_file; $this->setLogger(); $this->setProcess(); @@ -57,30 +56,29 @@ public function __construct( Client $client, string $script_file ) protected function setLogger() { - $this->logger = new Logger( 'laravel-deploy-logger' ); + $this->logger = new Logger('laravel-deploy-logger'); - $log_file_name = config( 'laravel-deploy.log_file_name', 'laravel-log' ); - $stream = new StreamHandler( storage_path( '/logs/' . $log_file_name ), Logger::DEBUG ); - $formatter = tap( new LineFormatter( null, null, true, true ), function ( $formatter ) { + $log_file_name = config('laravel-deploy.log_file_name', 'laravel-log'); + $stream = new StreamHandler(storage_path('/logs/'.$log_file_name), Logger::DEBUG); + $formatter = tap(new LineFormatter(null, null, true, true), function ($formatter) { $formatter->includeStacktraces(); - } ); - $stream->setFormatter( $formatter ); + }); + $stream->setFormatter($formatter); - - $this->logger->pushHandler( $stream ); + $this->logger->pushHandler($stream); } protected function setProcess() { - $command = 'echo ' . config( 'laravel-deploy.user.password' ); - $command .= ' | sudo -S -u ' . config( 'laravel-deploy.user.username' ); - $command .= ' sh ' . $this->script_file; + $command = 'echo '.config('laravel-deploy.user.password'); + $command .= ' | sudo -S -u '.config('laravel-deploy.user.username'); + $command .= ' sh '.$this->script_file; $this->command = $command; - $process = new Process( $command ); - $process->setTimeout( 500 ); - $process->setIdleTimeout( 100 ); + $process = new Process($command); + $process->setTimeout(500); + $process->setIdleTimeout(100); $this->process = $process; } @@ -92,26 +90,22 @@ protected function setProcess() */ public function handle() { - event( new LaravelDeployStarted( $this->client, $this->command ) ); + event(new LaravelDeployStarted($this->client, $this->command)); $this->process->run(); - if ( $this->process->isSuccessful() ) { - - $this->logger->info( PHP_EOL . $this->process->getOutput(), [ - 'client' => json_encode( $this->client ), + if ($this->process->isSuccessful()) { + $this->logger->info(PHP_EOL.$this->process->getOutput(), [ + 'client' => json_encode($this->client), 'script' => $this->script_file, - ] ); - event( new LaravelDeployFinished( $this->client, $this->process->getOutput() ) ); - + ]); + event(new LaravelDeployFinished($this->client, $this->process->getOutput())); } else { - - $this->logger->critical( PHP_EOL . $this->process->getOutput(), [ - 'client' => json_encode( $this->client ), + $this->logger->critical(PHP_EOL.$this->process->getOutput(), [ + 'client' => json_encode($this->client), 'script' => $this->script_file, - ] ); - event( new LaravelDeployFailed( $this->client, $this->process->getOutput() ) ); + ]); + event(new LaravelDeployFailed($this->client, $this->process->getOutput())); } - } } diff --git a/src/LaravelDeployServiceProvider.php b/src/LaravelDeployServiceProvider.php index 0136499..9958d70 100644 --- a/src/LaravelDeployServiceProvider.php +++ b/src/LaravelDeployServiceProvider.php @@ -3,12 +3,11 @@ * Created by PhpStorm. * User: kgbot * Date: 6/3/18 - * Time: 11:48 PM + * Time: 11:48 PM. */ namespace KgBot\LaravelDeploy; - use Illuminate\Support\Facades\View; use Illuminate\Support\ServiceProvider; use KgBot\LaravelDeploy\Console\Commands\NewClient; @@ -18,87 +17,86 @@ class LaravelDeployServiceProvider extends ServiceProvider { public function register() { - /** + /* * We define those globals because we need them in LogParser */ - if ( !defined( 'REGEX_DATE_PATTERN' ) ) { - define( 'REGEX_DATE_PATTERN', '\d{4}(-\d{2}){2}' ); // YYYY-MM-DD + if (! defined('REGEX_DATE_PATTERN')) { + define('REGEX_DATE_PATTERN', '\d{4}(-\d{2}){2}'); // YYYY-MM-DD } - if ( !defined( 'REGEX_TIME_PATTERN' ) ) { - define( 'REGEX_TIME_PATTERN', '\d{2}(:\d{2}){2}' ); // HH:MM:SS + if (! defined('REGEX_TIME_PATTERN')) { + define('REGEX_TIME_PATTERN', '\d{2}(:\d{2}){2}'); // HH:MM:SS } - if ( !defined( 'REGEX_DATETIME_PATTERN' ) ) { + if (! defined('REGEX_DATETIME_PATTERN')) { define( 'REGEX_DATETIME_PATTERN', - REGEX_DATE_PATTERN . ' ' . REGEX_TIME_PATTERN // YYYY-MM-DD HH:MM:SS + REGEX_DATE_PATTERN.' '.REGEX_TIME_PATTERN // YYYY-MM-DD HH:MM:SS ); } } public function boot() { - View::composer( 'laravel-deploy::dashboard', function ( $view ) { - - return $view->with( [ + View::composer('laravel-deploy::dashboard', function ($view) { + return $view->with([ 'user' => auth()->user(), 'messages' => ExportLocalizations::export()->toFlat(), - ] ); - } ); + ]); + }); - if ( $this->app->runningInConsole() ) { - $this->commands( [ + if ($this->app->runningInConsole()) { + $this->commands([ NewClient::class, - ] ); + ]); } - /** + /* * Config */ $this->mergeConfigFrom( - __DIR__ . '//config/laravel-deploy.php', 'laravel-deploy' + __DIR__.'//config/laravel-deploy.php', 'laravel-deploy' ); - $this->publishes( [ - __DIR__ . '/config/laravel-deploy.php' => config_path( 'laravel-deploy.php' ), - ], 'config' ); + $this->publishes([ + __DIR__.'/config/laravel-deploy.php' => config_path('laravel-deploy.php'), + ], 'config'); - /** + /* * Migrations */ - $this->publishes( [ - __DIR__ . '/database/migrations/' => database_path( 'migrations' ), - ], 'migrations' ); + $this->publishes([ + __DIR__.'/database/migrations/' => database_path('migrations'), + ], 'migrations'); - /** + /* * Routes */ - $this->loadRoutesFrom( __DIR__ . '/../routes.php' ); + $this->loadRoutesFrom(__DIR__.'/../routes.php'); - /** + /* * Assets */ - $this->publishes( [ + $this->publishes([ - __DIR__ . '/resources/assets/' => resource_path( 'assets/vendor/laravel-deploy' ), - ], 'assets' ); + __DIR__.'/resources/assets/' => resource_path('assets/vendor/laravel-deploy'), + ], 'assets'); - /** + /* * Localization */ - $this->loadTranslationsFrom( __DIR__ . '/resource/lang', 'laravel-deploy' ); - $this->publishes( [ + $this->loadTranslationsFrom(__DIR__.'/resource/lang', 'laravel-deploy'); + $this->publishes([ - __DIR__ . '/resources/lang' => resource_path( 'lang/vendor/laravel-deploy' ), - ], 'lang' ); + __DIR__.'/resources/lang' => resource_path('lang/vendor/laravel-deploy'), + ], 'lang'); - /** + /* * Views */ - $this->loadViewsFrom( __DIR__ . '/resources/views', 'laravel-deploy' ); + $this->loadViewsFrom(__DIR__.'/resources/views', 'laravel-deploy'); - $this->publishes( [ + $this->publishes([ - __DIR__ . '/resources/views' => resource_path( 'views/vendor/laravel-deploy' ), - ], 'views' ); + __DIR__.'/resources/views' => resource_path('views/vendor/laravel-deploy'), + ], 'views'); } -} \ No newline at end of file +} diff --git a/src/Utils/LogParser.php b/src/Utils/LogParser.php index 2571526..b793e2e 100644 --- a/src/Utils/LogParser.php +++ b/src/Utils/LogParser.php @@ -3,15 +3,14 @@ * Created by PhpStorm. * User: kgbot * Date: 6/22/18 - * Time: 2:39 PM + * Time: 2:39 PM. */ namespace KgBot\LaravelDeploy\Utils; - use Carbon\Carbon; -use Illuminate\Support\Str; use Psr\Log\LogLevel; +use Illuminate\Support\Str; class LogParser { @@ -30,22 +29,21 @@ class LogParser /** * @var string This determines log heading */ - protected $heading_pattern = '/\[' . REGEX_DATE_PATTERN . ' ' . REGEX_TIME_PATTERN . '\].*:/'; + protected $heading_pattern = '/\['.REGEX_DATE_PATTERN.' '.REGEX_TIME_PATTERN.'\].*:/'; /** * @var string Log date pattern */ - protected $date_patern = '/' . REGEX_DATE_PATTERN . ' ' . REGEX_TIME_PATTERN . '/'; + protected $date_patern = '/'.REGEX_DATE_PATTERN.' '.REGEX_TIME_PATTERN.'/'; protected $extra_patter = '/{.*:".*".*}/D'; public function __construct() { - $log_levels_class = new \ReflectionClass( LogLevel::class ); - - foreach ( $log_levels_class->getConstants() as $level ) { + $log_levels_class = new \ReflectionClass(LogLevel::class); - array_push( $this->levels, $level ); + foreach ($log_levels_class->getConstants() as $level) { + array_push($this->levels, $level); } } @@ -56,28 +54,30 @@ public function __construct() * * @return array */ - public function parse( $raw ) + public function parse($raw) { $this->parsed = []; - list( $headings, $data ) = $this->parseRawData( $raw ); + list($headings, $data) = $this->parseRawData($raw); // @codeCoverageIgnoreStart - if ( !is_array( $headings ) ) { + if (! is_array($headings)) { return $this->parsed; } // @codeCoverageIgnoreEnd - foreach ( $headings as $heading ) { - for ( $i = 0, $j = count( $heading ); $i < $j; $i++ ) { - $this->populateEntries( $heading, $data, $i ); + foreach ($headings as $heading) { + for ($i = 0, $j = count($heading); $i < $j; $i++) { + $this->populateEntries($heading, $data, $i); } - }; - unset( $headings, $data ); + } + unset($headings, $data); - return array_reverse( $this->parsed ); + return array_reverse($this->parsed); } + /* ----------------------------------------------------------------- | Other Methods | ----------------------------------------------------------------- */ + /** * Parse raw data. * @@ -85,16 +85,16 @@ public function parse( $raw ) * * @return array */ - private function parseRawData( $raw ) + private function parseRawData($raw) { - preg_match_all( $this->heading_pattern, $raw, $headings ); - $data = preg_split( $this->heading_pattern, $raw ); - if ( $data[ 0 ] < 1 ) { - $trash = array_shift( $data ); - unset( $trash ); + preg_match_all($this->heading_pattern, $raw, $headings); + $data = preg_split($this->heading_pattern, $raw); + if ($data[0] < 1) { + $trash = array_shift($data); + unset($trash); } - return [ $headings, $data ]; + return [$headings, $data]; } /** @@ -104,24 +104,24 @@ private function parseRawData( $raw ) * @param array $data * @param int $key */ - private function populateEntries( $heading, $data, $key ) + private function populateEntries($heading, $data, $key) { - foreach ( $this->levels as $level ) { - if ( self::hasLogLevel( $heading[ $key ], $level ) ) { + foreach ($this->levels as $level) { + if (self::hasLogLevel($heading[$key], $level)) { // We use this to get the "extra" part from Monolog logger and provide it to user - preg_match( $this->extra_patter, $data[ $key ], $extra, null, 0 ); + preg_match($this->extra_patter, $data[$key], $extra, null, 0); // Here we just remove Monolog "extra" argument from string - $without_extra = preg_split( $this->extra_patter, $data[ $key ] )[ 0 ]; + $without_extra = preg_split($this->extra_patter, $data[$key])[0]; - preg_match( $this->date_patern, $heading[ $key ], $created_at ); + preg_match($this->date_patern, $heading[$key], $created_at); $this->parsed[] = [ - 'date' => Carbon::parse( $created_at[ 0 ] ), - 'level' => strtoupper( $level ), - 'header' => $heading[ $key ], + 'date' => Carbon::parse($created_at[0]), + 'level' => strtoupper($level), + 'header' => $heading[$key], 'message' => $without_extra, - 'extra' => $extra ? json_decode( $extra[ 0 ] ) : [], + 'extra' => $extra ? json_decode($extra[0]) : [], ]; } } @@ -135,8 +135,8 @@ private function populateEntries( $heading, $data, $key ) * * @return bool */ - private function hasLogLevel( $heading, $level ) + private function hasLogLevel($heading, $level) { - return Str::contains( strtolower( $heading ), strtolower( '.' . $level ) ); + return Str::contains(strtolower($heading), strtolower('.'.$level)); } -} \ No newline at end of file +}