Skip to content

Commit 1b8e5d7

Browse files
committed
add some cli tools
1 parent 632ba04 commit 1b8e5d7

File tree

15 files changed

+250
-31
lines changed

15 files changed

+250
-31
lines changed

application/config/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@
326326
| https://codeigniter.com/userguide3/libraries/encryption.html
327327
|
328328
*/
329-
$config['encryption_key'] = '<encryption_key>';
329+
$config['encryption_key'] = hex2bin('');
330330

331331
/*
332332
|--------------------------------------------------------------------------

application/config/hooks.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
| https://codeigniter.com/userguide3/general/hooks.html
1212
|
1313
*/
14-
$hook['pre_system'] = function()
15-
{
16-
if (ENVIRONMENT === 'maintenance')
17-
{
18-
show_error('Service Unavailable', 503);
19-
}
20-
};
14+
$hook['pre_controller'][] = array(
15+
'class' => 'Maintenance',
16+
'function' => 'run',
17+
'filename' => 'Maintenance.php',
18+
'filepath' => 'hooks'
19+
);

application/config/migration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
| be upgraded / downgraded to.
7070
|
7171
*/
72-
$config['migration_version'] = '<version>';
72+
$config['migration_version'] = 0;
7373

7474
/*
7575
|--------------------------------------------------------------------------

application/controllers/ArX.php

Lines changed: 131 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function controller($name)
1919
{
2020
$name = ucfirst($name);
2121

22-
if ( ! file_exists(APPPATH . "controllers/{$name}.php"))
22+
if (file_exists(APPPATH . "controllers/{$name}.php") === FALSE)
2323
{
2424
$template = read_file(APPPATH . 'templates/Controller.php');
2525
$template = str_replace('<controller>', $name, $template);
@@ -28,7 +28,39 @@ public function controller($name)
2828
}
2929
else
3030
{
31-
echo "\n{$name} controller already exists\n\n";
31+
echo "n\"{$name}\" controller already exists\n\n";
32+
}
33+
}
34+
35+
public function config($name)
36+
{
37+
if (file_exists(APPPATH . "config/{$name}.php") === FALSE)
38+
{
39+
$template = read_file(APPPATH . 'templates/Config.php');
40+
41+
write_file(APPPATH . "config/{$name}.php", $template);
42+
}
43+
else
44+
{
45+
$name = ucfirst($name);
46+
echo "\n\"{$name}\" config already exists\n\n";
47+
}
48+
}
49+
50+
public function core($name)
51+
{
52+
$name = ucfirst($name);
53+
54+
if (file_exists(BASEPATH . "core/{$name}.php") === FALSE AND file_exists(APPPATH . "core/{$name}.php") === FALSE)
55+
{
56+
$template = read_file(APPPATH . 'templates/Core.php');
57+
$template = str_replace('<core>', $name, $template);
58+
59+
write_file(APPPATH . "core/{$name}.php", $template);
60+
}
61+
else
62+
{
63+
echo "\n\"{$name}\" core already exists\n\n";
3264
}
3365
}
3466

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

3971
$this->dbforge->create_database($name);
72+
73+
$database = read_file(APPPATH . 'config/database.php');
74+
$database = preg_replace("/'database' => '.*'/", "'database' => '{$name}'", $database);
75+
76+
write_file(APPPATH . 'config/database.php', $database);
77+
}
78+
79+
public function environment($env)
80+
{
81+
if (in_array($env, array('development', 'testing', 'production', 'maintenance')) === TRUE)
82+
{
83+
$htaccess = read_file(FCPATH . '.htaccess');
84+
$htaccess = preg_replace("/SetEnv CI_ENV (development|testing|production|maintenance)/", "SetEnv CI_ENV {$env}", $htaccess);
85+
86+
write_file(FCPATH . '.htaccess', $htaccess);
87+
}
88+
else
89+
{
90+
$env = ucfirst($env);
91+
echo "\n\"{$env}\" environment not found\n\n";
92+
}
4093
}
4194

4295
public function helper($name)
4396
{
44-
if ( ! file_exists(APPPATH . "helpers/{$name}_helper.php"))
97+
if (file_exists(BASEPATH . "helpers/{$name}_helper.php") === FALSE AND file_exists(APPPATH . "helpers/{$name}_helper.php") === FALSE)
4598
{
4699
$template = read_file(APPPATH . 'templates/Helper.php');
47100

@@ -50,7 +103,24 @@ public function helper($name)
50103
else
51104
{
52105
$name = ucfirst($name);
53-
echo "\n{$name} helper already exists\n\n";
106+
echo "\n\"{$name}\" helper already exists\n\n";
107+
}
108+
}
109+
110+
public function hook($name)
111+
{
112+
$name = ucfirst($name);
113+
114+
if (file_exists(APPPATH . "hooks/{$name}.php") === FALSE)
115+
{
116+
$template = read_file(APPPATH . 'templates/Hook.php');
117+
$template = str_replace('<hook>', $name, $template);
118+
119+
write_file(APPPATH . "hooks/{$name}.php", $template);
120+
}
121+
else
122+
{
123+
echo "\n\"{$name}\" hook already exists\n\n";
54124
}
55125
}
56126

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

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

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

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

141+
public function language($name)
142+
{
143+
$localization = config_item('language');
144+
145+
if (file_exists(APPPATH . "language/{$localization}/{$name}_lang.php") === FALSE)
146+
{
147+
$template = read_file(APPPATH . 'templates/Language.php');
148+
$template = str_replace('<language>', $name, $template);
149+
150+
write_file(APPPATH . "language/{$localization}/{$name}_lang.php", $template);
151+
}
152+
else
153+
{
154+
$name = ucfirst($name);
155+
echo "\n\"{$name}\" language already exists\n\n";
156+
}
157+
}
158+
71159
public function library($name)
72160
{
73161
$name = ucfirst($name);
74162

75-
if ( ! file_exists(APPPATH . "libraries/{$name}.php"))
163+
if (file_exists(BASEPATH . "libraries/{$name}.php") === FALSE AND file_exists(APPPATH . "libraries/{$name}.php") === FALSE)
76164
{
77165
$template = read_file(APPPATH . 'templates/Library.php');
78166
$template = str_replace('<library>', $name, $template);
@@ -81,7 +169,7 @@ public function library($name)
81169
}
82170
else
83171
{
84-
echo "\n{$name} library already exists\n\n";
172+
echo "\n\"{$name}\" library already exists\n\n";
85173
}
86174
}
87175

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

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

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

118206
write_file(APPPATH . 'config/migration.php', $migration);
119207
}
120208
else
121209
{
122210
$name = ucfirst(str_replace('_', ' ', $name));
123-
echo "\n{$name} migration already exists\n\n";
211+
echo "\n\"{$name}\" migration already exists\n\n";
124212
}
125213
}
126214

127215
public function model($name)
128216
{
129217
$name = ucfirst($name);
130218

131-
if ( ! file_exists(APPPATH . "models/{$name}_model.php"))
219+
if (file_exists(APPPATH . "models/{$name}_model.php") === FALSE)
132220
{
133221
$template = read_file(APPPATH . 'templates/Model.php');
134222
$template = str_replace('<model>', $name, $template);
@@ -137,17 +225,45 @@ public function model($name)
137225
}
138226
else
139227
{
140-
echo "\n{$name} model already exists\n\n";
228+
echo "\n\"{$name}\" model already exists\n\n";
141229
}
142230
}
143231

144232
public function seed()
145233
{
146-
return;
234+
$this->load->library('seeder');
235+
236+
$seeders = get_filenames(APPPATH . 'seeders/');
237+
238+
foreach ($seeders as $seeder)
239+
{
240+
if (str_contains($seeder, '.php'))
241+
{
242+
$name = str_replace('.php', '', $seeder);
243+
244+
$this->seeder->call($name);
245+
}
246+
}
147247
}
148248

149-
public function seeder()
249+
public function seeder($name)
150250
{
151-
return;
251+
$name = ucfirst($name);
252+
253+
if (file_exists(APPPATH . "seeders/{$name}_seeder.php") === FALSE)
254+
{
255+
$template = read_file(APPPATH . 'templates/Seeder.php');
256+
$template = str_replace(
257+
array('<seeder>', '<table>'),
258+
array($name, strtolower($name)),
259+
$template
260+
);
261+
262+
write_file(APPPATH . "seeders/{$name}_seeder.php", $template);
263+
}
264+
else
265+
{
266+
echo "\n\"{$name}\" seeder already exists\n\n";
267+
}
152268
}
153269
}

application/hooks/Maintenance.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
4+
class Maintenance {
5+
6+
public function run()
7+
{
8+
if (ENVIRONMENT === 'maintenance')
9+
{
10+
show_error('Service Unavailable', 503);
11+
}
12+
}
13+
}

application/libraries/Seeder.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
4+
class Seeder {
5+
6+
protected $CI;
7+
8+
public function __construct()
9+
{
10+
$this->CI =& get_instance();
11+
}
12+
13+
public function call($seeder)
14+
{
15+
require_once APPPATH . "seeders/{$seeder}.php";
16+
17+
$seed = new $seeder();
18+
$seed->run();
19+
}
20+
}

application/templates/Config.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
4+
$config[''] = ;

application/templates/Core.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
4+
#[\AllowDynamicProperties]
5+
6+
class CI_<core> {
7+
8+
9+
}

application/templates/Helper.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
<?php
22
defined('BASEPATH') OR exit('No direct script access allowed');
3+
4+
if ( ! function_exists(''))
5+
{
6+
function () {
7+
8+
}
9+
}

application/templates/Hook.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
4+
class <hook> {
5+
6+
protected $CI;
7+
8+
public function __construct()
9+
{
10+
$this->CI =& get_instance();
11+
}
12+
13+
public function ($params = NULL)
14+
{
15+
16+
}
17+
}

0 commit comments

Comments
 (0)