-
Notifications
You must be signed in to change notification settings - Fork 1.9k
docs: add "Creating Composer Packages" #7742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kenjis
merged 8 commits into
codeigniter4:develop
from
kenjis:docs-extending/composer_packages.rst
Jul 31, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ce3cce5
docs: add Creating Composer Packages
kenjis c16221b
docs: add gitattributes and gitignore
kenjis c9cc01c
docs: add about DevKit
kenjis bab7227
docs: add References
kenjis add666c
docs: add LICENSE in Folder Structure
kenjis b21eb76
docs: fix by proofreading
kenjis 6684f7a
docs: remove `>` so that the commands can be copy pasted
kenjis 1a96f24
docs: stop representation by diff
kenjis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
########################## | ||
Creating Composer Packages | ||
########################## | ||
|
||
You can make the :doc:`../general/modules` you create into Composer packages, | ||
or can create a Composer package for CodeIgniter 4. | ||
|
||
.. contents:: | ||
:local: | ||
:depth: 2 | ||
|
||
**************** | ||
Folder Structure | ||
**************** | ||
|
||
Here's a typical directory structure for a Composer package:: | ||
|
||
your-package-name/ | ||
├── .gitattributes | ||
├── .gitignore | ||
├── LICENSE | ||
├── README.md | ||
├── composer.json | ||
├── src/ | ||
│ └── YourClass.php | ||
└── tests/ | ||
└── YourClassTest.php | ||
samsonasik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
********************** | ||
Creating composer.json | ||
********************** | ||
|
||
In the root of your package directory, create a **composer.json** file. This file | ||
defines metadata about your package and its dependencies. | ||
|
||
The ``composer init`` command helps you create it. | ||
|
||
For example, **composer.json** might look like this:: | ||
|
||
{ | ||
"name": "your-vendor/your-package", | ||
"description": "Your package description", | ||
"type": "library", | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"YourVendor\\YourPackage\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Your Name", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
// Any dependencies required by your package go here | ||
}, | ||
"require-dev": { | ||
// Any development dependencies (e.g., PHPUnit) go here | ||
} | ||
} | ||
|
||
Package Name | ||
============ | ||
|
||
The ``name`` field is important here. Package names are generally written in the | ||
format "vendor-name/package-name" with all lowercase. Here is a common example: | ||
|
||
- ``your-vendor-name``: The name that identifies the vendor (creator of the package), | ||
such as your name or your organization. | ||
- ``your-package-name``: The name of the package you are creating. | ||
|
||
Thus, it is important to make the name unique to distinguish it from other packages. | ||
Uniqueness is especially important when publishing. | ||
|
||
Namespace | ||
========= | ||
|
||
The package name then determines the vendor namespace in ``autoload.psr4``. | ||
|
||
If your package name is ``your-vendor/your-package``, the vendor namespace must | ||
paulbalandan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
be ``YourVendor``. So you would write like the following:: | ||
|
||
"autoload": { | ||
"psr-4": { | ||
"YourVendor\\YourPackage\\": "src/" | ||
} | ||
}, | ||
|
||
This setting instructs Composer to autoload the source code for your package. | ||
|
||
*************************** | ||
Preparing Development Tools | ||
*************************** | ||
|
||
There are many tools that help ensure quality code. So you should use them. | ||
You can easily install and configure such tools with | ||
`CodeIgniter DevKit <https://github.com/codeigniter4/devkit>`_. | ||
|
||
Installing DevKit | ||
================= | ||
|
||
In the root of your package directory, run the following commands: | ||
|
||
.. code-block:: console | ||
|
||
composer config minimum-stability dev | ||
composer config prefer-stable true | ||
composer require --dev codeigniter4/devkit | ||
|
||
The DevKit installs various Composer packages that helps your development, and | ||
installs templates for them in **vendor/codeigniter4/devkit/src/Template**. | ||
Copy the files in it to your project root folder, and edit them for your needs. | ||
|
||
Configuring Coding Standards Fixer | ||
================================== | ||
|
||
DevKit provides Coding Standards Fixer with | ||
`CodeIgniter Coding Standard <https://github.com/CodeIgniter/coding-standard>`_ | ||
based on `PHP-CS-Fixer <https://github.com/PHP-CS-Fixer/PHP-CS-Fixer>`_. | ||
|
||
Copy **vendor/codeigniter4/devkit/src/Template/.php-cs-fixer.dist.php** to your | ||
project root folder. | ||
|
||
Create the **build** folder for the cache file:: | ||
|
||
your-package-name/ | ||
├── .php-cs-fixer.dist.php | ||
├── build/ | ||
|
||
Open **.php-cs-fixer.dist.php** in your editor, and fix the folder path:: | ||
|
||
--- a/.php-cs-fixer.dist.php | ||
+++ b/.php-cs-fixer.dist.php | ||
@@ -7,7 +7,7 @@ use PhpCsFixer\Finder; | ||
$finder = Finder::create() | ||
->files() | ||
->in([ | ||
- __DIR__ . '/app/', | ||
+ __DIR__ . '/src/', | ||
__DIR__ . '/tests/', | ||
]) | ||
->exclude([ | ||
|
||
That't it. Now you can run Coding Standards Fixer:: | ||
|
||
> vendor/bin/php-cs-fixer fix --ansi --verbose --diff | ||
|
||
If you add ``scripts.cs-fix`` in your **composer.json**, you can run it with | ||
``composer cs-fix`` command:: | ||
|
||
{ | ||
// ... | ||
}, | ||
"scripts": { | ||
"cs-fix": "php-cs-fixer fix --ansi --verbose --diff" | ||
} | ||
} | ||
|
||
************ | ||
Config Files | ||
************ | ||
|
||
Allowing Users to Override Settings | ||
=================================== | ||
|
||
If your package has a configuration file and you want users to be able to override | ||
the settings, use :php:func:`config()` with the short classname like ``config('YourConfig')`` | ||
to call the configuration file. | ||
|
||
Users can then override the package configuration by placing a configuration class | ||
with the same short classname in **app/Config** that extends the package Config | ||
class like ``YourVendor\YourPackage\Config\YourConfig``. | ||
|
||
Overriding Settings in app/Config | ||
================================= | ||
|
||
If you need to override or add to known configurations in the **app/Config** folder, | ||
you can use :ref:`Implicit Registrars <registrars>`. | ||
|
||
********** | ||
References | ||
********** | ||
|
||
We have published some official packages. You can use these packages as references | ||
when creating your own packages: | ||
|
||
- https://github.com/codeigniter4/shield | ||
- https://github.com/codeigniter4/settings | ||
- https://github.com/codeigniter4/tasks | ||
- https://github.com/codeigniter4/cache | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.