├── composer.json
├── LICENSE
├── src
└── Psr4Autoload.php
└── README.md
/composer.json:
--------------------------------------------------------------------------------
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 | "require": {
14 | "php": ">=5.3.0"
15 | },
16 | "autoload": {
17 | "classmap": ["src/"],
18 | "psr-4": {
19 | "app\\": "../../../"
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
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.
22 |
--------------------------------------------------------------------------------
/src/Psr4Autoload.php:
--------------------------------------------------------------------------------
1 |
9 | * @since 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 | }
43 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
CodeIgniter PSR-4 Autoload
6 |
7 |
8 |
9 | CodeIgniter 3 PSR-4 Autoloader for Application
10 |
11 | [](https://packagist.org/packages/yidas/codeigniter-psr4-autoload)
12 | [](https://packagist.org/packages/yidas/codeigniter-psr4-autoload)
13 | [](https://packagist.org/packages/yidas/codeigniter-psr4-autoload)
14 | [](https://packagist.org/packages/yidas/codeigniter-psr4-autoload)
15 |
16 | This PSR-4 extension is collected into [yidas/codeigniter-pack](https://github.com/yidas/codeigniter-pack) which is a complete solution for Codeigniter framework.
17 |
18 | FEATURES
19 | --------
20 |
21 | - ***PSR-4 Namespace** support as Yii2 & Laravel elegant patterns like*
22 |
23 | - *Easy way to use **Interface, Trait, Abstract and Extending Class***
24 |
25 | - ***Whole Codeigniter application** directory structure support*
26 |
27 | ---
28 |
29 | OUTLINE
30 | -------
31 |
32 | - [Demonstration](#demonstration)
33 | - [Requirements](#requirements)
34 | - [Installation](#installation)
35 | - [Usage](#usage)
36 | - [Extending Class](#extending-class)
37 | - [Interface](#interface)
38 | - [Trait](#trait)
39 | - [Abstract](#abstract)
40 | - [Conception](#conception)
41 | - [Limitations](#limitations)
42 |
43 | ---
44 |
45 | DEMONSTRATION
46 | -------------
47 |
48 | By default, all PSR-4 namespace with `app` prefix in Codeigniter would relates to application directory.
49 |
50 | - The class `/application/libraries/MemberService.php` could be called by:
51 |
52 | ```php
53 | new \app\libraries\MemberService;
54 | ```
55 |
56 | - The class `/application/services/Process.php` with `static run()` method could be called by:
57 |
58 | ```php
59 | \app\services\Process::run();
60 | ```
61 |
62 | - Enable to extend or implement classes with standard way:
63 |
64 | ```php
65 | class Blog_model extends app\models\BaseModel {}
66 | class Blog extends app\components\BaseController {}
67 | class Car implements app\contracts\CarInterface {}
68 | ```
69 |
70 | ---
71 |
72 | REQUIREMENTS
73 | ------------
74 |
75 | This library requires the following:
76 |
77 | - PHP 5.3.0+
78 | - CodeIgniter 3.0.0+
79 |
80 | ---
81 |
82 | INSTALLATION
83 | ------------
84 |
85 | Run Composer in your Codeigniter project under the folder `\application`:
86 |
87 | composer require yidas/codeigniter-psr4-autoload
88 |
89 | Check Codeigniter `application/config/config.php`:
90 |
91 | ```php
92 | $config['composer_autoload'] = TRUE;
93 | ```
94 |
95 | > You could customize the vendor path into `$config['composer_autoload']`
96 |
97 | ---
98 |
99 | USAGE
100 | -----
101 |
102 | After installation, the namespace prefix `app` is used for the current Codeigniter application directory.
103 |
104 |
105 | ### Static Class
106 |
107 |
108 | Create a hepler with PSR-4 namespace with a new `helpers` folder under `application` directory, for eaxmple `\application\helpers\`:
109 |
110 | ```php
111 | In this case, Codeigniter `My_model` could not use PSR-4 namespace.
150 |
151 |
152 | ### Interface
153 |
154 | Create a interface with a new `interface` folder under `application` directory, for eaxmple `application/interface/CarInterface.php`:
155 |
156 | ```php
157 | In this case, the `Car` lib could be called by using `new \app\libraries\Car;`.
173 |
174 |
175 | ### Trait
176 |
177 | Create a trait under `application` directory, for eaxmple `application/libraries/LogTrait.php`:
178 |
179 | ```php
180 |