Skip to content

Commit 474c984

Browse files
authored
Merge pull request #6 from Simpletine/v1.1.0
Update v1.0.0 to v1.1.0
2 parents e88ef35 + 819ffea commit 474c984

24 files changed

Lines changed: 1859 additions & 253 deletions

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Change Log
2+
3+
## v1.1.0 - 2024-07-13
4+
5+
### Changes
6+
- Renamed command `make:module` to `module:create` for better consistency
7+
8+
### Features
9+
- Added module `config` folder for management of routes within configs ([#5](https://github.com/Simpletine/CodeIgniter4-HMVC/issues/5))
10+
- `module:controller`: Create a controller in a specified module ([#4](https://github.com/Simpletine/CodeIgniter4-HMVC/issues/4))
11+
- `module:model`: Create a model in a specified module ([#3](https://github.com/Simpletine/CodeIgniter4-HMVC/issues/3))
12+
- `module:copy`: Copy an existing module and rename key elements ([#2](https://github.com/Simpletine/CodeIgniter4-HMVC/issues/2))
13+
14+
## v1.0.0 - 2024-06-22
15+
16+
- Initial release
17+
- Implemented `php spark make:module blogs` command for creating new modules called `blogs`

CONTRIBUTE.md

Lines changed: 0 additions & 35 deletions
This file was deleted.

INSTALLING.md

Lines changed: 0 additions & 35 deletions
This file was deleted.

README.md

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,47 @@
1-
# Codeigniter4-HMVC
1+
# Codeigniter 4 HMVC
22

3-
[![Official Website](https://img.shields.io/badge/Official_Website-Visit-yellow)](https://simpletine.com)
4-
[![YouTube Channel](https://img.shields.io/badge/YouTube_Channel-Subscribe-FF0000)](https://www.youtube.com/channel/UCRuDf31rPyyC2PUbsMG0vZw)
3+
[![Official Website](https://img.shields.io/badge/Official_Website-Visit-yellow)](https://simpletine.com) [![YouTube Channel](https://img.shields.io/badge/YouTube_Channel-Subscribe-FF0000)](https://www.youtube.com/channel/UCRuDf31rPyyC2PUbsMG0vZw)
54

65
### Prerequisites
76
1. PHP 7.4 or above
87
2. Composer required
98
2. CodeIgniter 4.4.8
109

1110
### Installation Guide
12-
For the guideline, see the [documentation](/INSTALLING.md)
11+
12+
Clone project to your project root folder
13+
```bash
14+
composer create-project simpletine/codeigniter4-hmvc ci4_hmvc --stability=dev
15+
```
16+
Or
17+
```bash
18+
git clone https://github.com/Simpletine/CodeIgniter4-HMVC.git ci4_hmvc
19+
```
20+
Then
21+
```bash
22+
cd ci4_hmvc
23+
```
24+
25+
Copy some require file to root folder (Upgrading to v4.4.8)
26+
```bash
27+
composer update
28+
cp vendor/codeigniter4/framework/public/index.php public/index.php
29+
cp vendor/codeigniter4/framework/spark spark
30+
```
31+
32+
Copy `env` file
33+
```bash
34+
cp env .env
35+
```
36+
37+
Run the app, using different port, add options `--port=9000`
38+
```bash
39+
php spark serve
40+
```
41+
1342

1443
---
15-
## Generate New Module
44+
## Module Commands
1645
```bash
1746
php spark make:module [module]
1847
```
@@ -25,20 +54,20 @@ php spark make:module blogs
2554

2655
### Result Directory
2756
App
28-
├── Config
29-
│ └── Routes.php (Added group called blogs)
3057
├── Modules
3158
│ └── Blogs
59+
│ ├── Config
60+
│ └── Routes.php
3261
│ ├── Controllers
3362
│ └── Blogs.php
3463
│ ├── Models
35-
│ └── BlogsModel.php
64+
│ └── Blogs.php
3665
│ └── Views
3766
│ └── index.php
3867
└── ...
3968

4069
### Route Group
41-
After generate `Blogs` Module, add a route group for the module at `App\Config\Routes.php`
70+
After generate `Blogs` Module, the routes group for the module at `Modules\Config\Routes.php`
4271
```php
4372
$routes->group(
4473
'blogs', ['namespace' => '\Modules\Blogs\Controllers'], function ($routes) {
@@ -58,7 +87,4 @@ public $psr4 = [
5887
"Blogs" => APPPATH . "Modules/Blogs", // Example
5988
// ...
6089
];
61-
```
62-
---
63-
### Contribute
64-
To contribute to this repository and extend its architectural capabilities or you find an issue, follow these [steps](/CONTRIBUTE.md)
90+
```

app/Commands/ModuleController.php

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
namespace App\Commands;
4+
5+
use CodeIgniter\CLI\BaseCommand;
6+
use CodeIgniter\CLI\CLI;
7+
8+
/**
9+
* Generates a controller in the specified module.
10+
*/
11+
class ModuleController extends BaseCommand
12+
{
13+
/**
14+
* The Command's Group
15+
*
16+
* @var string
17+
*/
18+
protected $group = 'SimpleTine';
19+
20+
/**
21+
* The Command's Name
22+
*
23+
* @var string
24+
*/
25+
protected $name = 'module:controller';
26+
27+
/**
28+
* The Command's Description
29+
*
30+
* @var string
31+
*/
32+
protected $description = 'Generate a controller into a module';
33+
34+
/**
35+
* The Command's Usage
36+
*
37+
* @var string
38+
*/
39+
protected $usage = 'module:controller <module> <file>';
40+
41+
/**
42+
* The Command's Arguments
43+
*
44+
* @var array
45+
*/
46+
protected $arguments = [
47+
'module' => 'The name of the existing module directory',
48+
'file' => 'The name of the file to create',
49+
];
50+
51+
/**
52+
* The Command's Options
53+
*
54+
* @var array
55+
*/
56+
protected $options = [];
57+
58+
/**
59+
* Execute the command.
60+
*
61+
* @param array $params
62+
*/
63+
public function run(array $params)
64+
{
65+
helper('inflector');
66+
67+
$directoryMainFolder = 'Modules';
68+
if (!is_dir(APPPATH . $directoryMainFolder)) {
69+
if (mkdir(APPPATH . $directoryMainFolder, 0755, true)) {
70+
CLI::write('Modules Folder created', 'green');
71+
} else {
72+
CLI::error('Modules Folder creation failed. Please create a new folder (Modules) inside APP or try again.');
73+
return;
74+
}
75+
}
76+
77+
$directoryName = array_shift($params);
78+
$fileName = array_shift($params);
79+
80+
if (!$directoryName || !$fileName) {
81+
CLI::error('Both module name and file name are required.');
82+
return;
83+
}
84+
85+
$moduleDirectory = "$directoryMainFolder/$directoryName";
86+
if (!is_dir(APPPATH . $moduleDirectory)) {
87+
mkdir(APPPATH . $moduleDirectory, 0755, true);
88+
CLI::write('Module folder created - ' . APPPATH . $moduleDirectory, 'green');
89+
}
90+
91+
$controllerDirectory = APPPATH . $moduleDirectory . '/Controllers';
92+
if (!is_dir($controllerDirectory)) {
93+
mkdir($controllerDirectory, 0755, true);
94+
}
95+
96+
$namespace = str_replace('/', '\\', $moduleDirectory);
97+
$className = pascalize($fileName);
98+
$controllerTemplate = $this->getTemplate(
99+
'controller.new.tpl.php',
100+
[
101+
'{namespace}' => "$namespace\\Controllers",
102+
'{useModelStatement}' => "$namespace\\Models\\$className",
103+
'{useStatement}' => 'App\Controllers\BaseController',
104+
'{class}' => $className,
105+
'{modelClass}' => $className,
106+
'{extends}' => 'BaseController',
107+
'{directoryName}' => $directoryName,
108+
]
109+
);
110+
111+
$controllerPath = APPPATH . $moduleDirectory . "/Controllers/$className.php";
112+
file_put_contents($controllerPath, $controllerTemplate);
113+
114+
CLI::write("Controller '$className' created in module '$directoryName'.", 'green');
115+
}
116+
117+
/**
118+
* Get the template content with placeholders replaced.
119+
*
120+
* @param string $templateFile
121+
* @param array $placeholders
122+
* @return string
123+
*/
124+
protected function getTemplate(string $templateFile, array $placeholders): string
125+
{
126+
$templatePath = APPPATH . 'Commands/Views/' . $templateFile;
127+
128+
if (!file_exists($templatePath)) {
129+
CLI::write('Template file not found: ' . $templateFile, 'red');
130+
return '';
131+
}
132+
133+
$templateContent = file_get_contents($templatePath);
134+
135+
foreach ($placeholders as $placeholder => $value) {
136+
$templateContent = str_replace($placeholder, $value, $templateContent);
137+
}
138+
139+
return str_replace('<@php', '<?php', $templateContent);
140+
}
141+
}

0 commit comments

Comments
 (0)