-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add command settings:publish
#80
base: develop
Are you sure you want to change the base?
Changes from all commits
c3e4aad
e9a4a8f
0b113d9
0070db9
68a35fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,91 @@ | ||||||||||||
<?php | ||||||||||||
|
||||||||||||
namespace CodeIgniter\Settings\Commands; | ||||||||||||
|
||||||||||||
use CodeIgniter\CLI\BaseCommand; | ||||||||||||
use CodeIgniter\CLI\CLI; | ||||||||||||
use CodeIgniter\Publisher\Publisher; | ||||||||||||
use Throwable; | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Publish Settings config file into the current application. | ||||||||||||
*/ | ||||||||||||
class SettingsPublisher extends BaseCommand | ||||||||||||
{ | ||||||||||||
/** | ||||||||||||
* The group the command is lumped under | ||||||||||||
* when listing commands. | ||||||||||||
* | ||||||||||||
* @var string | ||||||||||||
*/ | ||||||||||||
protected $group = 'Settings'; | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* The Command's name | ||||||||||||
* | ||||||||||||
* @var string | ||||||||||||
*/ | ||||||||||||
protected $name = 'settings:publish'; | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* the Command's usage | ||||||||||||
* | ||||||||||||
* @var string | ||||||||||||
*/ | ||||||||||||
protected $usage = <<<'EOL' | ||||||||||||
settings:publish [options] | ||||||||||||
|
||||||||||||
Examples: | ||||||||||||
settings:publish -f | ||||||||||||
EOL; | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* the Command's short description | ||||||||||||
* | ||||||||||||
* @var string | ||||||||||||
*/ | ||||||||||||
protected $description = 'Publish Settings config file into the current application.'; | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* The Command's options | ||||||||||||
* | ||||||||||||
* @var array<string, string> | ||||||||||||
*/ | ||||||||||||
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); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If
Comment on lines
+74
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you align
Suggested change
|
||||||||||||
} 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."); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Commands; | ||
|
||
use CodeIgniter\Test\CIUnitTestCase; | ||
use CodeIgniter\Test\Filters\CITestStreamFilter; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class SettingsPublisherTest extends CIUnitTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
CITestStreamFilter::registration(); | ||
CITestStreamFilter::addOutputFilter(); | ||
CITestStreamFilter::addErrorFilter(); | ||
|
||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
|
||
CITestStreamFilter::removeOutputFilter(); | ||
CITestStreamFilter::removeErrorFilter(); | ||
|
||
} | ||
|
||
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'); | ||
} | ||
Comment on lines
+46
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need this? Since there's no unlinking in the tearDown, this operation would unnecessarily pollute the next test and the filesystem. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @paulbalandan In fact, /**
* @depends testPublishConfigFile
*/
public function testPublishConfigFileWithForce(): void
{
// ...
} phpunit v10.1: #[Depends('testPublishConfigFile')]
public function testPublishConfigFileWithForce(): void
{
// ...
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think instead of depending on the other test, in the 2nd test you should run the command twice. First without the |
||
} | ||
|
||
/** | ||
* @depends testPublishConfigFile | ||
*/ | ||
public function testPublishConfigFileWithForce(): void | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove extra whitespaces after |
||
|
||
$filepath = APPPATH . 'Config/Settings.php'; | ||
|
||
helper('filesystem'); | ||
write_file($filepath, 'fake text.'); | ||
$contents = $this->getFileContents($filepath); | ||
|
||
$this->assertFileExists($filepath); | ||
$this->assertStringContainsString('fake text.', $contents); | ||
|
||
Comment on lines
+58
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part basically tests |
||
command('settings:publish -f'); | ||
|
||
$expectedConfigFile = APPPATH . 'Config/Settings.php.bak'; | ||
$this->assertFileEquals($expectedConfigFile, $filepath); | ||
|
||
clearstatcache(true, $expectedConfigFile); | ||
if (is_file($expectedConfigFile)) { | ||
unlink($expectedConfigFile); | ||
} | ||
Comment on lines
+72
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cleanup should be done in teardown. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the cleanup is only for the test method, I think it is okay to put it in the test method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The advantage of |
||
|
||
} | ||
|
||
private function getFileContents(string $filepath): string | ||
{ | ||
return (string) @file_get_contents($filepath); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was changed here? Tabs to spaces?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@datamweb Please keep this file as close to the DevKit template as possible so updates are easier.