Initial
This commit is contained in:
87
system/Commands/Cache/ClearCache.php
Normal file
87
system/Commands/Cache/ClearCache.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Cache;
|
||||
|
||||
use CodeIgniter\Cache\CacheFactory;
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
|
||||
/**
|
||||
* Clears current cache.
|
||||
*/
|
||||
class ClearCache extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* Command grouping.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Cache';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'cache:clear';
|
||||
|
||||
/**
|
||||
* the Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Clears the current system caches.';
|
||||
|
||||
/**
|
||||
* the Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'cache:clear [driver]';
|
||||
|
||||
/**
|
||||
* the Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [
|
||||
'driver' => 'The cache driver to use',
|
||||
];
|
||||
|
||||
/**
|
||||
* Clears the cache
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$config = config('Cache');
|
||||
$handler = $params[0] ?? $config->handler;
|
||||
|
||||
if (! array_key_exists($handler, $config->validHandlers)) {
|
||||
CLI::error($handler . ' is not a valid cache handler.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$config->handler = $handler;
|
||||
$cache = CacheFactory::getHandler($config);
|
||||
|
||||
if (! $cache->clean()) {
|
||||
// @codeCoverageIgnoreStart
|
||||
CLI::error('Error while clearing the cache.');
|
||||
|
||||
return;
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
CLI::write(CLI::color('Cache cleared.', 'green'));
|
||||
}
|
||||
}
|
||||
88
system/Commands/Cache/InfoCache.php
Normal file
88
system/Commands/Cache/InfoCache.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Cache;
|
||||
|
||||
use CodeIgniter\Cache\CacheFactory;
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use CodeIgniter\I18n\Time;
|
||||
|
||||
/**
|
||||
* Shows information on the cache.
|
||||
*/
|
||||
class InfoCache extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* Command grouping.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Cache';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'cache:info';
|
||||
|
||||
/**
|
||||
* the Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Shows file cache information in the current system.';
|
||||
|
||||
/**
|
||||
* the Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'cache:info';
|
||||
|
||||
/**
|
||||
* Clears the cache
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$config = config('Cache');
|
||||
helper('number');
|
||||
|
||||
if ($config->handler !== 'file') {
|
||||
CLI::error('This command only supports the file cache handler.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$cache = CacheFactory::getHandler($config);
|
||||
$caches = $cache->getCacheInfo();
|
||||
$tbody = [];
|
||||
|
||||
foreach ($caches as $key => $field) {
|
||||
$tbody[] = [
|
||||
$key,
|
||||
clean_path($field['server_path']),
|
||||
number_to_size($field['size']),
|
||||
Time::createFromTimestamp($field['date']),
|
||||
];
|
||||
}
|
||||
|
||||
$thead = [
|
||||
CLI::color('Name', 'green'),
|
||||
CLI::color('Server Path', 'green'),
|
||||
CLI::color('Size', 'green'),
|
||||
CLI::color('Date', 'green'),
|
||||
];
|
||||
|
||||
CLI::table($tbody, $thead);
|
||||
}
|
||||
}
|
||||
155
system/Commands/Database/CreateDatabase.php
Normal file
155
system/Commands/Database/CreateDatabase.php
Normal file
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Database;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use CodeIgniter\Config\Factories;
|
||||
use CodeIgniter\Database\SQLite3\Connection;
|
||||
use Config\Database;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Creates a new database.
|
||||
*/
|
||||
class CreateDatabase extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Database';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'db:create';
|
||||
|
||||
/**
|
||||
* the Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Create a new database schema.';
|
||||
|
||||
/**
|
||||
* the Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'db:create <db_name> [options]';
|
||||
|
||||
/**
|
||||
* The Command's arguments
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
protected $arguments = [
|
||||
'db_name' => 'The database name to use',
|
||||
];
|
||||
|
||||
/**
|
||||
* The Command's options
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
protected $options = [
|
||||
'--ext' => 'File extension of the database file for SQLite3. Can be `db` or `sqlite`. Defaults to `db`.',
|
||||
];
|
||||
|
||||
/**
|
||||
* Creates a new database.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$name = array_shift($params);
|
||||
|
||||
if (empty($name)) {
|
||||
$name = CLI::prompt('Database name', null, 'required'); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
try {
|
||||
/**
|
||||
* @var Database $config
|
||||
*/
|
||||
$config = config('Database');
|
||||
|
||||
// Set to an empty database to prevent connection errors.
|
||||
$group = ENVIRONMENT === 'testing' ? 'tests' : $config->defaultGroup;
|
||||
|
||||
$config->{$group}['database'] = '';
|
||||
|
||||
$db = Database::connect();
|
||||
|
||||
// Special SQLite3 handling
|
||||
if ($db instanceof Connection) {
|
||||
$ext = $params['ext'] ?? CLI::getOption('ext') ?? 'db';
|
||||
|
||||
if (! in_array($ext, ['db', 'sqlite'], true)) {
|
||||
$ext = CLI::prompt('Please choose a valid file extension', ['db', 'sqlite']); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
if ($name !== ':memory:') {
|
||||
$name = str_replace(['.db', '.sqlite'], '', $name) . ".{$ext}";
|
||||
}
|
||||
|
||||
$config->{$group}['DBDriver'] = 'SQLite3';
|
||||
$config->{$group}['database'] = $name;
|
||||
|
||||
if ($name !== ':memory:') {
|
||||
$dbName = strpos($name, DIRECTORY_SEPARATOR) === false ? WRITEPATH . $name : $name;
|
||||
|
||||
if (is_file($dbName)) {
|
||||
CLI::error("Database \"{$dbName}\" already exists.", 'light_gray', 'red');
|
||||
CLI::newLine();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
unset($dbName);
|
||||
}
|
||||
|
||||
// Connect to new SQLite3 to create new database
|
||||
$db = Database::connect(null, false);
|
||||
$db->connect();
|
||||
|
||||
if (! is_file($db->getDatabase()) && $name !== ':memory:') {
|
||||
// @codeCoverageIgnoreStart
|
||||
CLI::error('Database creation failed.', 'light_gray', 'red');
|
||||
CLI::newLine();
|
||||
|
||||
return;
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
} elseif (! Database::forge()->createDatabase($name)) {
|
||||
// @codeCoverageIgnoreStart
|
||||
CLI::error('Database creation failed.', 'light_gray', 'red');
|
||||
CLI::newLine();
|
||||
|
||||
return;
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
CLI::write("Database \"{$name}\" successfully created.", 'green');
|
||||
CLI::newLine();
|
||||
} catch (Throwable $e) {
|
||||
$this->showError($e);
|
||||
} finally {
|
||||
// Reset the altered config no matter what happens.
|
||||
Factories::reset('config');
|
||||
}
|
||||
}
|
||||
}
|
||||
102
system/Commands/Database/Migrate.php
Normal file
102
system/Commands/Database/Migrate.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Database;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use Config\Services;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Runs all new migrations.
|
||||
*/
|
||||
class Migrate extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Database';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'migrate';
|
||||
|
||||
/**
|
||||
* the Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Locates and runs all new migrations against the database.';
|
||||
|
||||
/**
|
||||
* the Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'migrate [options]';
|
||||
|
||||
/**
|
||||
* the Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'-n' => 'Set migration namespace',
|
||||
'-g' => 'Set database group',
|
||||
'--all' => 'Set for all namespaces, will ignore (-n) option',
|
||||
];
|
||||
|
||||
/**
|
||||
* Ensures that all migrations have been run.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$runner = Services::migrations();
|
||||
$runner->clearCliMessages();
|
||||
|
||||
CLI::write(lang('Migrations.latest'), 'yellow');
|
||||
|
||||
$namespace = $params['n'] ?? CLI::getOption('n');
|
||||
$group = $params['g'] ?? CLI::getOption('g');
|
||||
|
||||
try {
|
||||
if (array_key_exists('all', $params) || CLI::getOption('all')) {
|
||||
$runner->setNamespace(null);
|
||||
} elseif ($namespace) {
|
||||
$runner->setNamespace($namespace);
|
||||
}
|
||||
|
||||
if (! $runner->latest($group)) {
|
||||
CLI::error(lang('Migrations.generalFault'), 'light_gray', 'red'); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
$messages = $runner->getCliMessages();
|
||||
|
||||
foreach ($messages as $message) {
|
||||
CLI::write($message);
|
||||
}
|
||||
|
||||
CLI::write('Done migrations.', 'green');
|
||||
|
||||
// @codeCoverageIgnoreStart
|
||||
} catch (Throwable $e) {
|
||||
$this->showError($e);
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
}
|
||||
}
|
||||
87
system/Commands/Database/MigrateRefresh.php
Normal file
87
system/Commands/Database/MigrateRefresh.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Database;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
|
||||
/**
|
||||
* Does a rollback followed by a latest to refresh the current state
|
||||
* of the database.
|
||||
*/
|
||||
class MigrateRefresh extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Database';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'migrate:refresh';
|
||||
|
||||
/**
|
||||
* the Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Does a rollback followed by a latest to refresh the current state of the database.';
|
||||
|
||||
/**
|
||||
* the Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'migrate:refresh [options]';
|
||||
|
||||
/**
|
||||
* the Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'-n' => 'Set migration namespace',
|
||||
'-g' => 'Set database group',
|
||||
'--all' => 'Set latest for all namespace, will ignore (-n) option',
|
||||
'-f' => 'Force command - this option allows you to bypass the confirmation question when running this command in a production environment',
|
||||
];
|
||||
|
||||
/**
|
||||
* Does a rollback followed by a latest to refresh the current state
|
||||
* of the database.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$params['b'] = 0;
|
||||
|
||||
if (ENVIRONMENT === 'production') {
|
||||
// @codeCoverageIgnoreStart
|
||||
$force = array_key_exists('f', $params) || CLI::getOption('f');
|
||||
|
||||
if (! $force && CLI::prompt(lang('Migrations.refreshConfirm'), ['y', 'n']) === 'n') {
|
||||
return;
|
||||
}
|
||||
|
||||
$params['f'] = null;
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
$this->call('migrate:rollback', $params);
|
||||
$this->call('migrate', $params);
|
||||
}
|
||||
}
|
||||
110
system/Commands/Database/MigrateRollback.php
Normal file
110
system/Commands/Database/MigrateRollback.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Database;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use Config\Services;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Runs all of the migrations in reverse order, until they have
|
||||
* all been unapplied.
|
||||
*/
|
||||
class MigrateRollback extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Database';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'migrate:rollback';
|
||||
|
||||
/**
|
||||
* the Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Runs the "down" method for all migrations in the last batch.';
|
||||
|
||||
/**
|
||||
* the Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'migrate:rollback [options]';
|
||||
|
||||
/**
|
||||
* the Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'-b' => 'Specify a batch to roll back to; e.g. "3" to return to batch #3 or "-2" to roll back twice',
|
||||
'-g' => 'Set database group',
|
||||
'-f' => 'Force command - this option allows you to bypass the confirmation question when running this command in a production environment',
|
||||
];
|
||||
|
||||
/**
|
||||
* Runs all of the migrations in reverse order, until they have
|
||||
* all been unapplied.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
if (ENVIRONMENT === 'production') {
|
||||
// @codeCoverageIgnoreStart
|
||||
$force = array_key_exists('f', $params) || CLI::getOption('f');
|
||||
|
||||
if (! $force && CLI::prompt(lang('Migrations.rollBackConfirm'), ['y', 'n']) === 'n') {
|
||||
return;
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
$runner = Services::migrations();
|
||||
$group = $params['g'] ?? CLI::getOption('g');
|
||||
|
||||
if (is_string($group)) {
|
||||
$runner->setGroup($group);
|
||||
}
|
||||
|
||||
try {
|
||||
$batch = $params['b'] ?? CLI::getOption('b') ?? $runner->getLastBatch() - 1;
|
||||
CLI::write(lang('Migrations.rollingBack') . ' ' . $batch, 'yellow');
|
||||
|
||||
if (! $runner->regress($batch)) {
|
||||
CLI::error(lang('Migrations.generalFault'), 'light_gray', 'red'); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
$messages = $runner->getCliMessages();
|
||||
|
||||
foreach ($messages as $message) {
|
||||
CLI::write($message);
|
||||
}
|
||||
|
||||
CLI::write('Done rolling back migrations.', 'green');
|
||||
|
||||
// @codeCoverageIgnoreStart
|
||||
} catch (Throwable $e) {
|
||||
$this->showError($e);
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
}
|
||||
}
|
||||
164
system/Commands/Database/MigrateStatus.php
Normal file
164
system/Commands/Database/MigrateStatus.php
Normal file
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Database;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use Config\Services;
|
||||
|
||||
/**
|
||||
* Displays a list of all migrations and whether they've been run or not.
|
||||
*/
|
||||
class MigrateStatus extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Database';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'migrate:status';
|
||||
|
||||
/**
|
||||
* the Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Displays a list of all migrations and whether they\'ve been run or not.';
|
||||
|
||||
/**
|
||||
* the Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'migrate:status [options]';
|
||||
|
||||
/**
|
||||
* the Command's Options
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
protected $options = [
|
||||
'-g' => 'Set database group',
|
||||
];
|
||||
|
||||
/**
|
||||
* Namespaces to ignore when looking for migrations.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $ignoredNamespaces = [
|
||||
'CodeIgniter',
|
||||
'Config',
|
||||
'Kint',
|
||||
'Laminas\ZendFrameworkBridge',
|
||||
'Laminas\Escaper',
|
||||
'Psr\Log',
|
||||
];
|
||||
|
||||
/**
|
||||
* Displays a list of all migrations and whether they've been run or not.
|
||||
*
|
||||
* @param array<string, mixed> $params
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$runner = Services::migrations();
|
||||
$group = $params['g'] ?? CLI::getOption('g');
|
||||
|
||||
// Get all namespaces
|
||||
$namespaces = Services::autoloader()->getNamespace();
|
||||
|
||||
// Collection of migration status
|
||||
$status = [];
|
||||
|
||||
foreach (array_keys($namespaces) as $namespace) {
|
||||
if (ENVIRONMENT !== 'testing') {
|
||||
// Make Tests\\Support discoverable for testing
|
||||
$this->ignoredNamespaces[] = 'Tests\Support'; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
if (in_array($namespace, $this->ignoredNamespaces, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (APP_NAMESPACE !== 'App' && $namespace === 'App') {
|
||||
continue; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
$migrations = $runner->findNamespaceMigrations($namespace);
|
||||
|
||||
if (empty($migrations)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$history = $runner->getHistory((string) $group);
|
||||
ksort($migrations);
|
||||
|
||||
foreach ($migrations as $uid => $migration) {
|
||||
$migrations[$uid]->name = mb_substr($migration->name, mb_strpos($migration->name, $uid . '_'));
|
||||
|
||||
$date = '---';
|
||||
$group = '---';
|
||||
$batch = '---';
|
||||
|
||||
foreach ($history as $row) {
|
||||
// @codeCoverageIgnoreStart
|
||||
if ($runner->getObjectUid($row) !== $migration->uid) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$date = date('Y-m-d H:i:s', $row->time);
|
||||
$group = $row->group;
|
||||
$batch = $row->batch;
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
$status[] = [
|
||||
$namespace,
|
||||
$migration->version,
|
||||
$migration->name,
|
||||
$group,
|
||||
$date,
|
||||
$batch,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if (! $status) {
|
||||
// @codeCoverageIgnoreStart
|
||||
CLI::error(lang('Migrations.noneFound'), 'light_gray', 'red');
|
||||
CLI::newLine();
|
||||
|
||||
return;
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
$headers = [
|
||||
CLI::color(lang('Migrations.namespace'), 'yellow'),
|
||||
CLI::color(lang('Migrations.version'), 'yellow'),
|
||||
CLI::color(lang('Migrations.filename'), 'yellow'),
|
||||
CLI::color(lang('Migrations.group'), 'yellow'),
|
||||
CLI::color(str_replace(': ', '', lang('Migrations.on')), 'yellow'),
|
||||
CLI::color(lang('Migrations.batch'), 'yellow'),
|
||||
];
|
||||
|
||||
CLI::table($status, $headers);
|
||||
}
|
||||
}
|
||||
82
system/Commands/Database/Seed.php
Normal file
82
system/Commands/Database/Seed.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Database;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use CodeIgniter\Database\Seeder;
|
||||
use Config\Database;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Runs the specified Seeder file to populate the database
|
||||
* with some data.
|
||||
*/
|
||||
class Seed extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Database';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'db:seed';
|
||||
|
||||
/**
|
||||
* the Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Runs the specified seeder to populate known data into the database.';
|
||||
|
||||
/**
|
||||
* the Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'db:seed <seeder_name>';
|
||||
|
||||
/**
|
||||
* the Command's Arguments
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
protected $arguments = [
|
||||
'seeder_name' => 'The seeder name to run',
|
||||
];
|
||||
|
||||
/**
|
||||
* Passes to Seeder to populate the database.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$seeder = new Seeder(new Database());
|
||||
$seedName = array_shift($params);
|
||||
|
||||
if (empty($seedName)) {
|
||||
$seedName = CLI::prompt(lang('Migrations.migSeeder'), null, 'required'); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
try {
|
||||
$seeder->call($seedName);
|
||||
} catch (Throwable $e) {
|
||||
$this->showError($e);
|
||||
}
|
||||
}
|
||||
}
|
||||
188
system/Commands/Encryption/GenerateKey.php
Normal file
188
system/Commands/Encryption/GenerateKey.php
Normal file
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Encryption;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use CodeIgniter\Config\DotEnv;
|
||||
use CodeIgniter\Encryption\Encryption;
|
||||
|
||||
/**
|
||||
* Generates a new encryption key.
|
||||
*/
|
||||
class GenerateKey extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* The Command's group.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Encryption';
|
||||
|
||||
/**
|
||||
* The Command's name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'key:generate';
|
||||
|
||||
/**
|
||||
* The Command's usage.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'key:generate [options]';
|
||||
|
||||
/**
|
||||
* The Command's short description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Generates a new encryption key and writes it in an `.env` file.';
|
||||
|
||||
/**
|
||||
* The command's options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'--force' => 'Force overwrite existing key in `.env` file.',
|
||||
'--length' => 'The length of the random string that should be returned in bytes. Defaults to 32.',
|
||||
'--prefix' => 'Prefix to prepend to encoded key (either hex2bin or base64). Defaults to hex2bin.',
|
||||
'--show' => 'Shows the generated key in the terminal instead of storing in the `.env` file.',
|
||||
];
|
||||
|
||||
/**
|
||||
* Actually execute the command.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$prefix = $params['prefix'] ?? CLI::getOption('prefix');
|
||||
if (in_array($prefix, [null, true], true)) {
|
||||
$prefix = 'hex2bin';
|
||||
} elseif (! in_array($prefix, ['hex2bin', 'base64'], true)) {
|
||||
$prefix = CLI::prompt('Please provide a valid prefix to use.', ['hex2bin', 'base64'], 'required'); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
$length = $params['length'] ?? CLI::getOption('length');
|
||||
if (in_array($length, [null, true], true)) {
|
||||
$length = 32;
|
||||
}
|
||||
|
||||
$encodedKey = $this->generateRandomKey($prefix, $length);
|
||||
|
||||
if (array_key_exists('show', $params) || (bool) CLI::getOption('show')) {
|
||||
CLI::write($encodedKey, 'yellow');
|
||||
CLI::newLine();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $this->setNewEncryptionKey($encodedKey, $params)) {
|
||||
CLI::write('Error in setting new encryption key to .env file.', 'light_gray', 'red');
|
||||
CLI::newLine();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// force DotEnv to reload the new env vars
|
||||
putenv('encryption.key');
|
||||
unset($_ENV['encryption.key'], $_SERVER['encryption.key']);
|
||||
$dotenv = new DotEnv(ROOTPATH);
|
||||
$dotenv->load();
|
||||
|
||||
CLI::write('Application\'s new encryption key was successfully set.', 'green');
|
||||
CLI::newLine();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a key and encodes it.
|
||||
*/
|
||||
protected function generateRandomKey(string $prefix, int $length): string
|
||||
{
|
||||
$key = Encryption::createKey($length);
|
||||
|
||||
if ($prefix === 'hex2bin') {
|
||||
return 'hex2bin:' . bin2hex($key);
|
||||
}
|
||||
|
||||
return 'base64:' . base64_encode($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the new encryption key in your .env file.
|
||||
*/
|
||||
protected function setNewEncryptionKey(string $key, array $params): bool
|
||||
{
|
||||
$currentKey = env('encryption.key', '');
|
||||
|
||||
if ($currentKey !== '' && ! $this->confirmOverwrite($params)) {
|
||||
// Not yet testable since it requires keyboard input
|
||||
// @codeCoverageIgnoreStart
|
||||
return false;
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
return $this->writeNewEncryptionKeyToFile($currentKey, $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether to overwrite existing encryption key.
|
||||
*/
|
||||
protected function confirmOverwrite(array $params): bool
|
||||
{
|
||||
return (array_key_exists('force', $params) || CLI::getOption('force')) || CLI::prompt('Overwrite existing key?', ['n', 'y']) === 'y';
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the new encryption key to .env file.
|
||||
*/
|
||||
protected function writeNewEncryptionKeyToFile(string $oldKey, string $newKey): bool
|
||||
{
|
||||
$baseEnv = ROOTPATH . 'env';
|
||||
$envFile = ROOTPATH . '.env';
|
||||
|
||||
if (! file_exists($envFile)) {
|
||||
if (! file_exists($baseEnv)) {
|
||||
CLI::write('Both default shipped `env` file and custom `.env` are missing.', 'yellow');
|
||||
CLI::write('Here\'s your new key instead: ' . CLI::color($newKey, 'yellow'));
|
||||
CLI::newLine();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
copy($baseEnv, $envFile);
|
||||
}
|
||||
|
||||
$ret = file_put_contents($envFile, preg_replace(
|
||||
$this->keyPattern($oldKey),
|
||||
"\nencryption.key = {$newKey}",
|
||||
file_get_contents($envFile)
|
||||
));
|
||||
|
||||
return $ret !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the regex of the current encryption key.
|
||||
*/
|
||||
protected function keyPattern(string $oldKey): string
|
||||
{
|
||||
$escaped = preg_quote($oldKey, '/');
|
||||
|
||||
if ($escaped !== '') {
|
||||
$escaped = "[{$escaped}]*";
|
||||
}
|
||||
|
||||
return "/^[#\\s]*encryption.key[=\\s]*{$escaped}$/m";
|
||||
}
|
||||
}
|
||||
119
system/Commands/Generators/CommandGenerator.php
Normal file
119
system/Commands/Generators/CommandGenerator.php
Normal file
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Generators;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use CodeIgniter\CLI\GeneratorTrait;
|
||||
|
||||
/**
|
||||
* Generates a skeleton command file.
|
||||
*/
|
||||
class CommandGenerator extends BaseCommand
|
||||
{
|
||||
use GeneratorTrait;
|
||||
|
||||
/**
|
||||
* The Command's Group
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Generators';
|
||||
|
||||
/**
|
||||
* The Command's Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'make:command';
|
||||
|
||||
/**
|
||||
* The Command's Description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Generates a new spark command.';
|
||||
|
||||
/**
|
||||
* The Command's Usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'make:command <name> [options]';
|
||||
|
||||
/**
|
||||
* The Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [
|
||||
'name' => 'The command class name.',
|
||||
];
|
||||
|
||||
/**
|
||||
* The Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'--command' => 'The command name. Default: "command:name"',
|
||||
'--type' => 'The command type. Options [basic, generator]. Default: "basic".',
|
||||
'--group' => 'The command group. Default: [basic -> "CodeIgniter", generator -> "Generators"].',
|
||||
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
|
||||
'--suffix' => 'Append the component title to the class name (e.g. User => UserCommand).',
|
||||
'--force' => 'Force overwrite existing file.',
|
||||
];
|
||||
|
||||
/**
|
||||
* Actually execute a command.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$this->component = 'Command';
|
||||
$this->directory = 'Commands';
|
||||
$this->template = 'command.tpl.php';
|
||||
|
||||
$this->classNameLang = 'CLI.generator.className.command';
|
||||
$this->execute($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare options and do the necessary replacements.
|
||||
*/
|
||||
protected function prepare(string $class): string
|
||||
{
|
||||
$command = $this->getOption('command');
|
||||
$group = $this->getOption('group');
|
||||
$type = $this->getOption('type');
|
||||
|
||||
$command = is_string($command) ? $command : 'command:name';
|
||||
$type = is_string($type) ? $type : 'basic';
|
||||
|
||||
if (! in_array($type, ['basic', 'generator'], true)) {
|
||||
// @codeCoverageIgnoreStart
|
||||
$type = CLI::prompt(lang('CLI.generator.commandType'), ['basic', 'generator'], 'required');
|
||||
CLI::newLine();
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
if (! is_string($group)) {
|
||||
$group = $type === 'generator' ? 'Generators' : 'CodeIgniter';
|
||||
}
|
||||
|
||||
return $this->parseTemplate(
|
||||
$class,
|
||||
['{group}', '{command}'],
|
||||
[$group, $command],
|
||||
['type' => $type]
|
||||
);
|
||||
}
|
||||
}
|
||||
98
system/Commands/Generators/ConfigGenerator.php
Normal file
98
system/Commands/Generators/ConfigGenerator.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Generators;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\GeneratorTrait;
|
||||
|
||||
/**
|
||||
* Generates a skeleton config file.
|
||||
*/
|
||||
class ConfigGenerator extends BaseCommand
|
||||
{
|
||||
use GeneratorTrait;
|
||||
|
||||
/**
|
||||
* The Command's Group
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Generators';
|
||||
|
||||
/**
|
||||
* The Command's Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'make:config';
|
||||
|
||||
/**
|
||||
* The Command's Description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Generates a new config file.';
|
||||
|
||||
/**
|
||||
* The Command's Usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'make:config <name> [options]';
|
||||
|
||||
/**
|
||||
* The Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [
|
||||
'name' => 'The config class name.',
|
||||
];
|
||||
|
||||
/**
|
||||
* The Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
|
||||
'--suffix' => 'Append the component title to the class name (e.g. User => UserConfig).',
|
||||
'--force' => 'Force overwrite existing file.',
|
||||
];
|
||||
|
||||
/**
|
||||
* Actually execute a command.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$this->component = 'Config';
|
||||
$this->directory = 'Config';
|
||||
$this->template = 'config.tpl.php';
|
||||
|
||||
$this->classNameLang = 'CLI.generator.className.config';
|
||||
$this->execute($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare options and do the necessary replacements.
|
||||
*/
|
||||
protected function prepare(string $class): string
|
||||
{
|
||||
$namespace = $this->getOption('namespace') ?? APP_NAMESPACE;
|
||||
|
||||
if ($namespace === APP_NAMESPACE) {
|
||||
$class = substr($class, strlen($namespace . '\\'));
|
||||
}
|
||||
|
||||
return $this->parseTemplate($class);
|
||||
}
|
||||
}
|
||||
131
system/Commands/Generators/ControllerGenerator.php
Normal file
131
system/Commands/Generators/ControllerGenerator.php
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Generators;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use CodeIgniter\CLI\GeneratorTrait;
|
||||
|
||||
/**
|
||||
* Generates a skeleton controller file.
|
||||
*/
|
||||
class ControllerGenerator extends BaseCommand
|
||||
{
|
||||
use GeneratorTrait;
|
||||
|
||||
/**
|
||||
* The Command's Group
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Generators';
|
||||
|
||||
/**
|
||||
* The Command's Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'make:controller';
|
||||
|
||||
/**
|
||||
* The Command's Description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Generates a new controller file.';
|
||||
|
||||
/**
|
||||
* The Command's Usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'make:controller <name> [options]';
|
||||
|
||||
/**
|
||||
* The Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [
|
||||
'name' => 'The controller class name.',
|
||||
];
|
||||
|
||||
/**
|
||||
* The Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'--bare' => 'Extends from CodeIgniter\Controller instead of BaseController.',
|
||||
'--restful' => 'Extends from a RESTful resource, Options: [controller, presenter]. Default: "controller".',
|
||||
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
|
||||
'--suffix' => 'Append the component title to the class name (e.g. User => UserController).',
|
||||
'--force' => 'Force overwrite existing file.',
|
||||
];
|
||||
|
||||
/**
|
||||
* Actually execute a command.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$this->component = 'Controller';
|
||||
$this->directory = 'Controllers';
|
||||
$this->template = 'controller.tpl.php';
|
||||
|
||||
$this->classNameLang = 'CLI.generator.className.controller';
|
||||
$this->execute($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare options and do the necessary replacements.
|
||||
*/
|
||||
protected function prepare(string $class): string
|
||||
{
|
||||
$bare = $this->getOption('bare');
|
||||
$rest = $this->getOption('restful');
|
||||
|
||||
$useStatement = trim(APP_NAMESPACE, '\\') . '\Controllers\BaseController';
|
||||
$extends = 'BaseController';
|
||||
|
||||
// Gets the appropriate parent class to extend.
|
||||
if ($bare || $rest) {
|
||||
if ($bare) {
|
||||
$useStatement = 'CodeIgniter\Controller';
|
||||
$extends = 'Controller';
|
||||
} elseif ($rest) {
|
||||
$rest = is_string($rest) ? $rest : 'controller';
|
||||
|
||||
if (! in_array($rest, ['controller', 'presenter'], true)) {
|
||||
// @codeCoverageIgnoreStart
|
||||
$rest = CLI::prompt(lang('CLI.generator.parentClass'), ['controller', 'presenter'], 'required');
|
||||
CLI::newLine();
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
if ($rest === 'controller') {
|
||||
$useStatement = 'CodeIgniter\RESTful\ResourceController';
|
||||
$extends = 'ResourceController';
|
||||
} elseif ($rest === 'presenter') {
|
||||
$useStatement = 'CodeIgniter\RESTful\ResourcePresenter';
|
||||
$extends = 'ResourcePresenter';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->parseTemplate(
|
||||
$class,
|
||||
['{useStatement}', '{extends}'],
|
||||
[$useStatement, $extends],
|
||||
['type' => $rest]
|
||||
);
|
||||
}
|
||||
}
|
||||
84
system/Commands/Generators/EntityGenerator.php
Normal file
84
system/Commands/Generators/EntityGenerator.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Generators;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\GeneratorTrait;
|
||||
|
||||
/**
|
||||
* Generates a skeleton Entity file.
|
||||
*/
|
||||
class EntityGenerator extends BaseCommand
|
||||
{
|
||||
use GeneratorTrait;
|
||||
|
||||
/**
|
||||
* The Command's Group
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Generators';
|
||||
|
||||
/**
|
||||
* The Command's Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'make:entity';
|
||||
|
||||
/**
|
||||
* The Command's Description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Generates a new entity file.';
|
||||
|
||||
/**
|
||||
* The Command's Usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'make:entity <name> [options]';
|
||||
|
||||
/**
|
||||
* The Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [
|
||||
'name' => 'The entity class name.',
|
||||
];
|
||||
|
||||
/**
|
||||
* The Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
|
||||
'--suffix' => 'Append the component title to the class name (e.g. User => UserEntity).',
|
||||
'--force' => 'Force overwrite existing file.',
|
||||
];
|
||||
|
||||
/**
|
||||
* Actually execute a command.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$this->component = 'Entity';
|
||||
$this->directory = 'Entities';
|
||||
$this->template = 'entity.tpl.php';
|
||||
|
||||
$this->classNameLang = 'CLI.generator.className.entity';
|
||||
$this->execute($params);
|
||||
}
|
||||
}
|
||||
84
system/Commands/Generators/FilterGenerator.php
Normal file
84
system/Commands/Generators/FilterGenerator.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Generators;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\GeneratorTrait;
|
||||
|
||||
/**
|
||||
* Generates a skeleton Filter file.
|
||||
*/
|
||||
class FilterGenerator extends BaseCommand
|
||||
{
|
||||
use GeneratorTrait;
|
||||
|
||||
/**
|
||||
* The Command's Group
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Generators';
|
||||
|
||||
/**
|
||||
* The Command's Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'make:filter';
|
||||
|
||||
/**
|
||||
* The Command's Description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Generates a new filter file.';
|
||||
|
||||
/**
|
||||
* The Command's Usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'make:filter <name> [options]';
|
||||
|
||||
/**
|
||||
* The Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [
|
||||
'name' => 'The filter class name.',
|
||||
];
|
||||
|
||||
/**
|
||||
* The Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
|
||||
'--suffix' => 'Append the component title to the class name (e.g. User => UserFilter).',
|
||||
'--force' => 'Force overwrite existing file.',
|
||||
];
|
||||
|
||||
/**
|
||||
* Actually execute a command.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$this->component = 'Filter';
|
||||
$this->directory = 'Filters';
|
||||
$this->template = 'filter.tpl.php';
|
||||
|
||||
$this->classNameLang = 'CLI.generator.className.filter';
|
||||
$this->execute($params);
|
||||
}
|
||||
}
|
||||
90
system/Commands/Generators/MigrateCreate.php
Normal file
90
system/Commands/Generators/MigrateCreate.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Generators;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
|
||||
/**
|
||||
* Deprecated class for the migration creation command.
|
||||
*
|
||||
* @deprecated Use make:migration instead.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class MigrateCreate extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Generators';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'migrate:create';
|
||||
|
||||
/**
|
||||
* The Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = '[DEPRECATED] Creates a new migration file. Please use "make:migration" instead.';
|
||||
|
||||
/**
|
||||
* The Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'migrate:create <name> [options]';
|
||||
|
||||
/**
|
||||
* The Command's arguments.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [
|
||||
'name' => 'The migration file name.',
|
||||
];
|
||||
|
||||
/**
|
||||
* The Command's options.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'--namespace' => 'Set root namespace. Defaults to APP_NAMESPACE',
|
||||
'--force' => 'Force overwrite existing files.',
|
||||
];
|
||||
|
||||
/**
|
||||
* Actually execute a command.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
// Resolve arguments before passing to make:migration
|
||||
$params[0] = $params[0] ?? CLI::getSegment(2);
|
||||
|
||||
$params['namespace'] = $params['namespace'] ?? CLI::getOption('namespace') ?? APP_NAMESPACE;
|
||||
|
||||
if (array_key_exists('force', $params) || CLI::getOption('force')) {
|
||||
$params['force'] = null;
|
||||
}
|
||||
|
||||
$this->call('make:migration', $params);
|
||||
}
|
||||
}
|
||||
121
system/Commands/Generators/MigrationGenerator.php
Normal file
121
system/Commands/Generators/MigrationGenerator.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Generators;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use CodeIgniter\CLI\GeneratorTrait;
|
||||
|
||||
/**
|
||||
* Generates a skeleton migration file.
|
||||
*/
|
||||
class MigrationGenerator extends BaseCommand
|
||||
{
|
||||
use GeneratorTrait;
|
||||
|
||||
/**
|
||||
* The Command's Group
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Generators';
|
||||
|
||||
/**
|
||||
* The Command's Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'make:migration';
|
||||
|
||||
/**
|
||||
* The Command's Description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Generates a new migration file.';
|
||||
|
||||
/**
|
||||
* The Command's Usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'make:migration <name> [options]';
|
||||
|
||||
/**
|
||||
* The Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [
|
||||
'name' => 'The migration class name.',
|
||||
];
|
||||
|
||||
/**
|
||||
* The Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'--session' => 'Generates the migration file for database sessions.',
|
||||
'--table' => 'Table name to use for database sessions. Default: "ci_sessions".',
|
||||
'--dbgroup' => 'Database group to use for database sessions. Default: "default".',
|
||||
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
|
||||
'--suffix' => 'Append the component title to the class name (e.g. User => UserMigration).',
|
||||
];
|
||||
|
||||
/**
|
||||
* Actually execute a command.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$this->component = 'Migration';
|
||||
$this->directory = 'Database\Migrations';
|
||||
$this->template = 'migration.tpl.php';
|
||||
|
||||
if (array_key_exists('session', $params) || CLI::getOption('session')) {
|
||||
$table = $params['table'] ?? CLI::getOption('table') ?? 'ci_sessions';
|
||||
$params[0] = "_create_{$table}_table";
|
||||
}
|
||||
|
||||
$this->classNameLang = 'CLI.generator.className.migration';
|
||||
$this->execute($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare options and do the necessary replacements.
|
||||
*/
|
||||
protected function prepare(string $class): string
|
||||
{
|
||||
$data['session'] = false;
|
||||
|
||||
if ($this->getOption('session')) {
|
||||
$table = $this->getOption('table');
|
||||
$DBGroup = $this->getOption('dbgroup');
|
||||
|
||||
$data['session'] = true;
|
||||
$data['table'] = is_string($table) ? $table : 'ci_sessions';
|
||||
$data['DBGroup'] = is_string($DBGroup) ? $DBGroup : 'default';
|
||||
$data['DBDriver'] = config('Database')->{$data['DBGroup']}['DBDriver'];
|
||||
$data['matchIP'] = config('App')->sessionMatchIP;
|
||||
}
|
||||
|
||||
return $this->parseTemplate($class, [], [], $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Change file basename before saving.
|
||||
*/
|
||||
protected function basename(string $filename): string
|
||||
{
|
||||
return gmdate(config('Migrations')->timestampFormat) . basename($filename);
|
||||
}
|
||||
}
|
||||
134
system/Commands/Generators/ModelGenerator.php
Normal file
134
system/Commands/Generators/ModelGenerator.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Generators;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use CodeIgniter\CLI\GeneratorTrait;
|
||||
|
||||
/**
|
||||
* Generates a skeleton Model file.
|
||||
*/
|
||||
class ModelGenerator extends BaseCommand
|
||||
{
|
||||
use GeneratorTrait;
|
||||
|
||||
/**
|
||||
* The Command's Group
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Generators';
|
||||
|
||||
/**
|
||||
* The Command's Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'make:model';
|
||||
|
||||
/**
|
||||
* The Command's Description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Generates a new model file.';
|
||||
|
||||
/**
|
||||
* The Command's Usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'make:model <name> [options]';
|
||||
|
||||
/**
|
||||
* The Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [
|
||||
'name' => 'The model class name.',
|
||||
];
|
||||
|
||||
/**
|
||||
* The Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'--table' => 'Supply a table name. Default: "the lowercased plural of the class name".',
|
||||
'--dbgroup' => 'Database group to use. Default: "default".',
|
||||
'--return' => 'Return type, Options: [array, object, entity]. Default: "array".',
|
||||
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
|
||||
'--suffix' => 'Append the component title to the class name (e.g. User => UserModel).',
|
||||
'--force' => 'Force overwrite existing file.',
|
||||
];
|
||||
|
||||
/**
|
||||
* Actually execute a command.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$this->component = 'Model';
|
||||
$this->directory = 'Models';
|
||||
$this->template = 'model.tpl.php';
|
||||
|
||||
$this->classNameLang = 'CLI.generator.className.model';
|
||||
$this->execute($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare options and do the necessary replacements.
|
||||
*/
|
||||
protected function prepare(string $class): string
|
||||
{
|
||||
$table = $this->getOption('table');
|
||||
$dbGroup = $this->getOption('dbgroup');
|
||||
$return = $this->getOption('return');
|
||||
|
||||
$baseClass = class_basename($class);
|
||||
|
||||
if (preg_match('/^(\S+)Model$/i', $baseClass, $match) === 1) {
|
||||
$baseClass = $match[1];
|
||||
}
|
||||
|
||||
$table = is_string($table) ? $table : plural(strtolower($baseClass));
|
||||
$dbGroup = is_string($dbGroup) ? $dbGroup : 'default';
|
||||
$return = is_string($return) ? $return : 'array';
|
||||
|
||||
if (! in_array($return, ['array', 'object', 'entity'], true)) {
|
||||
// @codeCoverageIgnoreStart
|
||||
$return = CLI::prompt(lang('CLI.generator.returnType'), ['array', 'object', 'entity'], 'required');
|
||||
CLI::newLine();
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
if ($return === 'entity') {
|
||||
$return = str_replace('Models', 'Entities', $class);
|
||||
|
||||
if (preg_match('/^(\S+)Model$/i', $return, $match) === 1) {
|
||||
$return = $match[1];
|
||||
|
||||
if ($this->getOption('suffix')) {
|
||||
$return .= 'Entity';
|
||||
}
|
||||
}
|
||||
|
||||
$return = '\\' . trim($return, '\\') . '::class';
|
||||
$this->call('make:entity', array_merge([$baseClass], $this->params));
|
||||
} else {
|
||||
$return = "'{$return}'";
|
||||
}
|
||||
|
||||
return $this->parseTemplate($class, ['{table}', '{dbGroup}', '{return}'], [$table, $dbGroup, $return]);
|
||||
}
|
||||
}
|
||||
121
system/Commands/Generators/ScaffoldGenerator.php
Normal file
121
system/Commands/Generators/ScaffoldGenerator.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Generators;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use CodeIgniter\CLI\GeneratorTrait;
|
||||
|
||||
/**
|
||||
* Generates a complete set of scaffold files.
|
||||
*/
|
||||
class ScaffoldGenerator extends BaseCommand
|
||||
{
|
||||
use GeneratorTrait;
|
||||
|
||||
/**
|
||||
* The Command's Group
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Generators';
|
||||
|
||||
/**
|
||||
* The Command's Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'make:scaffold';
|
||||
|
||||
/**
|
||||
* The Command's Description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Generates a complete set of scaffold files.';
|
||||
|
||||
/**
|
||||
* The Command's Usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'make:scaffold <name> [options]';
|
||||
|
||||
/**
|
||||
* The Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [
|
||||
'name' => 'The class name',
|
||||
];
|
||||
|
||||
/**
|
||||
* The Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'--bare' => 'Add the "--bare" option to controller component.',
|
||||
'--restful' => 'Add the "--restful" option to controller component.',
|
||||
'--table' => 'Add the "--table" option to the model component.',
|
||||
'--dbgroup' => 'Add the "--dbgroup" option to model component.',
|
||||
'--return' => 'Add the "--return" option to the model component.',
|
||||
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
|
||||
'--suffix' => 'Append the component title to the class name.',
|
||||
'--force' => 'Force overwrite existing file.',
|
||||
];
|
||||
|
||||
/**
|
||||
* Actually execute a command.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$this->params = $params;
|
||||
|
||||
$options = [];
|
||||
|
||||
if ($this->getOption('namespace')) {
|
||||
$options['namespace'] = $this->getOption('namespace');
|
||||
}
|
||||
|
||||
if ($this->getOption('suffix')) {
|
||||
$options['suffix'] = null;
|
||||
}
|
||||
|
||||
if ($this->getOption('force')) {
|
||||
$options['force'] = null;
|
||||
}
|
||||
|
||||
$controllerOpts = [];
|
||||
|
||||
if ($this->getOption('bare')) {
|
||||
$controllerOpts['bare'] = null;
|
||||
} elseif ($this->getOption('restful')) {
|
||||
$controllerOpts['restful'] = $this->getOption('restful');
|
||||
}
|
||||
|
||||
$modelOpts = [
|
||||
'table' => $this->getOption('table'),
|
||||
'dbgroup' => $this->getOption('dbgroup'),
|
||||
'return' => $this->getOption('return'),
|
||||
];
|
||||
|
||||
$class = $params[0] ?? CLI::getSegment(2);
|
||||
|
||||
// Call those commands!
|
||||
$this->call('make:controller', array_merge([$class], $controllerOpts, $options));
|
||||
$this->call('make:model', array_merge([$class], $modelOpts, $options));
|
||||
$this->call('make:migration', array_merge([$class], $options));
|
||||
$this->call('make:seeder', array_merge([$class], $options));
|
||||
}
|
||||
}
|
||||
84
system/Commands/Generators/SeederGenerator.php
Normal file
84
system/Commands/Generators/SeederGenerator.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Generators;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\GeneratorTrait;
|
||||
|
||||
/**
|
||||
* Generates a skeleton seeder file.
|
||||
*/
|
||||
class SeederGenerator extends BaseCommand
|
||||
{
|
||||
use GeneratorTrait;
|
||||
|
||||
/**
|
||||
* The Command's Group
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Generators';
|
||||
|
||||
/**
|
||||
* The Command's Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'make:seeder';
|
||||
|
||||
/**
|
||||
* The Command's Description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Generates a new seeder file.';
|
||||
|
||||
/**
|
||||
* The Command's Usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'make:seeder <name> [options]';
|
||||
|
||||
/**
|
||||
* The Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [
|
||||
'name' => 'The seeder class name.',
|
||||
];
|
||||
|
||||
/**
|
||||
* The Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
|
||||
'--suffix' => 'Append the component title to the class name (e.g. User => UserSeeder).',
|
||||
'--force' => 'Force overwrite existing file.',
|
||||
];
|
||||
|
||||
/**
|
||||
* Actually execute a command.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$this->component = 'Seeder';
|
||||
$this->directory = 'Database\Seeds';
|
||||
$this->template = 'seeder.tpl.php';
|
||||
|
||||
$this->classNameLang = 'CLI.generator.className.seeder';
|
||||
$this->execute($params);
|
||||
}
|
||||
}
|
||||
110
system/Commands/Generators/SessionMigrationGenerator.php
Normal file
110
system/Commands/Generators/SessionMigrationGenerator.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Generators;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use CodeIgniter\CLI\GeneratorTrait;
|
||||
|
||||
/**
|
||||
* Generates a migration file for database sessions.
|
||||
*
|
||||
* @deprecated Use `make:migration --session` instead.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class SessionMigrationGenerator extends BaseCommand
|
||||
{
|
||||
use GeneratorTrait;
|
||||
|
||||
/**
|
||||
* The Command's Group
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Generators';
|
||||
|
||||
/**
|
||||
* The Command's Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'session:migration';
|
||||
|
||||
/**
|
||||
* The Command's Description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = '[DEPRECATED] Generates the migration file for database sessions, Please use "make:migration --session" instead.';
|
||||
|
||||
/**
|
||||
* The Command's Usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'session:migration [options]';
|
||||
|
||||
/**
|
||||
* The Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'-t' => 'Supply a table name.',
|
||||
'-g' => 'Database group to use. Default: "default".',
|
||||
];
|
||||
|
||||
/**
|
||||
* Actually execute a command.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$this->component = 'Migration';
|
||||
$this->directory = 'Database\Migrations';
|
||||
$this->template = 'migration.tpl.php';
|
||||
|
||||
$table = 'ci_sessions';
|
||||
|
||||
if (array_key_exists('t', $params) || CLI::getOption('t')) {
|
||||
$table = $params['t'] ?? CLI::getOption('t');
|
||||
}
|
||||
|
||||
$params[0] = "_create_{$table}_table";
|
||||
|
||||
$this->execute($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the necessary replacements.
|
||||
*/
|
||||
protected function prepare(string $class): string
|
||||
{
|
||||
$data['session'] = true;
|
||||
$data['table'] = $this->getOption('t');
|
||||
$data['DBGroup'] = $this->getOption('g');
|
||||
$data['matchIP'] = config('App')->sessionMatchIP ?? false;
|
||||
|
||||
$data['table'] = is_string($data['table']) ? $data['table'] : 'ci_sessions';
|
||||
$data['DBGroup'] = is_string($data['DBGroup']) ? $data['DBGroup'] : 'default';
|
||||
|
||||
return $this->parseTemplate($class, [], [], $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Change file basename before saving.
|
||||
*/
|
||||
protected function basename(string $filename): string
|
||||
{
|
||||
return gmdate(config('Migrations')->timestampFormat) . basename($filename);
|
||||
}
|
||||
}
|
||||
84
system/Commands/Generators/ValidationGenerator.php
Normal file
84
system/Commands/Generators/ValidationGenerator.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Generators;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\GeneratorTrait;
|
||||
|
||||
/**
|
||||
* Generates a skeleton Validation file.
|
||||
*/
|
||||
class ValidationGenerator extends BaseCommand
|
||||
{
|
||||
use GeneratorTrait;
|
||||
|
||||
/**
|
||||
* The Command's Group
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Generators';
|
||||
|
||||
/**
|
||||
* The Command's Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'make:validation';
|
||||
|
||||
/**
|
||||
* The Command's Description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Generates a new validation file.';
|
||||
|
||||
/**
|
||||
* The Command's Usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'make:validation <name> [options]';
|
||||
|
||||
/**
|
||||
* The Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [
|
||||
'name' => 'The validation class name.',
|
||||
];
|
||||
|
||||
/**
|
||||
* The Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
|
||||
'--suffix' => 'Append the component title to the class name (e.g. User => UserValidation).',
|
||||
'--force' => 'Force overwrite existing file.',
|
||||
];
|
||||
|
||||
/**
|
||||
* Actually execute a command.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$this->component = 'Validation';
|
||||
$this->directory = 'Validation';
|
||||
$this->template = 'validation.tpl.php';
|
||||
|
||||
$this->classNameLang = 'CLI.generator.className.validation';
|
||||
$this->execute($params);
|
||||
}
|
||||
}
|
||||
76
system/Commands/Generators/Views/command.tpl.php
Normal file
76
system/Commands/Generators/Views/command.tpl.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<@php
|
||||
|
||||
namespace {namespace};
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
<?php if ($type === 'generator'): ?>
|
||||
use CodeIgniter\CLI\GeneratorTrait;
|
||||
<?php endif ?>
|
||||
|
||||
class {class} extends BaseCommand
|
||||
{
|
||||
<?php if ($type === 'generator'): ?>
|
||||
use GeneratorTrait;
|
||||
|
||||
<?php endif ?>
|
||||
/**
|
||||
* The Command's Group
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = '{group}';
|
||||
|
||||
/**
|
||||
* The Command's Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = '{command}';
|
||||
|
||||
/**
|
||||
* The Command's Description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = '';
|
||||
|
||||
/**
|
||||
* The Command's Usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = '{command} [arguments] [options]';
|
||||
|
||||
/**
|
||||
* The Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [];
|
||||
|
||||
/**
|
||||
* The Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* Actually execute a command.
|
||||
*
|
||||
* @param array $params
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
<?php if ($type === 'generator'): ?>
|
||||
$this->component = 'Command';
|
||||
$this->directory = 'Commands';
|
||||
$this->template = 'command.tpl.php';
|
||||
|
||||
$this->execute($params);
|
||||
<?php else: ?>
|
||||
//
|
||||
<?php endif ?>
|
||||
}
|
||||
}
|
||||
10
system/Commands/Generators/Views/config.tpl.php
Normal file
10
system/Commands/Generators/Views/config.tpl.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<@php
|
||||
|
||||
namespace {namespace};
|
||||
|
||||
use CodeIgniter\Config\BaseConfig;
|
||||
|
||||
class {class} extends BaseConfig
|
||||
{
|
||||
//
|
||||
}
|
||||
177
system/Commands/Generators/Views/controller.tpl.php
Normal file
177
system/Commands/Generators/Views/controller.tpl.php
Normal file
@@ -0,0 +1,177 @@
|
||||
<@php
|
||||
|
||||
namespace {namespace};
|
||||
|
||||
use {useStatement};
|
||||
|
||||
class {class} extends {extends}
|
||||
{
|
||||
<?php if ($type === 'controller'): ?>
|
||||
/**
|
||||
* Return an array of resource objects, themselves in array format
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the properties of a resource object
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function show($id = null)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new resource object, with default properties
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function new()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new resource object, from "posted" parameters
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the editable properties of a resource object
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function edit($id = null)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Add or update a model resource, from "posted" properties
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function update($id = null)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the designated resource object from the model
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete($id = null)
|
||||
{
|
||||
//
|
||||
}
|
||||
<?php elseif ($type === 'presenter'): ?>
|
||||
/**
|
||||
* Present a view of resource objects
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Present a view to present a specific resource object
|
||||
*
|
||||
* @param mixed $id
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function show($id = null)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Present a view to present a new single resource object
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function new()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the creation/insertion of a new resource object.
|
||||
* This should be a POST.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Present a view to edit the properties of a specific resource object
|
||||
*
|
||||
* @param mixed $id
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function edit($id = null)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the updating, full or partial, of a specific resource object.
|
||||
* This should be a POST.
|
||||
*
|
||||
* @param mixed $id
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function update($id = null)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Present a view to confirm the deletion of a specific resource object
|
||||
*
|
||||
* @param mixed $id
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function remove($id = null)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the deletion of a specific resource object
|
||||
*
|
||||
* @param mixed $id
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete($id = null)
|
||||
{
|
||||
//
|
||||
}
|
||||
<?php else: ?>
|
||||
public function index()
|
||||
{
|
||||
//
|
||||
}
|
||||
<?php endif ?>
|
||||
}
|
||||
12
system/Commands/Generators/Views/entity.tpl.php
Normal file
12
system/Commands/Generators/Views/entity.tpl.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<@php
|
||||
|
||||
namespace {namespace};
|
||||
|
||||
use CodeIgniter\Entity\Entity;
|
||||
|
||||
class {class} extends Entity
|
||||
{
|
||||
protected $datamap = [];
|
||||
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
|
||||
protected $casts = [];
|
||||
}
|
||||
47
system/Commands/Generators/Views/filter.tpl.php
Normal file
47
system/Commands/Generators/Views/filter.tpl.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<@php
|
||||
|
||||
namespace {namespace};
|
||||
|
||||
use CodeIgniter\Filters\FilterInterface;
|
||||
use CodeIgniter\HTTP\RequestInterface;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
|
||||
class {class} implements FilterInterface
|
||||
{
|
||||
/**
|
||||
* Do whatever processing this filter needs to do.
|
||||
* By default it should not return anything during
|
||||
* normal execution. However, when an abnormal state
|
||||
* is found, it should return an instance of
|
||||
* CodeIgniter\HTTP\Response. If it does, script
|
||||
* execution will end and that Response will be
|
||||
* sent back to the client, allowing for error pages,
|
||||
* redirects, etc.
|
||||
*
|
||||
* @param RequestInterface $request
|
||||
* @param array|null $arguments
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function before(RequestInterface $request, $arguments = null)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows After filters to inspect and modify the response
|
||||
* object as needed. This method does not allow any way
|
||||
* to stop execution of other after filters, short of
|
||||
* throwing an Exception or Error.
|
||||
*
|
||||
* @param RequestInterface $request
|
||||
* @param ResponseInterface $response
|
||||
* @param array|null $arguments
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
50
system/Commands/Generators/Views/migration.tpl.php
Normal file
50
system/Commands/Generators/Views/migration.tpl.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<@php
|
||||
|
||||
namespace {namespace};
|
||||
|
||||
use CodeIgniter\Database\Migration;
|
||||
|
||||
class {class} extends Migration
|
||||
{
|
||||
<?php if ($session): ?>
|
||||
protected $DBGroup = '<?= $DBGroup ?>';
|
||||
|
||||
public function up()
|
||||
{
|
||||
$this->forge->addField([
|
||||
'id' => ['type' => 'VARCHAR', 'constraint' => 128, 'null' => false],
|
||||
<?php if ($DBDriver === 'MySQLi'): ?>
|
||||
'ip_address' => ['type' => 'VARCHAR', 'constraint' => 45, 'null' => false],
|
||||
'timestamp timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL',
|
||||
'data' => ['type' => 'BLOB', 'null' => false],
|
||||
<?php elseif ($DBDriver === 'Postgre'): ?>
|
||||
'ip_address inet NOT NULL',
|
||||
'timestamp timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL',
|
||||
"data bytea DEFAULT '' NOT NULL",
|
||||
<?php endif; ?>
|
||||
]);
|
||||
<?php if ($matchIP) : ?>
|
||||
$this->forge->addKey(['id', 'ip_address'], true);
|
||||
<?php else: ?>
|
||||
$this->forge->addKey('id', true);
|
||||
<?php endif ?>
|
||||
$this->forge->addKey('timestamp');
|
||||
$this->forge->createTable('<?= $table ?>', true);
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
$this->forge->dropTable('<?= $table ?>', true);
|
||||
}
|
||||
<?php else: ?>
|
||||
public function up()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
//
|
||||
}
|
||||
<?php endif ?>
|
||||
}
|
||||
42
system/Commands/Generators/Views/model.tpl.php
Normal file
42
system/Commands/Generators/Views/model.tpl.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<@php
|
||||
|
||||
namespace {namespace};
|
||||
|
||||
use CodeIgniter\Model;
|
||||
|
||||
class {class} extends Model
|
||||
{
|
||||
protected $DBGroup = '{dbGroup}';
|
||||
protected $table = '{table}';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $insertID = 0;
|
||||
protected $returnType = {return};
|
||||
protected $useSoftDeletes = false;
|
||||
protected $protectFields = true;
|
||||
protected $allowedFields = [];
|
||||
|
||||
// Dates
|
||||
protected $useTimestamps = false;
|
||||
protected $dateFormat = 'datetime';
|
||||
protected $createdField = 'created_at';
|
||||
protected $updatedField = 'updated_at';
|
||||
protected $deletedField = 'deleted_at';
|
||||
|
||||
// Validation
|
||||
protected $validationRules = [];
|
||||
protected $validationMessages = [];
|
||||
protected $skipValidation = false;
|
||||
protected $cleanValidationRules = true;
|
||||
|
||||
// Callbacks
|
||||
protected $allowCallbacks = true;
|
||||
protected $beforeInsert = [];
|
||||
protected $afterInsert = [];
|
||||
protected $beforeUpdate = [];
|
||||
protected $afterUpdate = [];
|
||||
protected $beforeFind = [];
|
||||
protected $afterFind = [];
|
||||
protected $beforeDelete = [];
|
||||
protected $afterDelete = [];
|
||||
}
|
||||
13
system/Commands/Generators/Views/seeder.tpl.php
Normal file
13
system/Commands/Generators/Views/seeder.tpl.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<@php
|
||||
|
||||
namespace {namespace};
|
||||
|
||||
use CodeIgniter\Database\Seeder;
|
||||
|
||||
class {class} extends Seeder
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
11
system/Commands/Generators/Views/validation.tpl.php
Normal file
11
system/Commands/Generators/Views/validation.tpl.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<@php
|
||||
|
||||
namespace {namespace};
|
||||
|
||||
class {class}
|
||||
{
|
||||
// public function custom_rule(): bool
|
||||
// {
|
||||
// return true;
|
||||
// }
|
||||
}
|
||||
85
system/Commands/Help.php
Normal file
85
system/Commands/Help.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
|
||||
/**
|
||||
* CI Help command for the spark script.
|
||||
*
|
||||
* Lists the basic usage information for the spark script,
|
||||
* and provides a way to list help for other commands.
|
||||
*/
|
||||
class Help extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'CodeIgniter';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'help';
|
||||
|
||||
/**
|
||||
* the Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Displays basic usage information.';
|
||||
|
||||
/**
|
||||
* the Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'help command_name';
|
||||
|
||||
/**
|
||||
* the Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [
|
||||
'command_name' => 'The command name [default: "help"]',
|
||||
];
|
||||
|
||||
/**
|
||||
* the Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* Displays the help for spark commands.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$command = array_shift($params);
|
||||
$command = $command ?? 'help';
|
||||
$commands = $this->commands->getCommands();
|
||||
|
||||
if (! $this->commands->verifyCommand($command, $commands)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$class = new $commands[$command]['class']($this->logger, $this->commands);
|
||||
$class->showHelp();
|
||||
}
|
||||
}
|
||||
70
system/Commands/Housekeeping/ClearDebugbar.php
Normal file
70
system/Commands/Housekeeping/ClearDebugbar.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Housekeeping;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
|
||||
/**
|
||||
* ClearDebugbar Command
|
||||
*/
|
||||
class ClearDebugbar extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Housekeeping';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'debugbar:clear';
|
||||
|
||||
/**
|
||||
* The Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'debugbar:clear';
|
||||
|
||||
/**
|
||||
* The Command's short description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Clears all debugbar JSON files.';
|
||||
|
||||
/**
|
||||
* Actually runs the command.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
helper('filesystem');
|
||||
|
||||
if (! delete_files(WRITEPATH . 'debugbar')) {
|
||||
// @codeCoverageIgnoreStart
|
||||
CLI::error('Error deleting the debugbar JSON files.');
|
||||
CLI::newLine();
|
||||
|
||||
return;
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
CLI::write('Debugbar cleared.', 'green');
|
||||
CLI::newLine();
|
||||
}
|
||||
}
|
||||
91
system/Commands/Housekeeping/ClearLogs.php
Normal file
91
system/Commands/Housekeeping/ClearLogs.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Housekeeping;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
|
||||
/**
|
||||
* ClearLogs command.
|
||||
*/
|
||||
class ClearLogs extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'Housekeeping';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'logs:clear';
|
||||
|
||||
/**
|
||||
* The Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Clears all log files.';
|
||||
|
||||
/**
|
||||
* The Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'logs:clear [option';
|
||||
|
||||
/**
|
||||
* The Command's options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'--force' => 'Force delete of all logs files without prompting.',
|
||||
];
|
||||
|
||||
/**
|
||||
* Actually execute a command.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$force = array_key_exists('force', $params) || CLI::getOption('force');
|
||||
|
||||
if (! $force && CLI::prompt('Are you sure you want to delete the logs?', ['n', 'y']) === 'n') {
|
||||
// @codeCoverageIgnoreStart
|
||||
CLI::error('Deleting logs aborted.', 'light_gray', 'red');
|
||||
CLI::error('If you want, use the "-force" option to force delete all log files.', 'light_gray', 'red');
|
||||
CLI::newLine();
|
||||
|
||||
return;
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
helper('filesystem');
|
||||
|
||||
if (! delete_files(WRITEPATH . 'logs', false, true)) {
|
||||
// @codeCoverageIgnoreStart
|
||||
CLI::error('Error in deleting the logs files.', 'light_gray', 'red');
|
||||
CLI::newLine();
|
||||
|
||||
return;
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
CLI::write('Logs cleared.', 'green');
|
||||
CLI::newLine();
|
||||
}
|
||||
}
|
||||
134
system/Commands/ListCommands.php
Normal file
134
system/Commands/ListCommands.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
|
||||
/**
|
||||
* CI Help command for the spark script.
|
||||
*
|
||||
* Lists the basic usage information for the spark script,
|
||||
* and provides a way to list help for other commands.
|
||||
*/
|
||||
class ListCommands extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'CodeIgniter';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'list';
|
||||
|
||||
/**
|
||||
* the Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Lists the available commands.';
|
||||
|
||||
/**
|
||||
* the Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'list';
|
||||
|
||||
/**
|
||||
* the Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [];
|
||||
|
||||
/**
|
||||
* the Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'--simple' => 'Prints a list of the commands with no other info',
|
||||
];
|
||||
|
||||
/**
|
||||
* Displays the help for the spark cli script itself.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$commands = $this->commands->getCommands();
|
||||
ksort($commands);
|
||||
|
||||
// Check for 'simple' format
|
||||
return array_key_exists('simple', $params) || CLI::getOption('simple')
|
||||
? $this->listSimple($commands)
|
||||
: $this->listFull($commands);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists the commands with accompanying info.
|
||||
*/
|
||||
protected function listFull(array $commands)
|
||||
{
|
||||
// Sort into buckets by group
|
||||
$groups = [];
|
||||
|
||||
foreach ($commands as $title => $command) {
|
||||
if (! isset($groups[$command['group']])) {
|
||||
$groups[$command['group']] = [];
|
||||
}
|
||||
|
||||
$groups[$command['group']][$title] = $command;
|
||||
}
|
||||
|
||||
$length = max(array_map('strlen', array_keys($commands)));
|
||||
|
||||
ksort($groups);
|
||||
|
||||
// Display it all...
|
||||
foreach ($groups as $group => $commands) {
|
||||
CLI::write($group, 'yellow');
|
||||
|
||||
foreach ($commands as $name => $command) {
|
||||
$name = $this->setPad($name, $length, 2, 2);
|
||||
$output = CLI::color($name, 'green');
|
||||
|
||||
if (isset($command['description'])) {
|
||||
$output .= CLI::wrap($command['description'], 125, strlen($name));
|
||||
}
|
||||
|
||||
CLI::write($output);
|
||||
}
|
||||
|
||||
if ($group !== array_key_last($groups)) {
|
||||
CLI::newLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists the commands only.
|
||||
*/
|
||||
protected function listSimple(array $commands)
|
||||
{
|
||||
foreach (array_keys($commands) as $title) {
|
||||
CLI::write($title);
|
||||
}
|
||||
}
|
||||
}
|
||||
117
system/Commands/Server/Serve.php
Normal file
117
system/Commands/Server/Serve.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Server;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
|
||||
/**
|
||||
* Launch the PHP development server
|
||||
*
|
||||
* Not testable, as it throws phpunit for a loop :-/
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class Serve extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* Group
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'CodeIgniter';
|
||||
|
||||
/**
|
||||
* Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'serve';
|
||||
|
||||
/**
|
||||
* Description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Launches the CodeIgniter PHP-Development Server.';
|
||||
|
||||
/**
|
||||
* Usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'serve';
|
||||
|
||||
/**
|
||||
* Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [];
|
||||
|
||||
/**
|
||||
* The current port offset.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $portOffset = 0;
|
||||
|
||||
/**
|
||||
* The max number of ports to attempt to serve from
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $tries = 10;
|
||||
|
||||
/**
|
||||
* Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [
|
||||
'--php' => 'The PHP Binary [default: "PHP_BINARY"]',
|
||||
'--host' => 'The HTTP Host [default: "localhost"]',
|
||||
'--port' => 'The HTTP Host Port [default: "8080"]',
|
||||
];
|
||||
|
||||
/**
|
||||
* Run the server
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
// Collect any user-supplied options and apply them.
|
||||
$php = escapeshellarg(CLI::getOption('php') ?? PHP_BINARY);
|
||||
$host = CLI::getOption('host') ?? 'localhost';
|
||||
$port = (int) (CLI::getOption('port') ?? 8080) + $this->portOffset;
|
||||
|
||||
// Get the party started.
|
||||
CLI::write('CodeIgniter development server started on http://' . $host . ':' . $port, 'green');
|
||||
CLI::write('Press Control-C to stop.');
|
||||
|
||||
// Set the Front Controller path as Document Root.
|
||||
$docroot = escapeshellarg(FCPATH);
|
||||
|
||||
// Mimic Apache's mod_rewrite functionality with user settings.
|
||||
$rewrite = escapeshellarg(__DIR__ . '/rewrite.php');
|
||||
|
||||
// Call PHP's built-in webserver, making sure to set our
|
||||
// base path to the public folder, and to use the rewrite file
|
||||
// to ensure our environment is set and it simulates basic mod_rewrite.
|
||||
passthru($php . ' -S ' . $host . ':' . $port . ' -t ' . $docroot . ' ' . $rewrite, $status);
|
||||
|
||||
if ($status && $this->portOffset < $this->tries) {
|
||||
$this->portOffset++;
|
||||
|
||||
$this->run($params);
|
||||
}
|
||||
}
|
||||
}
|
||||
44
system/Commands/Server/rewrite.php
Normal file
44
system/Commands/Server/rewrite.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/*
|
||||
* CodeIgniter PHP-Development Server Rewrite Rules
|
||||
*
|
||||
* This script works with the CLI serve command to help run a seamless
|
||||
* development server based around PHP's built-in development
|
||||
* server. This file simply tries to mimic Apache's mod_rewrite
|
||||
* functionality so the site will operate as normal.
|
||||
*/
|
||||
|
||||
// @codeCoverageIgnoreStart
|
||||
// Avoid this file run when listing commands
|
||||
if (PHP_SAPI === 'cli') {
|
||||
return;
|
||||
}
|
||||
|
||||
$uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
|
||||
|
||||
// Front Controller path - expected to be in the default folder
|
||||
$fcpath = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR;
|
||||
|
||||
// Full path
|
||||
$path = $fcpath . ltrim($uri, '/');
|
||||
|
||||
// If $path is an existing file or folder within the public folder
|
||||
// then let the request handle it like normal.
|
||||
if ($uri !== '/' && (is_file($path) || is_dir($path))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Otherwise, we'll load the index file and let
|
||||
// the framework handle the request from here.
|
||||
require_once $fcpath . 'index.php';
|
||||
// @codeCoverageIgnoreEnd
|
||||
155
system/Commands/Utilities/Environment.php
Normal file
155
system/Commands/Utilities/Environment.php
Normal file
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Utilities;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use CodeIgniter\Config\DotEnv;
|
||||
|
||||
/**
|
||||
* Command to display the current environment,
|
||||
* or set a new one in the `.env` file.
|
||||
*/
|
||||
final class Environment extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'CodeIgniter';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'env';
|
||||
|
||||
/**
|
||||
* The Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Retrieves the current environment, or set a new one.';
|
||||
|
||||
/**
|
||||
* The Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'env [<environment>]';
|
||||
|
||||
/**
|
||||
* The Command's arguments
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
protected $arguments = [
|
||||
'environment' => '[Optional] The new environment to set. If none is provided, this will print the current environment.',
|
||||
];
|
||||
|
||||
/**
|
||||
* The Command's options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* Allowed values for environment. `testing` is excluded
|
||||
* since spark won't work on it.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
private static $knownTypes = [
|
||||
'production',
|
||||
'development',
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
if ($params === []) {
|
||||
CLI::write(sprintf('Your environment is currently set as %s.', CLI::color($_SERVER['CI_ENVIRONMENT'] ?? ENVIRONMENT, 'green')));
|
||||
CLI::newLine();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$env = strtolower(array_shift($params));
|
||||
|
||||
if ($env === 'testing') {
|
||||
CLI::error('The "testing" environment is reserved for PHPUnit testing.', 'light_gray', 'red');
|
||||
CLI::error('You will not be able to run spark under a "testing" environment.', 'light_gray', 'red');
|
||||
CLI::newLine();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (! in_array($env, self::$knownTypes, true)) {
|
||||
CLI::error(sprintf('Invalid environment type "%s". Expected one of "%s".', $env, implode('" and "', self::$knownTypes)), 'light_gray', 'red');
|
||||
CLI::newLine();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (! $this->writeNewEnvironmentToEnvFile($env)) {
|
||||
CLI::error('Error in writing new environment to .env file.', 'light_gray', 'red');
|
||||
CLI::newLine();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// force DotEnv to reload the new environment
|
||||
// however we cannot redefine the ENVIRONMENT constant
|
||||
putenv('CI_ENVIRONMENT');
|
||||
unset($_ENV['CI_ENVIRONMENT'], $_SERVER['CI_ENVIRONMENT']);
|
||||
(new DotEnv(ROOTPATH))->load();
|
||||
|
||||
CLI::write(sprintf('Environment is successfully changed to "%s".', $env), 'green');
|
||||
CLI::write('The ENVIRONMENT constant will be changed in the next script execution.');
|
||||
CLI::newLine();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://regex101.com/r/4sSORp/1 for the regex in action
|
||||
*/
|
||||
private function writeNewEnvironmentToEnvFile(string $newEnv): bool
|
||||
{
|
||||
$baseEnv = ROOTPATH . 'env';
|
||||
$envFile = ROOTPATH . '.env';
|
||||
|
||||
if (! is_file($envFile)) {
|
||||
if (! is_file($baseEnv)) {
|
||||
CLI::write('Both default shipped `env` file and custom `.env` are missing.', 'yellow');
|
||||
CLI::write('It is impossible to write the new environment type.', 'yellow');
|
||||
CLI::newLine();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
copy($baseEnv, $envFile);
|
||||
}
|
||||
|
||||
$pattern = preg_quote($_SERVER['CI_ENVIRONMENT'] ?? ENVIRONMENT, '/');
|
||||
$pattern = sprintf('/^[#\s]*CI_ENVIRONMENT[=\s]+%s$/m', $pattern);
|
||||
|
||||
return file_put_contents(
|
||||
$envFile,
|
||||
preg_replace($pattern, "\nCI_ENVIRONMENT = {$newEnv}", file_get_contents($envFile), -1, $count)
|
||||
) !== false && $count > 0;
|
||||
}
|
||||
}
|
||||
95
system/Commands/Utilities/Namespaces.php
Normal file
95
system/Commands/Utilities/Namespaces.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Utilities;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use Config\Autoload;
|
||||
|
||||
/**
|
||||
* Lists namespaces set in Config\Autoload with their
|
||||
* full server path. Helps you to verify that you have
|
||||
* the namespaces setup correctly.
|
||||
*/
|
||||
class Namespaces extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'CodeIgniter';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'namespaces';
|
||||
|
||||
/**
|
||||
* the Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Verifies your namespaces are setup correctly.';
|
||||
|
||||
/**
|
||||
* the Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'namespaces';
|
||||
|
||||
/**
|
||||
* the Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [];
|
||||
|
||||
/**
|
||||
* the Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* Displays the help for the spark cli script itself.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$config = new Autoload();
|
||||
|
||||
$tbody = [];
|
||||
|
||||
foreach ($config->psr4 as $ns => $path) {
|
||||
$path = realpath($path) ?: $path;
|
||||
|
||||
$tbody[] = [
|
||||
$ns,
|
||||
realpath($path) ?: $path,
|
||||
is_dir($path) ? 'Yes' : 'MISSING',
|
||||
];
|
||||
}
|
||||
|
||||
$thead = [
|
||||
'Namespace',
|
||||
'Path',
|
||||
'Found?',
|
||||
];
|
||||
|
||||
CLI::table($tbody, $thead);
|
||||
}
|
||||
}
|
||||
104
system/Commands/Utilities/Publish.php
Normal file
104
system/Commands/Utilities/Publish.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Utilities;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use CodeIgniter\Publisher\Publisher;
|
||||
|
||||
/**
|
||||
* Discovers all Publisher classes from the "Publishers/" directory
|
||||
* across namespaces. Executes `publish()` from each instance, parsing
|
||||
* each result.
|
||||
*/
|
||||
class Publish extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'CodeIgniter';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'publish';
|
||||
|
||||
/**
|
||||
* The Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Discovers and executes all predefined Publisher classes.';
|
||||
|
||||
/**
|
||||
* The Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'publish [<directory>]';
|
||||
|
||||
/**
|
||||
* The Command's arguments
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
protected $arguments = [
|
||||
'directory' => '[Optional] The directory to scan within each namespace. Default: "Publishers".',
|
||||
];
|
||||
|
||||
/**
|
||||
* the Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* Displays the help for the spark cli script itself.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$directory = array_shift($params) ?? 'Publishers';
|
||||
|
||||
if ([] === $publishers = Publisher::discover($directory)) {
|
||||
CLI::write(lang('Publisher.publishMissing', [$directory]));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($publishers as $publisher) {
|
||||
if ($publisher->publish()) {
|
||||
CLI::write(lang('Publisher.publishSuccess', [
|
||||
get_class($publisher),
|
||||
count($publisher->getPublished()),
|
||||
$publisher->getDestination(),
|
||||
]), 'green');
|
||||
} else {
|
||||
CLI::error(lang('Publisher.publishFailure', [
|
||||
get_class($publisher),
|
||||
$publisher->getDestination(),
|
||||
]), 'light_gray', 'red');
|
||||
|
||||
foreach ($publisher->getErrors() as $file => $exception) {
|
||||
CLI::write($file);
|
||||
CLI::error($exception->getMessage());
|
||||
CLI::newLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
112
system/Commands/Utilities/Routes.php
Normal file
112
system/Commands/Utilities/Routes.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of CodeIgniter 4 framework.
|
||||
*
|
||||
* (c) CodeIgniter Foundation <admin@codeigniter.com>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace CodeIgniter\Commands\Utilities;
|
||||
|
||||
use CodeIgniter\CLI\BaseCommand;
|
||||
use CodeIgniter\CLI\CLI;
|
||||
use Config\Services;
|
||||
|
||||
/**
|
||||
* Lists all of the user-defined routes. This will include any Routes files
|
||||
* that can be discovered, but will NOT include any routes that are not defined
|
||||
* in a routes file, but are instead discovered through auto-routing.
|
||||
*/
|
||||
class Routes extends BaseCommand
|
||||
{
|
||||
/**
|
||||
* The group the command is lumped under
|
||||
* when listing commands.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $group = 'CodeIgniter';
|
||||
|
||||
/**
|
||||
* The Command's name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'routes';
|
||||
|
||||
/**
|
||||
* the Command's short description
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Displays all of user-defined routes. Does NOT display auto-detected routes.';
|
||||
|
||||
/**
|
||||
* the Command's usage
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $usage = 'routes';
|
||||
|
||||
/**
|
||||
* the Command's Arguments
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [];
|
||||
|
||||
/**
|
||||
* the Command's Options
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* Displays the help for the spark cli script itself.
|
||||
*/
|
||||
public function run(array $params)
|
||||
{
|
||||
$collection = Services::routes(true);
|
||||
$methods = [
|
||||
'get',
|
||||
'head',
|
||||
'post',
|
||||
'patch',
|
||||
'put',
|
||||
'delete',
|
||||
'options',
|
||||
'trace',
|
||||
'connect',
|
||||
'cli',
|
||||
];
|
||||
|
||||
$tbody = [];
|
||||
|
||||
foreach ($methods as $method) {
|
||||
$routes = $collection->getRoutes($method);
|
||||
|
||||
foreach ($routes as $route => $handler) {
|
||||
// filter for strings, as callbacks aren't displayable
|
||||
if (is_string($handler)) {
|
||||
$tbody[] = [
|
||||
strtoupper($method),
|
||||
$route,
|
||||
$handler,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$thead = [
|
||||
'Method',
|
||||
'Route',
|
||||
'Handler',
|
||||
];
|
||||
|
||||
CLI::table($tbody, $thead);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user