From c3e4aad66dc6a1248d9ccb457dd78be979297288 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Sat, 20 May 2023 11:04:57 +0330 Subject: [PATCH 1/5] feat: add command `settings:publish` --- src/Config/Commands/SettingsPublisher.php | 95 +++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/Config/Commands/SettingsPublisher.php diff --git a/src/Config/Commands/SettingsPublisher.php b/src/Config/Commands/SettingsPublisher.php new file mode 100644 index 0000000..af6fce0 --- /dev/null +++ b/src/Config/Commands/SettingsPublisher.php @@ -0,0 +1,95 @@ + + */ + protected $options = [ + '-f' => 'Set to enable overwrites.', + ]; + + /** + * @var bool `true` to enable overwrites file + */ + private bool $overwrites = false; + + public function run(array $params): void + { + if (array_key_exists('f', $params)) { + $this->overwrites = true; + } + + // Use the Autoloader to figure out the module path + $source = service('autoloader')->getNamespace('CodeIgniter\\Settings')[0]; + + $publisher = new Publisher($source, APPPATH); + + try { + $publisher->addPath('Config//Settings.php') + ->merge($this->overwrites); + + } catch (Throwable $e) { + + $this->showError($e); + } + + // If publication succeeded then update file + foreach ($publisher->getPublished() as $file) { + // Replace data in file + $contents = file_get_contents($file); + $contents = str_replace('namespace CodeIgniter\\Settings\\Config', 'namespace Config', $contents); + $contents = str_replace('use CodeIgniter\\Config\\BaseConfig', 'use CodeIgniter\\Settings\\Config\\Settings as SettingsConfig', $contents); + $contents = str_replace('class Settings extends BaseConfig', 'class Settings extends SettingsConfig', $contents); + file_put_contents($file, $contents); + CLI::write(CLI::color(' Published! ', 'green') . "You can customize the configuration by editing the \"{$file}\" file."); + } + + } +} From e9a4a8f89931e266da611af8bce818f84c495315 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Sat, 20 May 2023 11:05:47 +0330 Subject: [PATCH 2/5] tests: add test for command `settings:publish` --- tests/Commands/SettingsPublisherTest.php | 93 ++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 tests/Commands/SettingsPublisherTest.php diff --git a/tests/Commands/SettingsPublisherTest.php b/tests/Commands/SettingsPublisherTest.php new file mode 100644 index 0000000..1d93581 --- /dev/null +++ b/tests/Commands/SettingsPublisherTest.php @@ -0,0 +1,93 @@ +=')) { + CITestStreamFilter::registration(); + CITestStreamFilter::addOutputFilter(); + CITestStreamFilter::addErrorFilter(); + } else { + CITestStreamFilter::$buffer = ''; + + $this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter'); + $this->streamFilter = stream_filter_append(STDERR, 'CITestStreamFilter'); + } + + } + + protected function tearDown(): void + { + parent::tearDown(); + + if (version_compare(CodeIgniter::CI_VERSION, '4.3.0', '>=')) { + CITestStreamFilter::removeOutputFilter(); + CITestStreamFilter::removeErrorFilter(); + } else { + stream_filter_remove($this->streamFilter); + } + } + + public function testPublishConfigFile(): void + { + command('settings:publish'); + + $filepath = APPPATH . 'Config/Settings.php'; + $this->assertFileExists($filepath); + $this->assertStringContainsString(' Published! ', CITestStreamFilter::$buffer); + + $contents = $this->getFileContents($filepath); + $this->assertStringContainsString('namespace Config;', $contents); + $this->assertStringContainsString('use CodeIgniter\\Settings\\Config\\Settings as SettingsConfig;', $contents); + $this->assertStringContainsString('class Settings extends SettingsConfig', $contents); + + if (is_file($filepath)) { + copy($filepath, APPPATH . 'Config/Settings.php.bak'); + } + } + + public function testPublishConfigFileWithForce(): void + { + + $filepath = APPPATH . 'Config/Settings.php'; + + helper('filesystem'); + write_file($filepath, 'fake text.'); + $contents = $this->getFileContents($filepath); + + $this->assertFileExists($filepath); + $this->assertStringContainsString('fake text.', $contents); + + command('settings:publish -f'); + + $expectedConfigFile = APPPATH . 'Config/Settings.php.bak'; + $this->assertFileEquals($expectedConfigFile, $filepath); + + clearstatcache(true, $expectedConfigFile); + if (is_file($expectedConfigFile)) { + unlink($expectedConfigFile); + } + + } + + private function getFileContents(string $filepath): string + { + return (string) @file_get_contents($filepath); + } +} From 0b113d9b948cc41003caf21a15dea6fffcf23d32 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Sat, 20 May 2023 11:16:28 +0330 Subject: [PATCH 3/5] chore: fix CQ error --- phpstan.neon.dist | 44 +++++++++---------- .../Commands/SettingsPublisher.php | 0 tests/Commands/SettingsPublisherTest.php | 25 +++-------- 3 files changed, 28 insertions(+), 41 deletions(-) rename src/{Config => }/Commands/SettingsPublisher.php (100%) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index f27f414..30a946d 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,22 +1,22 @@ -parameters: - tmpDir: build/phpstan - level: 5 - paths: - - src/ - - tests/ - bootstrapFiles: - - vendor/codeigniter4/framework/system/Test/bootstrap.php - excludePaths: - - src/Config/Routes.php - - src/Views/* - ignoreErrors: - universalObjectCratesClasses: - - CodeIgniter\Entity - - CodeIgniter\Entity\Entity - - Faker\Generator - scanDirectories: - - vendor/codeigniter4/framework/system/Helpers - dynamicConstantNames: - - APP_NAMESPACE - - CI_DEBUG - - ENVIRONMENT +parameters: + tmpDir: build/phpstan + level: 5 + paths: + - src/ + - tests/ + bootstrapFiles: + - vendor/codeigniter4/framework/system/Test/bootstrap.php + excludePaths: + - src/Config/Routes.php + - src/Views/* + ignoreErrors: + universalObjectCratesClasses: + - CodeIgniter\Entity + - CodeIgniter\Entity\Entity + - Faker\Generator + scanDirectories: + - vendor/codeigniter4/framework/system/Helpers + dynamicConstantNames: + - APP_NAMESPACE + - CI_DEBUG + - ENVIRONMENT diff --git a/src/Config/Commands/SettingsPublisher.php b/src/Commands/SettingsPublisher.php similarity index 100% rename from src/Config/Commands/SettingsPublisher.php rename to src/Commands/SettingsPublisher.php diff --git a/tests/Commands/SettingsPublisherTest.php b/tests/Commands/SettingsPublisherTest.php index 1d93581..2bea791 100644 --- a/tests/Commands/SettingsPublisherTest.php +++ b/tests/Commands/SettingsPublisherTest.php @@ -4,7 +4,6 @@ namespace Tests\Commands; -use CodeIgniter\CodeIgniter; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\Filters\CITestStreamFilter; @@ -13,22 +12,13 @@ */ final class SettingsPublisherTest extends CIUnitTestCase { - private $streamFilter; - protected function setUp(): void { parent::setUp(); - if (version_compare(CodeIgniter::CI_VERSION, '4.3.0', '>=')) { - CITestStreamFilter::registration(); - CITestStreamFilter::addOutputFilter(); - CITestStreamFilter::addErrorFilter(); - } else { - CITestStreamFilter::$buffer = ''; - - $this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter'); - $this->streamFilter = stream_filter_append(STDERR, 'CITestStreamFilter'); - } + CITestStreamFilter::registration(); + CITestStreamFilter::addOutputFilter(); + CITestStreamFilter::addErrorFilter(); } @@ -36,12 +26,9 @@ protected function tearDown(): void { parent::tearDown(); - if (version_compare(CodeIgniter::CI_VERSION, '4.3.0', '>=')) { - CITestStreamFilter::removeOutputFilter(); - CITestStreamFilter::removeErrorFilter(); - } else { - stream_filter_remove($this->streamFilter); - } + CITestStreamFilter::removeOutputFilter(); + CITestStreamFilter::removeErrorFilter(); + } public function testPublishConfigFile(): void From 0070db93870feeff0fb05089995684100ff2bc39 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Sat, 20 May 2023 23:04:10 +0330 Subject: [PATCH 4/5] remove double slashes and extra whitespace --- src/Commands/SettingsPublisher.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Commands/SettingsPublisher.php b/src/Commands/SettingsPublisher.php index af6fce0..c48d889 100644 --- a/src/Commands/SettingsPublisher.php +++ b/src/Commands/SettingsPublisher.php @@ -67,16 +67,13 @@ public function run(array $params): void } // Use the Autoloader to figure out the module path - $source = service('autoloader')->getNamespace('CodeIgniter\\Settings')[0]; - + $source = service('autoloader')->getNamespace('CodeIgniter\\Settings')[0]; $publisher = new Publisher($source, APPPATH); try { - $publisher->addPath('Config//Settings.php') + $publisher->addPath('Config/Settings.php') ->merge($this->overwrites); - } catch (Throwable $e) { - $this->showError($e); } @@ -90,6 +87,5 @@ public function run(array $params): void file_put_contents($file, $contents); CLI::write(CLI::color(' Published! ', 'green') . "You can customize the configuration by editing the \"{$file}\" file."); } - } } From 68a35fa8078c3b69f03897b15026f87f2dfa5035 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Dadashi Date: Sat, 20 May 2023 23:27:51 +0330 Subject: [PATCH 5/5] tests: fixed random error failure in phpunit --- tests/Commands/SettingsPublisherTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Commands/SettingsPublisherTest.php b/tests/Commands/SettingsPublisherTest.php index 2bea791..2242e82 100644 --- a/tests/Commands/SettingsPublisherTest.php +++ b/tests/Commands/SettingsPublisherTest.php @@ -49,6 +49,9 @@ public function testPublishConfigFile(): void } } + /** + * @depends testPublishConfigFile + */ public function testPublishConfigFileWithForce(): void {