Skip to content

Commit 081bb2c

Browse files
committed
add publish command
1 parent 68af8af commit 081bb2c

3 files changed

Lines changed: 129 additions & 1 deletion

File tree

src/Commands/PublishSettings.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter Queue.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Settings\Commands;
15+
16+
use CodeIgniter\CLI\BaseCommand;
17+
use CodeIgniter\CLI\CLI;
18+
use CodeIgniter\Publisher\Publisher;
19+
use Throwable;
20+
21+
class PublishSettings extends BaseCommand
22+
{
23+
protected $group = 'Settings';
24+
protected $name = 'settings:publish';
25+
protected $description = 'Publish Settings config file into the current application.';
26+
27+
public function run(array $params): void
28+
{
29+
$source = service('autoloader')->getNamespace('CodeIgniter\\Settings')[0];
30+
31+
$publisher = new Publisher($source, APPPATH);
32+
33+
try {
34+
$publisher->addPaths([
35+
'Config/Settings.php',
36+
])->merge(false);
37+
} catch (Throwable $e) {
38+
$this->showError($e);
39+
40+
return;
41+
}
42+
43+
foreach ($publisher->getPublished() as $file) {
44+
$contents = file_get_contents($file);
45+
$contents = str_replace('namespace CodeIgniter\\Settings\\Config', 'namespace Config', $contents);
46+
$contents = str_replace('use CodeIgniter\\Config\\BaseConfig', 'use CodeIgniter\\Settings\\Config\\Settings as BaseSettings', $contents);
47+
$contents = str_replace('class Settings extends BaseConfig', 'class Settings extends BaseSettings', $contents);
48+
file_put_contents($file, $contents);
49+
}
50+
51+
CLI::write(CLI::color(' Published! ', 'green') . 'You can customize the configuration by editing the "app/Config/Settings.php" file.');
52+
}
53+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter Queue.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Commands;
15+
16+
use CodeIgniter\Test\Filters\CITestStreamFilter;
17+
use Tests\Support\TestCase;
18+
19+
/**
20+
* @internal
21+
*/
22+
final class PublishSettingsTest extends TestCase
23+
{
24+
public function testRun(): void
25+
{
26+
CITestStreamFilter::registration();
27+
CITestStreamFilter::addOutputFilter();
28+
29+
$this->assertNotFalse(command('settings:publish'));
30+
$output = $this->parseOutput(CITestStreamFilter::$buffer);
31+
32+
CITestStreamFilter::removeOutputFilter();
33+
34+
$this->assertSame(' Published! You can customize the configuration by editing the "app/Config/Settings.php" file.', $output);
35+
}
36+
}

tests/_support/TestCase.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
namespace Tests\Support;
44

5-
use CodeIgniter\Settings\Handlers\ArrayHandler;
5+
use CodeIgniter\CLI\CLI;
66
use CodeIgniter\Settings\Settings;
77
use CodeIgniter\Test\CIUnitTestCase;
8+
use CodeIgniter\Test\ReflectionHelper;
89
use Config\Services;
910

1011
abstract class TestCase extends CIUnitTestCase
1112
{
13+
use ReflectionHelper;
14+
15+
private array $lines = [];
16+
1217
/**
1318
* @var Settings
1419
*/
@@ -34,4 +39,38 @@ protected function tearDown(): void
3439

3540
$this->resetServices();
3641
}
42+
43+
protected function parseOutput(string $output): string
44+
{
45+
$this->lines = [];
46+
$output = $this->removeColorCodes($output);
47+
$this->lines = explode("\n", $output);
48+
49+
return $output;
50+
}
51+
52+
protected function getLine(int $line = 0): ?string
53+
{
54+
return $this->lines[$line] ?? null;
55+
}
56+
57+
protected function getLines(): string
58+
{
59+
return implode('', $this->lines);
60+
}
61+
62+
protected function removeColorCodes(string $output): string
63+
{
64+
$colors = $this->getPrivateProperty(CLI::class, 'foreground_colors');
65+
$colors = array_values(array_map(static fn ($color) => "\033[" . $color . 'm', $colors));
66+
$colors = array_merge(["\033[0m"], $colors);
67+
68+
$output = str_replace($colors, '', trim($output));
69+
70+
if (is_windows()) {
71+
$output = str_replace("\r\n", "\n", $output);
72+
}
73+
74+
return $output;
75+
}
3776
}

0 commit comments

Comments
 (0)