diff --git a/logs/v2.0.0.txt b/logs/v2.0.0.txt deleted file mode 100644 index 4019cfe..0000000 --- a/logs/v2.0.0.txt +++ /dev/null @@ -1,15 +0,0 @@ -- we now use commands instead of events, so plz remove the v1 setup if you still have - -+ composer.json --> 'App\\Providers\\EventServiceProvider::postAutoloadDump' -+ app\Providers\EventServiceProvider --> -``` -/** -* "ctf0/package-changelog". -*/ -public static function postAutoloadDump(\Composer\Script\Event $event) -{ - if (class_exists('ctf0\PackageChangeLog\Ops')) { - return \ctf0\PackageChangeLog\Ops::postAutoloadDump($event); - } -} -``` diff --git a/logs/v2.0.2.txt b/logs/v2.0.2.txt new file mode 100644 index 0000000..568afea --- /dev/null +++ b/logs/v2.0.2.txt @@ -0,0 +1 @@ +- fix using incorrect event, plz move `@php artisan pcl:post-update` from `scripts:post-autoload-dump` to "scripts:post-update-cmd" diff --git a/src/PackageChangeLogServiceProvider.php b/src/PackageChangeLogServiceProvider.php index f3da5a4..833b307 100644 --- a/src/PackageChangeLogServiceProvider.php +++ b/src/PackageChangeLogServiceProvider.php @@ -11,7 +11,8 @@ class PackageChangeLogServiceProvider extends ServiceProvider public function boot() { - if (file_exists(base_path() . '/composer.json') && + if ( + file_exists(\base_path() . '/composer.json') && !$this->app['cache']->store('file')->has('ct-pcl') ) { $this->autoReg(); @@ -28,11 +29,7 @@ public function boot() protected function autoReg() { - $comp_file = base_path() . '/composer.json'; - - $this->postUpdate($comp_file); - $this->postInstall($comp_file); - $this->preUninstall($comp_file); + $this->doStuff(); // run check once $this->app['cache']->store('file')->rememberForever('ct-pcl', function () { diff --git a/src/Traits/Init.php b/src/Traits/Init.php index c75aac5..aad5511 100644 --- a/src/Traits/Init.php +++ b/src/Traits/Init.php @@ -6,65 +6,31 @@ trait Init { - /* -------------------------------------------------------------------------- */ - /* OPS */ - /* -------------------------------------------------------------------------- */ - - protected function postUpdate($comp_file) + protected function doStuff() { - $search = '@php artisan pcl:post-update'; - - if (!$this->checkExist($comp_file, $search)) { - $data = $search; - $res = json_decode(file_get_contents($comp_file), true); - - if ($res['scripts']['post-autoload-dump']) { - array_push($res['scripts']['post-autoload-dump'], $data); - $json = json_encode($res, JSON_PRETTY_PRINT); - $final = str_replace('\/', '/', $json); - file_put_contents($comp_file, $final); + $comp_file = \base_path() . '/composer.json'; + $json_data = file_get_contents($comp_file); + $list = [ + 'post-install' => 'post-install-cmd', + 'post-update' => 'post-update-cmd', + 'pre-uninstall' => 'pre-package-uninstall', + ]; + + $final = json_decode($json_data, true); + + foreach ($list as $cmnd => $event) { + $search = "@php artisan pcl:$cmnd"; + + if (!Str::contains($json_data, $search)) { + isset($final['scripts'][$event]) + ? array_push($final['scripts'][$event], $search) + : $final['scripts'][$event] = [$search]; } } - } - - protected function postInstall($comp_file) - { - $search = '@php artisan pcl:post-install'; - - if (!$this->checkExist($comp_file, $search)) { - $res = json_decode(file_get_contents($comp_file), true); - - isset($res['scripts']['post-install-cmd']) - ? array_push($res['scripts']['post-install-cmd'], $search) - : $res['scripts']['post-install-cmd'] = [$search]; - $json = json_encode($res, JSON_PRETTY_PRINT); - $final = str_replace('\/', '/', $json); - file_put_contents($comp_file, $final); - } - } + $json = json_encode($final, JSON_PRETTY_PRINT); + $final = str_replace('\/', '/', $json); - protected function preUninstall($comp_file) - { - $search = '@php artisan pcl:pre-uninstall'; - - if (!$this->checkExist($comp_file, $search)) { - $res = json_decode(file_get_contents($comp_file), true); - - isset($res['scripts']['pre-package-uninstall']) - ? array_push($res['scripts']['pre-package-uninstall'], $search) - : $res['scripts']['pre-package-uninstall'] = [$search]; - - $json = json_encode($res, JSON_PRETTY_PRINT); - $final = str_replace('\/', '/', $json); - file_put_contents($comp_file, $final); - } - } - - /* -------------------------------------------------------------------------- */ - - protected function checkExist($file, $search) - { - return Str::contains(file_get_contents($file), $search); + return file_put_contents($comp_file, $final); } }