Skip to content

Commit

Permalink
add some cli tools
Browse files Browse the repository at this point in the history
  • Loading branch information
arxcode40 committed Sep 26, 2024
1 parent 632ba04 commit 1b8e5d7
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 31 deletions.
2 changes: 1 addition & 1 deletion application/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
| https://codeigniter.com/userguide3/libraries/encryption.html
|
*/
$config['encryption_key'] = '<encryption_key>';
$config['encryption_key'] = hex2bin('');

/*
|--------------------------------------------------------------------------
Expand Down
13 changes: 6 additions & 7 deletions application/config/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
| https://codeigniter.com/userguide3/general/hooks.html
|
*/
$hook['pre_system'] = function()
{
if (ENVIRONMENT === 'maintenance')
{
show_error('Service Unavailable', 503);
}
};
$hook['pre_controller'][] = array(
'class' => 'Maintenance',
'function' => 'run',
'filename' => 'Maintenance.php',
'filepath' => 'hooks'
);
2 changes: 1 addition & 1 deletion application/config/migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
| be upgraded / downgraded to.
|
*/
$config['migration_version'] = '<version>';
$config['migration_version'] = 0;

/*
|--------------------------------------------------------------------------
Expand Down
146 changes: 131 additions & 15 deletions application/controllers/ArX.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function controller($name)
{
$name = ucfirst($name);

if ( ! file_exists(APPPATH . "controllers/{$name}.php"))
if (file_exists(APPPATH . "controllers/{$name}.php") === FALSE)
{
$template = read_file(APPPATH . 'templates/Controller.php');
$template = str_replace('<controller>', $name, $template);
Expand All @@ -28,7 +28,39 @@ public function controller($name)
}
else
{
echo "\n{$name} controller already exists\n\n";
echo "n\"{$name}\" controller already exists\n\n";
}
}

public function config($name)
{
if (file_exists(APPPATH . "config/{$name}.php") === FALSE)
{
$template = read_file(APPPATH . 'templates/Config.php');

write_file(APPPATH . "config/{$name}.php", $template);
}
else
{
$name = ucfirst($name);
echo "\n\"{$name}\" config already exists\n\n";
}
}

public function core($name)
{
$name = ucfirst($name);

if (file_exists(BASEPATH . "core/{$name}.php") === FALSE AND file_exists(APPPATH . "core/{$name}.php") === FALSE)
{
$template = read_file(APPPATH . 'templates/Core.php');
$template = str_replace('<core>', $name, $template);

write_file(APPPATH . "core/{$name}.php", $template);
}
else
{
echo "\n\"{$name}\" core already exists\n\n";
}
}

Expand All @@ -37,11 +69,32 @@ public function database($name)
$this->load->dbforge();

$this->dbforge->create_database($name);

$database = read_file(APPPATH . 'config/database.php');
$database = preg_replace("/'database' => '.*'/", "'database' => '{$name}'", $database);

write_file(APPPATH . 'config/database.php', $database);
}

public function environment($env)
{
if (in_array($env, array('development', 'testing', 'production', 'maintenance')) === TRUE)
{
$htaccess = read_file(FCPATH . '.htaccess');
$htaccess = preg_replace("/SetEnv CI_ENV (development|testing|production|maintenance)/", "SetEnv CI_ENV {$env}", $htaccess);

write_file(FCPATH . '.htaccess', $htaccess);
}
else
{
$env = ucfirst($env);
echo "\n\"{$env}\" environment not found\n\n";
}
}

public function helper($name)
{
if ( ! file_exists(APPPATH . "helpers/{$name}_helper.php"))
if (file_exists(BASEPATH . "helpers/{$name}_helper.php") === FALSE AND file_exists(APPPATH . "helpers/{$name}_helper.php") === FALSE)
{
$template = read_file(APPPATH . 'templates/Helper.php');

Expand All @@ -50,7 +103,24 @@ public function helper($name)
else
{
$name = ucfirst($name);
echo "\n{$name} helper already exists\n\n";
echo "\n\"{$name}\" helper already exists\n\n";
}
}

public function hook($name)
{
$name = ucfirst($name);

if (file_exists(APPPATH . "hooks/{$name}.php") === FALSE)
{
$template = read_file(APPPATH . 'templates/Hook.php');
$template = str_replace('<hook>', $name, $template);

write_file(APPPATH . "hooks/{$name}.php", $template);
}
else
{
echo "\n\"{$name}\" hook already exists\n\n";
}
}

Expand All @@ -61,18 +131,36 @@ public function key($length = 16)
$encryption_key = bin2hex($this->encryption->create_key($length));

$config = read_file(APPPATH . 'config/config.php');
$config = str_replace("'<encryption_key>'", "bin2hex('{$encryption_key}')", $config);
$config = preg_replace('/\$config\[\'encryption_key\'\] = hex2bin\(\'[0-9a-f]*\'\);/', "\$config['encryption_key'] = hex2bin('{$encryption_key}');", $config);

write_file(APPPATH . 'config/config.php', $config);

echo "\nEncryption key: {$encryption_key}\n\n";
}

public function language($name)
{
$localization = config_item('language');

if (file_exists(APPPATH . "language/{$localization}/{$name}_lang.php") === FALSE)
{
$template = read_file(APPPATH . 'templates/Language.php');
$template = str_replace('<language>', $name, $template);

write_file(APPPATH . "language/{$localization}/{$name}_lang.php", $template);
}
else
{
$name = ucfirst($name);
echo "\n\"{$name}\" language already exists\n\n";
}
}

public function library($name)
{
$name = ucfirst($name);

if ( ! file_exists(APPPATH . "libraries/{$name}.php"))
if (file_exists(BASEPATH . "libraries/{$name}.php") === FALSE AND file_exists(APPPATH . "libraries/{$name}.php") === FALSE)
{
$template = read_file(APPPATH . 'templates/Library.php');
$template = str_replace('<library>', $name, $template);
Expand All @@ -81,7 +169,7 @@ public function library($name)
}
else
{
echo "\n{$name} library already exists\n\n";
echo "\n\"{$name}\" library already exists\n\n";
}
}

Expand All @@ -101,7 +189,7 @@ public function migration($name)
$table = explode('_', $name);
$table = end($table);

if ( ! file_exists(APPPATH . "migrations/{$timestamp}_{$name}.php"))
if (file_exists(APPPATH . "migrations/{$timestamp}_{$name}.php") === FALSE)
{
$template = read_file(APPPATH . 'templates/Migration.php');
$template = str_replace(
Expand All @@ -113,22 +201,22 @@ public function migration($name)
write_file(APPPATH . "migrations/{$timestamp}_{$name}.php", $template);

$migration = read_file(APPPATH . 'config/migration.php');
$migration = str_replace("'<version>'", $timestamp, $migration);
$migration = preg_replace('/(\$config\[\'migration_version\'\] =) \d+;/', '$1 ' . $timestamp . ';', $migration);

write_file(APPPATH . 'config/migration.php', $migration);
}
else
{
$name = ucfirst(str_replace('_', ' ', $name));
echo "\n{$name} migration already exists\n\n";
echo "\n\"{$name}\" migration already exists\n\n";
}
}

public function model($name)
{
$name = ucfirst($name);

if ( ! file_exists(APPPATH . "models/{$name}_model.php"))
if (file_exists(APPPATH . "models/{$name}_model.php") === FALSE)
{
$template = read_file(APPPATH . 'templates/Model.php');
$template = str_replace('<model>', $name, $template);
Expand All @@ -137,17 +225,45 @@ public function model($name)
}
else
{
echo "\n{$name} model already exists\n\n";
echo "\n\"{$name}\" model already exists\n\n";
}
}

public function seed()
{
return;
$this->load->library('seeder');

$seeders = get_filenames(APPPATH . 'seeders/');

foreach ($seeders as $seeder)
{
if (str_contains($seeder, '.php'))
{
$name = str_replace('.php', '', $seeder);

$this->seeder->call($name);
}
}
}

public function seeder()
public function seeder($name)
{
return;
$name = ucfirst($name);

if (file_exists(APPPATH . "seeders/{$name}_seeder.php") === FALSE)
{
$template = read_file(APPPATH . 'templates/Seeder.php');
$template = str_replace(
array('<seeder>', '<table>'),
array($name, strtolower($name)),
$template
);

write_file(APPPATH . "seeders/{$name}_seeder.php", $template);
}
else
{
echo "\n\"{$name}\" seeder already exists\n\n";
}
}
}
13 changes: 13 additions & 0 deletions application/hooks/Maintenance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Maintenance {

public function run()
{
if (ENVIRONMENT === 'maintenance')
{
show_error('Service Unavailable', 503);
}
}
}
20 changes: 20 additions & 0 deletions application/libraries/Seeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Seeder {

protected $CI;

public function __construct()
{
$this->CI =& get_instance();
}

public function call($seeder)
{
require_once APPPATH . "seeders/{$seeder}.php";

$seed = new $seeder();
$seed->run();
}
}
4 changes: 4 additions & 0 deletions application/templates/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$config[''] = ;
9 changes: 9 additions & 0 deletions application/templates/Core.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

#[\AllowDynamicProperties]

class CI_<core> {


}
7 changes: 7 additions & 0 deletions application/templates/Helper.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

if ( ! function_exists(''))
{
function () {

}
}
17 changes: 17 additions & 0 deletions application/templates/Hook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class <hook> {

protected $CI;

public function __construct()
{
$this->CI =& get_instance();
}

public function ($params = NULL)
{

}
}
4 changes: 4 additions & 0 deletions application/templates/Language.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$lang['<language>_'] = '';
5 changes: 5 additions & 0 deletions application/templates/Library.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ public function __construct($params = NULL)
{
$this->CI =& get_instance();
}

public function ()
{

}
}
16 changes: 9 additions & 7 deletions application/templates/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ public function __construct()
}

public function up() {
$this->dbforge->add_field(array(
'id' => array(
'auto_increment' => TRUE,
'type' => 'BIGINT',
'unsigned' => TRUE
)
));
$this->dbforge->add_field(
array(
'id' => array(
'auto_increment' => TRUE,
'type' => 'BIGINT',
'unsigned' => TRUE
)
)
);
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('<table>');
}
Expand Down
Loading

0 comments on commit 1b8e5d7

Please sign in to comment.