|
| 1 | +########################## |
| 2 | +Creating Composer Packages |
| 3 | +########################## |
| 4 | + |
| 5 | +You can make the :doc:`../general/modules` you create into Composer packages, |
| 6 | +or can create a Composer package for CodeIgniter 4. |
| 7 | + |
| 8 | +.. contents:: |
| 9 | + :local: |
| 10 | + :depth: 2 |
| 11 | + |
| 12 | +**************** |
| 13 | +Folder Structure |
| 14 | +**************** |
| 15 | + |
| 16 | +Here's a typical directory structure for a Composer package:: |
| 17 | + |
| 18 | + your-package-name/ |
| 19 | + ├── .gitattributes |
| 20 | + ├── .gitignore |
| 21 | + ├── LICENSE |
| 22 | + ├── README.md |
| 23 | + ├── composer.json |
| 24 | + ├── src/ |
| 25 | + │ └── YourClass.php |
| 26 | + └── tests/ |
| 27 | + └── YourClassTest.php |
| 28 | + |
| 29 | +********************** |
| 30 | +Creating composer.json |
| 31 | +********************** |
| 32 | + |
| 33 | +In the root of your package directory, create a **composer.json** file. This file |
| 34 | +defines metadata about your package and its dependencies. |
| 35 | + |
| 36 | +The ``composer init`` command helps you create it. |
| 37 | + |
| 38 | +For example, **composer.json** might look like this:: |
| 39 | + |
| 40 | + { |
| 41 | + "name": "your-vendor/your-package", |
| 42 | + "description": "Your package description", |
| 43 | + "type": "library", |
| 44 | + "license": "MIT", |
| 45 | + "autoload": { |
| 46 | + "psr-4": { |
| 47 | + "YourVendor\\YourPackage\\": "src/" |
| 48 | + } |
| 49 | + }, |
| 50 | + "authors": [ |
| 51 | + { |
| 52 | + "name": "Your Name", |
| 53 | + "email": "yourname@example.com" |
| 54 | + } |
| 55 | + ], |
| 56 | + "require": { |
| 57 | + // Any dependencies required by your package go here |
| 58 | + }, |
| 59 | + "require-dev": { |
| 60 | + // Any development dependencies (e.g., PHPUnit) go here |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | +Package Name |
| 65 | +============ |
| 66 | + |
| 67 | +The ``name`` field is important here. Package names are generally written in the |
| 68 | +format "vendor-name/package-name" with all lowercase. Here is a common example: |
| 69 | + |
| 70 | +- ``your-vendor-name``: The name that identifies the vendor (creator of the package), |
| 71 | + such as your name or your organization. |
| 72 | +- ``your-package-name``: The name of the package you are creating. |
| 73 | + |
| 74 | +Thus, it is important to make the name unique to distinguish it from other packages. |
| 75 | +Uniqueness is especially important when publishing. |
| 76 | + |
| 77 | +Namespace |
| 78 | +========= |
| 79 | + |
| 80 | +The package name then determines the vendor namespace in ``autoload.psr4``. |
| 81 | + |
| 82 | +If your package name is ``your-vendor/your-package``, the vendor namespace must |
| 83 | +be ``YourVendor``. So you would write like the following:: |
| 84 | + |
| 85 | + "autoload": { |
| 86 | + "psr-4": { |
| 87 | + "YourVendor\\YourPackage\\": "src/" |
| 88 | + } |
| 89 | + }, |
| 90 | + |
| 91 | +This setting instructs Composer to autoload the source code for your package. |
| 92 | + |
| 93 | +*************************** |
| 94 | +Preparing Development Tools |
| 95 | +*************************** |
| 96 | + |
| 97 | +There are many tools that help ensure quality code. So you should use them. |
| 98 | +You can easily install and configure such tools with |
| 99 | +`CodeIgniter DevKit <https://github.com/codeigniter4/devkit>`_. |
| 100 | + |
| 101 | +Installing DevKit |
| 102 | +================= |
| 103 | + |
| 104 | +In the root of your package directory, run the following commands: |
| 105 | + |
| 106 | +.. code-block:: console |
| 107 | +
|
| 108 | + composer config minimum-stability dev |
| 109 | + composer config prefer-stable true |
| 110 | + composer require --dev codeigniter4/devkit |
| 111 | +
|
| 112 | +The DevKit installs various Composer packages that helps your development, and |
| 113 | +installs templates for them in **vendor/codeigniter4/devkit/src/Template**. |
| 114 | +Copy the files in it to your project root folder, and edit them for your needs. |
| 115 | + |
| 116 | +Configuring Coding Standards Fixer |
| 117 | +================================== |
| 118 | + |
| 119 | +DevKit provides Coding Standards Fixer with |
| 120 | +`CodeIgniter Coding Standard <https://github.com/CodeIgniter/coding-standard>`_ |
| 121 | +based on `PHP-CS-Fixer <https://github.com/PHP-CS-Fixer/PHP-CS-Fixer>`_. |
| 122 | + |
| 123 | +Copy **vendor/codeigniter4/devkit/src/Template/.php-cs-fixer.dist.php** to your |
| 124 | +project root folder. |
| 125 | + |
| 126 | +Create the **build** folder for the cache file:: |
| 127 | + |
| 128 | + your-package-name/ |
| 129 | + ├── .php-cs-fixer.dist.php |
| 130 | + ├── build/ |
| 131 | + |
| 132 | +Open **.php-cs-fixer.dist.php** in your editor, and fix the folder path:: |
| 133 | + |
| 134 | + --- a/.php-cs-fixer.dist.php |
| 135 | + +++ b/.php-cs-fixer.dist.php |
| 136 | + @@ -7,7 +7,7 @@ use PhpCsFixer\Finder; |
| 137 | + $finder = Finder::create() |
| 138 | + ->files() |
| 139 | + ->in([ |
| 140 | + - __DIR__ . '/app/', |
| 141 | + + __DIR__ . '/src/', |
| 142 | + __DIR__ . '/tests/', |
| 143 | + ]) |
| 144 | + ->exclude([ |
| 145 | + |
| 146 | +That't it. Now you can run Coding Standards Fixer:: |
| 147 | + |
| 148 | + > vendor/bin/php-cs-fixer fix --ansi --verbose --diff |
| 149 | + |
| 150 | +If you add ``scripts.cs-fix`` in your **composer.json**, you can run it with |
| 151 | +``composer cs-fix`` command:: |
| 152 | + |
| 153 | + { |
| 154 | + // ... |
| 155 | + }, |
| 156 | + "scripts": { |
| 157 | + "cs-fix": "php-cs-fixer fix --ansi --verbose --diff" |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | +************ |
| 162 | +Config Files |
| 163 | +************ |
| 164 | + |
| 165 | +Allowing Users to Override Settings |
| 166 | +=================================== |
| 167 | + |
| 168 | +If your package has a configuration file and you want users to be able to override |
| 169 | +the settings, use :php:func:`config()` with the short classname like ``config('YourConfig')`` |
| 170 | +to call the configuration file. |
| 171 | + |
| 172 | +Users can then override the package configuration by placing a configuration class |
| 173 | +with the same short classname in **app/Config** that extends the package Config |
| 174 | +class like ``YourVendor\YourPackage\Config\YourConfig``. |
| 175 | + |
| 176 | +Overriding Settings in app/Config |
| 177 | +================================= |
| 178 | + |
| 179 | +If you need to override or add to known configurations in the **app/Config** folder, |
| 180 | +you can use :ref:`Implicit Registrars <registrars>`. |
| 181 | + |
| 182 | +********** |
| 183 | +References |
| 184 | +********** |
| 185 | + |
| 186 | +We have published some official packages. You can use these packages as references |
| 187 | +when creating your own packages: |
| 188 | + |
| 189 | +- https://github.com/codeigniter4/shield |
| 190 | +- https://github.com/codeigniter4/settings |
| 191 | +- https://github.com/codeigniter4/tasks |
| 192 | +- https://github.com/codeigniter4/cache |
| 193 | + |
0 commit comments