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/Commands/SettingsPublisher.php b/src/Commands/SettingsPublisher.php new file mode 100644 index 0000000..c48d889 --- /dev/null +++ b/src/Commands/SettingsPublisher.php @@ -0,0 +1,91 @@ + + */ + 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."); + } + } +} diff --git a/tests/Commands/SettingsPublisherTest.php b/tests/Commands/SettingsPublisherTest.php new file mode 100644 index 0000000..2242e82 --- /dev/null +++ b/tests/Commands/SettingsPublisherTest.php @@ -0,0 +1,83 @@ +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'); + } + } + + /** + * @depends testPublishConfigFile + */ + 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); + } +}