|
| 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 | + ├── README.md |
| 20 | + ├── composer.json |
| 21 | + ├── src/ |
| 22 | + │ └── YourClass.php |
| 23 | + └── tests/ |
| 24 | + └── YourClassTest.php |
| 25 | + |
| 26 | +********************** |
| 27 | +Creating composer.json |
| 28 | +********************** |
| 29 | + |
| 30 | +In the root of your package directory, create a **composer.json** file. This file |
| 31 | +defines metadata about your package and its dependencies. |
| 32 | + |
| 33 | +The ``composer init`` command helps you create it. |
| 34 | + |
| 35 | +For example, **composer.json** might look like this:: |
| 36 | + |
| 37 | + { |
| 38 | + "name": "your-vendor/your-package", |
| 39 | + "description": "Your package description", |
| 40 | + "type": "library", |
| 41 | + "license": "MIT", |
| 42 | + "autoload": { |
| 43 | + "psr-4": { |
| 44 | + "YourVendor\\YourPackage\\": "src/" |
| 45 | + } |
| 46 | + }, |
| 47 | + "authors": [ |
| 48 | + { |
| 49 | + "name": "Your Name", |
| 50 | + |
| 51 | + } |
| 52 | + ], |
| 53 | + "require": { |
| 54 | + // Any dependencies required by your package go here |
| 55 | + }, |
| 56 | + "require-dev": { |
| 57 | + // Any development dependencies (e.g., PHPUnit) go here |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | +Package Name |
| 62 | +============ |
| 63 | + |
| 64 | +The ``name`` field is important here. Package names are generally written in the |
| 65 | +format "vendor-name/package-name" with all lowercase. Here is a common example: |
| 66 | + |
| 67 | +- ``your-vendor-name``: The name that identifies the vendor (creator of the package), |
| 68 | + such as the name of you or your organization. |
| 69 | +- ``your-package-name``: The name of the package you are creating. |
| 70 | + |
| 71 | +Thus, it is important to make the name unique and distinguish it from other packages. |
| 72 | +Uniqueness is especially important when publishing. |
| 73 | + |
| 74 | +Namespace |
| 75 | +========= |
| 76 | + |
| 77 | +The package name then determines the vendor namespace in ``autoload.psr4``. |
| 78 | + |
| 79 | +If your package name is ``your-vendor/your-package``, the vendor namespace must |
| 80 | +be ``YourVendor``. So you would write like the following:: |
| 81 | + |
| 82 | + "autoload": { |
| 83 | + "psr-4": { |
| 84 | + "YourVendor\\YourPackage\\": "src/" |
| 85 | + } |
| 86 | + }, |
| 87 | + |
| 88 | +This setting instructs Composer to autoload the source code for your package. |
| 89 | + |
| 90 | +************ |
| 91 | +Config Files |
| 92 | +************ |
| 93 | + |
| 94 | +Allowing Users to Override Settings |
| 95 | +=================================== |
| 96 | + |
| 97 | +If your package has a configuration file and you want users to be able to override |
| 98 | +the settings, use :php:func:`config()` with the short classname like ``config('YourConfig')`` |
| 99 | +to call the configuration file. |
| 100 | + |
| 101 | +Users can then override the package configuration by placing a configuration class |
| 102 | +with the same short classname in **app/Config** that extends the package Config |
| 103 | +class like ``YourVendor\YourPackage\Config\YourConfig``. |
| 104 | + |
| 105 | +Overriding Settings in app/Config |
| 106 | +================================= |
| 107 | + |
| 108 | +If you need to override or add to known configurations in the **app/Config** folder, |
| 109 | +you can use :ref:`Implicit Registrars <registrars>`. |
0 commit comments