first commit

This commit is contained in:
DESKTOP-DH6BVPV\chiefsoft
2022-11-20 09:46:33 -05:00
commit 6ddfa92eed
618 changed files with 104537 additions and 0 deletions
+87
View 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
View 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
View 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
View 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(lang('Migrations.migrated'), 'green');
// @codeCoverageIgnoreStart
} catch (Throwable $e) {
$this->showError($e);
// @codeCoverageIgnoreEnd
}
}
}
@@ -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);
}
}
@@ -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
}
}
}
+165
View File
@@ -0,0 +1,165 @@
<?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();
$paramGroup = $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;
}
$runner->setNamespace($namespace);
$history = $runner->getHistory((string) $paramGroup);
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
View 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);
}
}
}
+283
View File
@@ -0,0 +1,283 @@
<?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\BaseConnection;
use Config\Database;
/**
* Get table data if it exists in the database.
*/
class ShowTableInfo 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:table';
/**
* the Command's short description
*
* @var string
*/
protected $description = 'Retrieves information on the selected table.';
/**
* the Command's usage
*
* @var string
*/
protected $usage = <<<'EOL'
db:table [<table_name>] [options]
Examples:
db:table --show
db:table --metadata
db:table my_table --metadata
db:table my_table
db:table my_table --limit-rows 5 --limit-field-value 10 --desc
EOL;
/**
* The Command's arguments
*
* @var array<string, string>
*/
protected $arguments = [
'table_name' => 'The table name to show info',
];
/**
* The Command's options
*
* @var array<string, string>
*/
protected $options = [
'--show' => 'Lists the names of all database tables.',
'--metadata' => 'Retrieves list containing field information.',
'--desc' => 'Sorts the table rows in DESC order.',
'--limit-rows' => 'Limits the number of rows. Default: 10.',
'--limit-field-value' => 'Limits the length of field values. Default: 15.',
];
/**
* @phpstan-var list<list<string|int>> Table Data.
*/
private array $tbody;
private BaseConnection $db;
/**
* @var bool Sort the table rows in DESC order or not.
*/
private bool $sortDesc = false;
private string $DBPrefix;
public function run(array $params)
{
$this->db = Database::connect();
$this->DBPrefix = $this->db->getPrefix();
$tables = $this->db->listTables();
if (array_key_exists('desc', $params)) {
$this->sortDesc = true;
}
if ($tables === []) {
CLI::error('Database has no tables!', 'light_gray', 'red');
CLI::newLine();
return;
}
if (array_key_exists('show', $params)) {
$this->showAllTables($tables);
return;
}
$tableName = $params[0] ?? null;
$limitRows = (int) ($params['limit-rows'] ?? 10);
$limitFieldValue = (int) ($params['limit-field-value'] ?? 15);
if (! in_array($tableName, $tables, true)) {
$tableNameNo = CLI::promptByKey(
['Here is the list of your database tables:', 'Which table do you want to see?'],
$tables,
'required'
);
CLI::newLine();
$tableName = $tables[$tableNameNo];
}
if (array_key_exists('metadata', $params)) {
$this->showFieldMetaData($tableName);
return;
}
$this->showDataOfTable($tableName, $limitRows, $limitFieldValue);
}
private function removeDBPrefix(): void
{
$this->db->setPrefix('');
}
private function restoreDBPrefix(): void
{
$this->db->setPrefix($this->DBPrefix);
}
private function showDataOfTable(string $tableName, int $limitRows, int $limitFieldValue)
{
CLI::write("Data of Table \"{$tableName}\":", 'black', 'yellow');
CLI::newLine();
$this->removeDBPrefix();
$thead = $this->db->getFieldNames($tableName);
$this->restoreDBPrefix();
// If there is a field named `id`, sort by it.
$sortField = null;
if (in_array('id', $thead, true)) {
$sortField = 'id';
}
$this->tbody = $this->makeTableRows($tableName, $limitRows, $limitFieldValue, $sortField);
CLI::table($this->tbody, $thead);
}
private function showAllTables(array $tables)
{
CLI::write('The following is a list of the names of all database tables:', 'black', 'yellow');
CLI::newLine();
$thead = ['ID', 'Table Name', 'Num of Rows', 'Num of Fields'];
$this->tbody = $this->makeTbodyForShowAllTables($tables);
CLI::table($this->tbody, $thead);
CLI::newLine();
}
private function makeTbodyForShowAllTables(array $tables): array
{
$this->removeDBPrefix();
foreach ($tables as $id => $tableName) {
$table = $this->db->protectIdentifiers($tableName);
$db = $this->db->query("SELECT * FROM {$table}");
$this->tbody[] = [
$id + 1,
$tableName,
$db->getNumRows(),
$db->getFieldCount(),
];
}
$this->restoreDBPrefix();
if ($this->sortDesc) {
krsort($this->tbody);
}
return $this->tbody;
}
private function makeTableRows(
string $tableName,
int $limitRows,
int $limitFieldValue,
?string $sortField = null
): array {
$this->tbody = [];
$this->removeDBPrefix();
$builder = $this->db->table($tableName);
$builder->limit($limitRows);
if ($sortField !== null) {
$builder->orderBy($sortField, $this->sortDesc ? 'DESC' : 'ASC');
}
$rows = $builder->get()->getResultArray();
$this->restoreDBPrefix();
foreach ($rows as $row) {
$row = array_map(
static fn ($item): string => mb_strlen((string) $item) > $limitFieldValue
? mb_substr((string) $item, 0, $limitFieldValue) . '...'
: (string) $item,
$row
);
$this->tbody[] = $row;
}
if ($sortField === null && $this->sortDesc) {
krsort($this->tbody);
}
return $this->tbody;
}
private function showFieldMetaData(string $tableName): void
{
CLI::write("List of Metadata Information in Table \"{$tableName}\":", 'black', 'yellow');
CLI::newLine();
$thead = ['Field Name', 'Type', 'Max Length', 'Nullable', 'Default', 'Primary Key'];
$this->removeDBPrefix();
$fields = $this->db->getFieldData($tableName);
$this->restoreDBPrefix();
foreach ($fields as $row) {
$this->tbody[] = [
$row->name,
$row->type,
$row->max_length,
isset($row->nullable) ? $this->setYesOrNo($row->nullable) : 'n/a',
$row->default,
isset($row->primary_key) ? $this->setYesOrNo($row->primary_key) : 'n/a',
];
}
if ($this->sortDesc) {
krsort($this->tbody);
}
CLI::table($this->tbody, $thead);
}
private function setYesOrNo(bool $fieldValue): string
{
if ($fieldValue) {
return CLI::color('Yes', 'green');
}
return CLI::color('No', 'red');
}
}
+188
View 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 (! is_file($envFile)) {
if (! is_file($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";
}
}
@@ -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]
);
}
}
@@ -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);
}
}
@@ -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;
use CodeIgniter\Controller;
use CodeIgniter\RESTful\ResourceController;
use CodeIgniter\RESTful\ResourcePresenter;
/**
* 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 = Controller::class;
$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 = ResourceController::class;
$extends = 'ResourceController';
} elseif ($rest === 'presenter') {
$useStatement = ResourcePresenter::class;
$extends = 'ResourcePresenter';
}
}
}
return $this->parseTemplate(
$class,
['{useStatement}', '{extends}'],
[$useStatement, $extends],
['type' => $rest]
);
}
}
@@ -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);
}
}
@@ -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);
}
}
@@ -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] ??= CLI::getSegment(2);
$params['namespace'] ??= CLI::getOption('namespace') ?? APP_NAMESPACE;
if (array_key_exists('force', $params) || CLI::getOption('force')) {
$params['force'] = null;
}
$this->call('make:migration', $params);
}
}
@@ -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);
}
}
@@ -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]);
}
}
@@ -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));
}
}
@@ -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);
}
}
@@ -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);
}
}
@@ -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);
}
}
@@ -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 ?>
}
}
@@ -0,0 +1,10 @@
<@php
namespace {namespace};
use CodeIgniter\Config\BaseConfig;
class {class} extends BaseConfig
{
//
}
@@ -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 ?>
}
@@ -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 = [];
}
@@ -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)
{
//
}
}
@@ -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 ?>
}
@@ -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 = [];
}
@@ -0,0 +1,13 @@
<@php
namespace {namespace};
use CodeIgniter\Database\Seeder;
class {class} extends Seeder
{
public function run()
{
//
}
}
@@ -0,0 +1,11 @@
<@php
namespace {namespace};
class {class}
{
// public function custom_rule(): bool
// {
// return true;
// }
}
+85
View 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 ??= 'help';
$commands = $this->commands->getCommands();
if (! $this->commands->verifyCommand($command, $commands)) {
return;
}
$class = new $commands[$command]['class']($this->logger, $this->commands);
$class->showHelp();
}
}
@@ -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();
}
}
@@ -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
View 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
View 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);
}
}
}
+47
View File
@@ -0,0 +1,47 @@
<?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));
// All request handle by index.php file.
$_SERVER['SCRIPT_NAME'] = '/index.php';
// 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
View 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 array $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
View 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
View 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();
}
}
}
}
}
+159
View File
@@ -0,0 +1,159 @@
<?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 Closure;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\Commands\Utilities\Routes\AutoRouteCollector;
use CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\AutoRouteCollector as AutoRouteCollectorImproved;
use CodeIgniter\Commands\Utilities\Routes\FilterCollector;
use CodeIgniter\Commands\Utilities\Routes\SampleURIGenerator;
use Config\Services;
/**
* Lists all the routes. This will include any Routes files
* that can be discovered, and will include routes that are not defined
* in routes files, 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 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 = [];
$uriGenerator = new SampleURIGenerator();
$filterCollector = new FilterCollector();
foreach ($methods as $method) {
$routes = $collection->getRoutes($method);
foreach ($routes as $route => $handler) {
if (is_string($handler) || $handler instanceof Closure) {
$sampleUri = $uriGenerator->get($route);
$filters = $filterCollector->get($method, $sampleUri);
$tbody[] = [
strtoupper($method),
$route,
is_string($handler) ? $handler : '(Closure)',
implode(' ', array_map('class_basename', $filters['before'])),
implode(' ', array_map('class_basename', $filters['after'])),
];
}
}
}
if ($collection->shouldAutoRoute()) {
$autoRoutesImproved = config('Feature')->autoRoutesImproved ?? false;
if ($autoRoutesImproved) {
$autoRouteCollector = new AutoRouteCollectorImproved(
$collection->getDefaultNamespace(),
$collection->getDefaultController(),
$collection->getDefaultMethod(),
$methods,
$collection->getRegisteredControllers('*')
);
$autoRoutes = $autoRouteCollector->get();
} else {
$autoRouteCollector = new AutoRouteCollector(
$collection->getDefaultNamespace(),
$collection->getDefaultController(),
$collection->getDefaultMethod()
);
$autoRoutes = $autoRouteCollector->get();
foreach ($autoRoutes as &$routes) {
// There is no `auto` method, but it is intentional not to get route filters.
$filters = $filterCollector->get('auto', $uriGenerator->get($routes[1]));
$routes[] = implode(' ', array_map('class_basename', $filters['before']));
$routes[] = implode(' ', array_map('class_basename', $filters['after']));
}
}
$tbody = [...$tbody, ...$autoRoutes];
}
$thead = [
'Method',
'Route',
'Handler',
'Before Filters',
'After Filters',
];
CLI::table($tbody, $thead);
}
}
@@ -0,0 +1,66 @@
<?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\Routes;
/**
* Collects data for auto route listing.
*/
final class AutoRouteCollector
{
/**
* @var string namespace to search
*/
private string $namespace;
private string $defaultController;
private string $defaultMethod;
/**
* @param string $namespace namespace to search
*/
public function __construct(string $namespace, string $defaultController, string $defaultMethod)
{
$this->namespace = $namespace;
$this->defaultController = $defaultController;
$this->defaultMethod = $defaultMethod;
}
/**
* @return array<int, array<int, string>>
* @phpstan-return list<list<string>>
*/
public function get(): array
{
$finder = new ControllerFinder($this->namespace);
$reader = new ControllerMethodReader($this->namespace);
$tbody = [];
foreach ($finder->find() as $class) {
$output = $reader->read(
$class,
$this->defaultController,
$this->defaultMethod
);
foreach ($output as $item) {
$tbody[] = [
'auto',
$item['route'],
$item['handler'],
];
}
}
return $tbody;
}
}
@@ -0,0 +1,138 @@
<?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\Routes\AutoRouterImproved;
use CodeIgniter\Commands\Utilities\Routes\ControllerFinder;
use CodeIgniter\Commands\Utilities\Routes\FilterCollector;
/**
* Collects data for Auto Routing Improved.
*/
final class AutoRouteCollector
{
/**
* @var string namespace to search
*/
private string $namespace;
private string $defaultController;
private string $defaultMethod;
private array $httpMethods;
/**
* List of controllers in Defined Routes that should not be accessed via Auto-Routing.
*
* @var class-string[]
*/
private array $protectedControllers;
/**
* @param string $namespace namespace to search
*/
public function __construct(
string $namespace,
string $defaultController,
string $defaultMethod,
array $httpMethods,
array $protectedControllers
) {
$this->namespace = $namespace;
$this->defaultController = $defaultController;
$this->defaultMethod = $defaultMethod;
$this->httpMethods = $httpMethods;
$this->protectedControllers = $protectedControllers;
}
/**
* @return array<int, array<int, string>>
* @phpstan-return list<list<string>>
*/
public function get(): array
{
$finder = new ControllerFinder($this->namespace);
$reader = new ControllerMethodReader($this->namespace, $this->httpMethods);
$tbody = [];
foreach ($finder->find() as $class) {
// Exclude controllers in Defined Routes.
if (in_array('\\' . $class, $this->protectedControllers, true)) {
continue;
}
$routes = $reader->read(
$class,
$this->defaultController,
$this->defaultMethod
);
if ($routes === []) {
continue;
}
$routes = $this->addFilters($routes);
foreach ($routes as $item) {
$tbody[] = [
strtoupper($item['method']) . '(auto)',
$item['route'] . $item['route_params'],
$item['handler'],
$item['before'],
$item['after'],
];
}
}
return $tbody;
}
private function addFilters($routes)
{
$filterCollector = new FilterCollector(true);
foreach ($routes as &$route) {
// Search filters for the URI with all params
$sampleUri = $this->generateSampleUri($route);
$filtersLongest = $filterCollector->get($route['method'], $route['route'] . $sampleUri);
// Search filters for the URI without optional params
$sampleUri = $this->generateSampleUri($route, false);
$filtersShortest = $filterCollector->get($route['method'], $route['route'] . $sampleUri);
// Get common array elements
$filters['before'] = array_intersect($filtersLongest['before'], $filtersShortest['before']);
$filters['after'] = array_intersect($filtersLongest['after'], $filtersShortest['after']);
$route['before'] = implode(' ', array_map('class_basename', $filters['before']));
$route['after'] = implode(' ', array_map('class_basename', $filters['after']));
}
return $routes;
}
private function generateSampleUri(array $route, bool $longest = true): string
{
$sampleUri = '';
if (isset($route['params'])) {
$i = 1;
foreach ($route['params'] as $required) {
if ($longest && ! $required) {
$sampleUri .= '/' . $i++;
}
}
}
return $sampleUri;
}
}
@@ -0,0 +1,182 @@
<?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\Routes\AutoRouterImproved;
use ReflectionClass;
use ReflectionMethod;
/**
* Reads a controller and returns a list of auto route listing.
*/
final class ControllerMethodReader
{
/**
* @var string the default namespace
*/
private string $namespace;
private array $httpMethods;
/**
* @param string $namespace the default namespace
*/
public function __construct(string $namespace, array $httpMethods)
{
$this->namespace = $namespace;
$this->httpMethods = $httpMethods;
}
/**
* Returns found route info in the controller.
*
* @phpstan-param class-string $class
*
* @return array<int, array<string, array|string>>
* @phpstan-return list<array<string, string|array>>
*/
public function read(string $class, string $defaultController = 'Home', string $defaultMethod = 'index'): array
{
$reflection = new ReflectionClass($class);
if ($reflection->isAbstract()) {
return [];
}
$classname = $reflection->getName();
$classShortname = $reflection->getShortName();
$output = [];
$classInUri = $this->getUriByClass($classname);
foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
$methodName = $method->getName();
foreach ($this->httpMethods as $httpVerb) {
if (strpos($methodName, $httpVerb) === 0) {
// Remove HTTP verb prefix.
$methodInUri = lcfirst(substr($methodName, strlen($httpVerb)));
if ($methodInUri === $defaultMethod) {
$routeWithoutController = $this->getRouteWithoutController(
$classShortname,
$defaultController,
$classInUri,
$classname,
$methodName,
$httpVerb
);
if ($routeWithoutController !== []) {
$output = [...$output, ...$routeWithoutController];
continue;
}
// Route for the default method.
$output[] = [
'method' => $httpVerb,
'route' => $classInUri,
'route_params' => '',
'handler' => '\\' . $classname . '::' . $methodName,
'params' => [],
];
continue;
}
$route = $classInUri . '/' . $methodInUri;
$params = [];
$routeParams = '';
$refParams = $method->getParameters();
foreach ($refParams as $param) {
$required = true;
if ($param->isOptional()) {
$required = false;
$routeParams .= '[/..]';
} else {
$routeParams .= '/..';
}
// [variable_name => required?]
$params[$param->getName()] = $required;
}
$output[] = [
'method' => $httpVerb,
'route' => $route,
'route_params' => $routeParams,
'handler' => '\\' . $classname . '::' . $methodName,
'params' => $params,
];
}
}
}
return $output;
}
/**
* @phpstan-param class-string $classname
*
* @return string URI path part from the folder(s) and controller
*/
private function getUriByClass(string $classname): string
{
// remove the namespace
$pattern = '/' . preg_quote($this->namespace, '/') . '/';
$class = ltrim(preg_replace($pattern, '', $classname), '\\');
$classParts = explode('\\', $class);
$classPath = '';
foreach ($classParts as $part) {
// make the first letter lowercase, because auto routing makes
// the URI path's first letter uppercase and search the controller
$classPath .= lcfirst($part) . '/';
}
return rtrim($classPath, '/');
}
/**
* Gets a route without default controller.
*/
private function getRouteWithoutController(
string $classShortname,
string $defaultController,
string $uriByClass,
string $classname,
string $methodName,
string $httpVerb
): array {
$output = [];
if ($classShortname === $defaultController) {
$pattern = '#' . preg_quote(lcfirst($defaultController), '#') . '\z#';
$routeWithoutController = rtrim(preg_replace($pattern, '', $uriByClass), '/');
$routeWithoutController = $routeWithoutController ?: '/';
$output[] = [
'method' => $httpVerb,
'route' => $routeWithoutController,
'route_params' => '',
'handler' => '\\' . $classname . '::' . $methodName,
'params' => [],
];
}
return $output;
}
}
@@ -0,0 +1,76 @@
<?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\Routes;
use CodeIgniter\Autoloader\FileLocator;
use CodeIgniter\Config\Services;
/**
* Finds all controllers in a namespace for auto route listing.
*/
final class ControllerFinder
{
/**
* @var string namespace to search
*/
private string $namespace;
private FileLocator $locator;
/**
* @param string $namespace namespace to search
*/
public function __construct(string $namespace)
{
$this->namespace = $namespace;
$this->locator = Services::locator();
}
/**
* @return string[]
* @phpstan-return class-string[]
*/
public function find(): array
{
$nsArray = explode('\\', trim($this->namespace, '\\'));
$count = count($nsArray);
$ns = '';
for ($i = 0; $i < $count; $i++) {
$ns .= '\\' . array_shift($nsArray);
$path = implode('\\', $nsArray);
$files = $this->locator->listNamespaceFiles($ns, $path);
if ($files !== []) {
break;
}
}
$classes = [];
foreach ($files as $file) {
if (\is_file($file)) {
$classnameOrEmpty = $this->locator->getClassname($file);
if ($classnameOrEmpty !== '') {
/** @phpstan-var class-string $classname */
$classname = $classnameOrEmpty;
$classes[] = $classname;
}
}
}
return $classes;
}
}
@@ -0,0 +1,176 @@
<?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\Routes;
use ReflectionClass;
use ReflectionMethod;
/**
* Reads a controller and returns a list of auto route listing.
*/
final class ControllerMethodReader
{
/**
* @var string the default namespace
*/
private string $namespace;
/**
* @param string $namespace the default namespace
*/
public function __construct(string $namespace)
{
$this->namespace = $namespace;
}
/**
* @phpstan-param class-string $class
*
* @return array<int, array{route: string, handler: string}>
* @phpstan-return list<array{route: string, handler: string}>
*/
public function read(string $class, string $defaultController = 'Home', string $defaultMethod = 'index'): array
{
$reflection = new ReflectionClass($class);
if ($reflection->isAbstract()) {
return [];
}
$classname = $reflection->getName();
$classShortname = $reflection->getShortName();
$output = [];
$uriByClass = $this->getUriByClass($classname);
if ($this->hasRemap($reflection)) {
$methodName = '_remap';
$routeWithoutController = $this->getRouteWithoutController(
$classShortname,
$defaultController,
$uriByClass,
$classname,
$methodName
);
$output = [...$output, ...$routeWithoutController];
$output[] = [
'route' => $uriByClass . '[/...]',
'handler' => '\\' . $classname . '::' . $methodName,
];
return $output;
}
foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
$methodName = $method->getName();
$route = $uriByClass . '/' . $methodName;
// Exclude BaseController and initController
// See system/Config/Routes.php
if (preg_match('#\AbaseController.*#', $route) === 1) {
continue;
}
if (preg_match('#.*/initController\z#', $route) === 1) {
continue;
}
if ($methodName === $defaultMethod) {
$routeWithoutController = $this->getRouteWithoutController(
$classShortname,
$defaultController,
$uriByClass,
$classname,
$methodName
);
$output = [...$output, ...$routeWithoutController];
$output[] = [
'route' => $uriByClass,
'handler' => '\\' . $classname . '::' . $methodName,
];
}
$output[] = [
'route' => $route . '[/...]',
'handler' => '\\' . $classname . '::' . $methodName,
];
}
return $output;
}
/**
* Whether the class has a _remap() method.
*/
private function hasRemap(ReflectionClass $class): bool
{
if ($class->hasMethod('_remap')) {
$remap = $class->getMethod('_remap');
return $remap->isPublic();
}
return false;
}
/**
* @phpstan-param class-string $classname
*
* @return string URI path part from the folder(s) and controller
*/
private function getUriByClass(string $classname): string
{
// remove the namespace
$pattern = '/' . preg_quote($this->namespace, '/') . '/';
$class = ltrim(preg_replace($pattern, '', $classname), '\\');
$classParts = explode('\\', $class);
$classPath = '';
foreach ($classParts as $part) {
// make the first letter lowercase, because auto routing makes
// the URI path's first letter uppercase and search the controller
$classPath .= lcfirst($part) . '/';
}
return rtrim($classPath, '/');
}
/**
* Gets a route without default controller.
*/
private function getRouteWithoutController(
string $classShortname,
string $defaultController,
string $uriByClass,
string $classname,
string $methodName
): array {
$output = [];
if ($classShortname === $defaultController) {
$pattern = '#' . preg_quote(lcfirst($defaultController), '#') . '\z#';
$routeWithoutController = rtrim(preg_replace($pattern, '', $uriByClass), '/');
$routeWithoutController = $routeWithoutController ?: '/';
$output[] = [
'route' => $routeWithoutController,
'handler' => '\\' . $classname . '::' . $methodName,
];
}
return $output;
}
}
@@ -0,0 +1,79 @@
<?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\Routes;
use CodeIgniter\Config\Services;
use CodeIgniter\Filters\Filters;
use CodeIgniter\HTTP\Request;
use CodeIgniter\Router\Router;
/**
* Collects filters for a route.
*/
final class FilterCollector
{
/**
* Whether to reset Defined Routes.
*
* If set to true, route filters are not found.
*/
private bool $resetRoutes;
public function __construct(bool $resetRoutes = false)
{
$this->resetRoutes = $resetRoutes;
}
/**
* @param string $method HTTP method
* @param string $uri URI path to find filters for
*
* @return array{before: list<string>, after: list<string>} array of filter alias or classname
*/
public function get(string $method, string $uri): array
{
if ($method === 'cli') {
return [
'before' => [],
'after' => [],
];
}
$request = Services::request(null, false);
$request->setMethod($method);
$router = $this->createRouter($request);
$filters = $this->createFilters($request);
$finder = new FilterFinder($router, $filters);
return $finder->find($uri);
}
private function createRouter(Request $request): Router
{
$routes = Services::routes();
if ($this->resetRoutes) {
$routes->resetRoutes();
}
return new Router($routes, $request);
}
private function createFilters(Request $request): Filters
{
$config = config('Filters');
return new Filters($config, $request, Services::response());
}
}
@@ -0,0 +1,78 @@
<?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\Routes;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\Filters\Filters;
use CodeIgniter\Router\Exceptions\RedirectException;
use CodeIgniter\Router\Router;
use Config\Services;
/**
* Finds filters.
*/
final class FilterFinder
{
private Router $router;
private Filters $filters;
public function __construct(?Router $router = null, ?Filters $filters = null)
{
$this->router = $router ?? Services::router();
$this->filters = $filters ?? Services::filters();
}
private function getRouteFilters(string $uri): array
{
$this->router->handle($uri);
$multipleFiltersEnabled = config('Feature')->multipleFilters ?? false;
if (! $multipleFiltersEnabled) {
$filter = $this->router->getFilter();
return $filter === null ? [] : [$filter];
}
return $this->router->getFilters();
}
/**
* @param string $uri URI path to find filters for
*
* @return array{before: list<string>, after: list<string>} array of filter alias or classname
*/
public function find(string $uri): array
{
$this->filters->reset();
// Add route filters
try {
$routeFilters = $this->getRouteFilters($uri);
$this->filters->enableFilters($routeFilters, 'before');
$this->filters->enableFilters($routeFilters, 'after');
$this->filters->initialize($uri);
return $this->filters->getFilters();
} catch (RedirectException $e) {
return [
'before' => [],
'after' => [],
];
} catch (PageNotFoundException $e) {
return [
'before' => ['<unknown>'],
'after' => ['<unknown>'],
];
}
}
}
@@ -0,0 +1,61 @@
<?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\Routes;
use CodeIgniter\Config\Services;
use CodeIgniter\Router\RouteCollection;
/**
* Generate a sample URI path from route key regex.
*/
final class SampleURIGenerator
{
private RouteCollection $routes;
/**
* Sample URI path for placeholder.
*
* @var array<string, string>
*/
private array $samples = [
'any' => '123/abc',
'segment' => 'abc_123',
'alphanum' => 'abc123',
'num' => '123',
'alpha' => 'abc',
'hash' => 'abc_123',
];
public function __construct(?RouteCollection $routes = null)
{
$this->routes = $routes ?? Services::routes();
}
/**
* @param string $routeKey route key regex
*
* @return string sample URI path
*/
public function get(string $routeKey): string
{
$sampleUri = $routeKey;
foreach ($this->routes->getPlaceholders() as $placeholder => $regex) {
$sample = $this->samples[$placeholder] ?? '::unknown::';
$sampleUri = str_replace('(' . $regex . ')', $sample, $sampleUri);
}
// auto route
return str_replace('[/...]', '/1/2/3/4/5', $sampleUri);
}
}