Skip to content

Commit 31af9b4

Browse files
committed
Release version 1.0.0
1 parent 1bd88d4 commit 31af9b4

File tree

4 files changed

+319
-2
lines changed

4 files changed

+319
-2
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Nick Tsai
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 240 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,240 @@
1-
# codeigniter-psr4-autoloader
2-
CodeIgniter 3 PSR-4 Autoloader Support
1+
<p align="center">
2+
<a href="https://codeigniter.com/" target="_blank">
3+
<img src="https://codeigniter.com/assets/images/ci-logo-big.png" height="100px">
4+
</a>
5+
<h1 align="center">CodeIgniter PSR-4 Autoload</h1>
6+
<br>
7+
</p>
8+
9+
CodeIgniter 3 PSR-4 Autoloader for Application
10+
11+
[![Latest Stable Version](https://poser.pugx.org/yidas/codeigniter-psr4-autoload/v/stable?format=flat-square)](https://packagist.org/packages/yidas/codeigniter-psr4-autoload)
12+
[![Latest Unstable Version](https://poser.pugx.org/yidas/codeigniter-psr4-autoload/v/unstable?format=flat-square)](https://packagist.org/packages/yidas/codeigniter-psr4-autoload)
13+
[![License](https://poser.pugx.org/yidas/codeigniter-psr4-autoload/license?format=flat-square)](https://packagist.org/packages/yidas/codeigniter-psr4-autoload)
14+
15+
FEATURES
16+
--------
17+
18+
- ***PSR-4 Namespace** support as Yii2 & Laravel elegant patterns like*
19+
20+
- *Easy way to use **Interface, Trait, Abstract and Extending Class***
21+
22+
- ***Whole Codeigniter application** directory structure support*
23+
24+
25+
DEMONSTRATION
26+
-------------
27+
28+
Autoload class files by PSR-4 namespace with `app` prefix in Codeigniter:
29+
30+
```php
31+
# /application/libraries/MemberService.php:
32+
\app\libraries\MemberService::auth();
33+
34+
# /application/widgets/StatWidget.php:
35+
\app\widgets\StatWidget::run();
36+
37+
class Blog_model extends app\models\BaseModel {}
38+
class Blog extends app\libraries\BaseController {}
39+
class Car implements app\contracts\CarInterface {}
40+
```
41+
42+
More specifically, create a class with namespace refering to the file path `\application\helpers\`:
43+
44+
```php
45+
<?php
46+
namespace app\helpers;
47+
48+
class ArrayHelper
49+
{
50+
public static function indexBy($input) {}
51+
}
52+
```
53+
54+
Then call it in Controller action:
55+
56+
```php
57+
<?php
58+
use app\helpers\ArrayHelper;
59+
...
60+
ArrayHelper::indexBy($input);
61+
\app\helpers\ArrayHelper::indexBy($input);
62+
```
63+
64+
---
65+
66+
REQUIREMENTS
67+
------------
68+
69+
This library requires the following:
70+
71+
- PHP 5.3.0+
72+
- CodeIgniter 3.0.0+
73+
74+
---
75+
76+
INSTALLATION
77+
------------
78+
79+
Run Composer in your Codeigniter project under the folder `\application`:
80+
81+
composer require yidas/codeigniter-psr4-autoload
82+
83+
Check Codeigniter `application/config/config.php`:
84+
85+
```php
86+
$config['composer_autoload'] = TRUE;
87+
```
88+
89+
> You could customize the vendor path into `$config['composer_autoload']`
90+
91+
---
92+
93+
CONFIGURATION
94+
-------------
95+
96+
After installation, you need to register it into Codeigniter system hook.
97+
98+
### 1. Enabling Hooks
99+
100+
The hooks feature can be globally enabled/disabled by setting the following item in the `application/config/config.php` file:
101+
102+
```php
103+
$config['enable_hooks'] = TRUE;
104+
```
105+
106+
### 2. Adding a Hook
107+
108+
Hooks are defined in the `application/config/hooks.php` file, add above hook into it:
109+
110+
```php
111+
/*
112+
| -------------------------------------------------------------------
113+
| Auto-load All Classes with PSR-4
114+
| -------------------------------------------------------------------
115+
| After registering \yidas\Psr4Autoload, you could auto-load every
116+
| classes in the whole Codeigniter application with `app` PSR-4
117+
| prefix by default, for example:
118+
| # /application/libraries/MemberService.php:
119+
| \app\libraries\MemberService::auth();
120+
| # /application/widgets/StatWidget.php:
121+
| \app\widgets\StatWidget::run();
122+
| class Blog_model extends app\models\BaseModel {}
123+
| class Blog extends app\libraries\BaseController {}
124+
| class Car_model implements app\contracts\CarInterface {}
125+
|
126+
| The called class need to define namespace to support PSR-4 Autoload
127+
| only, which means it would not support CI_Loader anymore.
128+
|
129+
| @see https://github.com/yidas/codeigniter-psr4-autoload
130+
*/
131+
132+
$hook['pre_system'][] = [new yidas\Psr4Autoload, 'register'];
133+
```
134+
135+
---
136+
137+
USAGE
138+
-----
139+
140+
After installation, the namespace prefix `app` is used for the current Codeigniter application directory.
141+
142+
### Extending Class
143+
144+
Create a class with PSR-4 namespace under `application` directory, for eaxmple `application/model/BaseModel.php`:
145+
146+
```php
147+
<?php
148+
namespace app\models;
149+
150+
class BaseModel extends \CI_Model {}
151+
```
152+
153+
Then define a class to extend above class, for eaxmple `application/model/My_model.php`:
154+
155+
```php
156+
<?php
157+
158+
class My_model extends app\models\BaseModel {}
159+
```
160+
161+
> In this case, Codeigniter `My_model` could not use PSR-4 namespace.
162+
163+
164+
### Interface
165+
166+
Create a interface under `application` directory, for eaxmple `application/interface/CarInterface.php`:
167+
168+
```php
169+
<?php
170+
namespace app\interfaces;
171+
172+
interface CarInterface {}
173+
```
174+
175+
Then apply the interface to a class, for eaxmple `application/libraries/Car.php`:
176+
177+
```php
178+
<?php
179+
namespace app\libraries;
180+
181+
class Car implements \app\interfaces\CarInterface {}
182+
```
183+
184+
> In this case, the `Car` lib could be called by using `new \app\libraries\Car;`.
185+
186+
187+
### Trait
188+
189+
Create a trait under `application` directory, for eaxmple `application/libraries/LogTrait.php`:
190+
191+
```php
192+
<?php
193+
namespace app\libraries;
194+
195+
trait LogTrait {}
196+
```
197+
198+
Then inject the trait into a class, for eaxmple `application/controller/Blog.php`:
199+
200+
```php
201+
class Blog extends CI_Controller
202+
{
203+
use \app\libraries\LogTrait;
204+
}
205+
```
206+
207+
### Abstract
208+
209+
Create an abstract under `application` directory, for eaxmple `application/libraries/BaseController.php`:
210+
211+
```php
212+
<?php
213+
namespace app\libraries;
214+
215+
abstract class BaseController extends \CI_Controller {}
216+
```
217+
218+
Then define a class to extend above abstract class, for eaxmple `application/libraries/BaseController.php`:
219+
220+
```php
221+
class Blog extends app\libraries\BaseController {}
222+
```
223+
224+
---
225+
226+
CONCEPTION
227+
----------
228+
229+
Codeigniter Loader is a good practice for loading one time instantiated component just like Yii2 Application Components, but it's lacking of Class mapping, which makes inconvenience to load classes including interfaces, traits, abstracts or extending classes.
230+
231+
Thus, You could defind classes with PSR-4 Namespace while these classes are not component kind, even Helpers which you may want use static method and customized class name.
232+
233+
---
234+
235+
LIMITATIONS
236+
-----------
237+
238+
### Namespace Class No Longer Support CI_Loader
239+
240+
The called class need to define namespace to support PSR-4 Autoload only, which means it would not support Built-in CI_Loader anymore.

composer.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "yidas/codeigniter-psr4-autoload",
3+
"description": "CodeIgniter 3 PSR-4 Autoloader for Application",
4+
"keywords": ["codeIgniter", "autoload", "PSR-4", "autoloader"],
5+
"homepage": "https://github.com/yidas/codeigniter-psr4-autoload",
6+
"type": "library",
7+
"license": "MIT",
8+
"support": {
9+
"issues": "https://github.com/yidas/codeigniter-psr4-autoload/issues",
10+
"source": "https://github.com/yidas/codeigniter-psr4-autoload"
11+
},
12+
"minimum-stability": "stable",
13+
"autoload": {
14+
"classmap": ["src/"]
15+
}
16+
}

src/Psr4Autoload.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace yidas;
4+
5+
/**
6+
* PSR-4 Autoloader for Codeiginiter 3 application
7+
*
8+
* @author Nick Tsai <myintaer@gmail.com>
9+
* @version 1.0.0
10+
* @see https://github.com/yidas/codeigniter-psr4-autoload
11+
*/
12+
class Psr4Autoload
13+
{
14+
/**
15+
* @var string Nampsapce prefix refered to application root
16+
*/
17+
const DEFAULT_PREFIX = "app";
18+
19+
/**
20+
* Register Autoloader
21+
*
22+
* @param string $prefix PSR-4 namespace prefix
23+
*/
24+
public static function register($prefix=null)
25+
{
26+
$prefix = ($prefix) ? (string)$prefix : self::DEFAULT_PREFIX;
27+
28+
spl_autoload_register(function ($classname) use ($prefix) {
29+
// Prefix check
30+
if (strpos(strtolower($classname), "{$prefix}\\")===0) {
31+
// Locate class relative path
32+
$classname = str_replace("{$prefix}\\", "", $classname);
33+
$filepath = APPPATH. str_replace('\\', DIRECTORY_SEPARATOR, ltrim($classname, '\\')) . '.php';
34+
35+
if (file_exists($filepath)) {
36+
37+
require $filepath;
38+
}
39+
}
40+
});
41+
}
42+
}

0 commit comments

Comments
 (0)