Good content takes time and effort to come up with.

Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.







How To Create A Composer Package? Test It Locally And Add To Packagist Repository


DEVELOPE PACKAGE PACKAGIST COMPOSER
10th May 2020 5 mins read
Share Article     Share On WhatsApp   Buy Me Coffee | StackCoder Buy Coffee


Hello friends in this article, you will learn a simple way to create a composer package & add it to packagist. Let's built RSS Generating package as an example. Here I wont be explaining a whole project code, will add some partial code which will give you good understanding.


This article covers how to

  1. Composer Package Project Setup
  2. To Test Your Package Locally Before Pushing To Packagist
  3. Setup Project For GIT Releases
  4. Push Package To Setup

Prerequisites


Basic knowledge on composer & write PHP scripts, in case if your new to composer or would like to know in depth knowledge this please refer this article What Is Composer? How Does It Work? Useful Composer Commands And Usage.


1) Composer Package Project Setup


First create a project folder in www, html or htdocs folder when you run your basic php projects.


For our example I have created rss inside htdocs folder.


i. Create composer.json file


Basically my folder structure is as follows


RSS Composer Package Folder Structure

RSS Composer Package Folder Structure



You project must have the composer.json file in the root of your project folder as shown in the above image and will have the following content


{
    "name": "channaveer/rss",
    "description": "Simple RSS 2.0 Generator",
    "keywords": ["rss", "rss2.0", "simple-rss", "rss-generator"],
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "Channaveer Hakari",
            "email": "channaveer888@gmail.com",
            "homepage": "https://stackcoder.in/about-me",
            "role": "Developer"
        }
    ],
    "require": {
        "php": ">=5.6.0"
    },
    "autoload": {
        "psr-4": {
            "Channaveer\\RSS\\": "src/"
        }
    },
    "extra": {
        "branch-alias": {
            "dev-master": "1.0-dev"
        }
    },
    "minimum-stability": "dev",
    "config": {
        "sort-packages": true
    }
}


name - Package name of your project, make sure that this must be unique


description - Description of your project


keywords - Keywords your project needs to be searched for


type - Mine is library, if you are creating something like Laravel then this will be project


license - Type of license of your project


author - Array of author details


require - What packages does your project depends on add it over here. Mine doesn't depend on any so I left empty here

require-dev - If your project depends on any development packages then add it here like PHPUnit


autoload - Here you can autoload the default folder & files that is need by your project


I would like to add more details for autoload over here


"psr-4": {
    "Channaveer\\RSS\\": "src/"
}


Here I would like to explain about autoload, all the files from src/ folder of my composer project will be auto loaded. All the files inside this src/ folder will have the namespace with Channaveer\RSS namespace. Make sure to add it as Channaveer\\RSS\\ as is.


ii. Add Your Code Inside src Folder


As you can see from the above image I have a folder called as src this houses all the project code. RSS.php is the main file which will have all of my code. Since I am writing a very tiny project I do have only 1 file. If you look at really good projects their src folder is very large


iii. Overview Of My Project Accessing


I have written the main code inside src/RSS.php file. I wanted to developed the package so that I can call it with ease as follows


$rss = new RSS();

$rss->siteName('SiteName')
    ->siteUrl('https://siteurl.com')
    ->description('Some random description of the site')
    ->language('en-US') /** Default is en-Us you can set any of yours */
    ->lastUpdated($feedItems[0]['updatedAt']) /** Just pass datetime string or date string */
    ->generate($path, $rssItems, $filename);


As you can see the I have used method chaining to make my calls dam easy to understand. I will possibly create another article on method chaining and explaining about my code. But for this article it's out of scope.


I have written my main code inside src/RSS.php file


I have deployed it in GitHub & even in Packagist. Feel free to explore it.


2) To Test Your Package Locally Before Pushing To Packagist


Now you have the above code setup & RSS.php file inside src/ but don't know if it works for you or not, so we need to test it for the sake of debugging.


I will create another project named as testrss inside htdocs folder for testing my rss composer package.


The following is our project structure


Test RSS Project Folder Structure

Test RSS Project Folder Structure



Our testrss folder will have the following composer.json file


{
    "name": "dc/demo",
    "description": "",
    "require": {
        "channaveer/rss": "@dev"
    },
    "autoload": {
        "psr-4": {
            "Channaveer\\RSS\\": "src/"
        }
    },
    "repositories": [
        {
            "type": "path",
            "url": "../rss"
        }
    ]
}


Concentrate on the following code inside the above composer.json file


"repositories": [
        {
            "type": "path",
            "url": "../rss"
        }
    ]


This code is the one while will tell that we wan to load the package from the local repository path. Since my rss package is one level up I have added url as ../rss


Once you add the above code inside composer.json file. Just run


composer install


This will install the rss project as normal composer project and will point symbolically to the rss project. Any changes you make in rss project will automatically reflect here directly.


3) Setup Project For GIT Releases


Go to your rss composer project.


Now we have MVP of our package so I want to push it to packagist. Since we have developed this in a dev environment, we have to add the releases in GITHUB directly or can add tags with GIT. If we don't add the tag or release then we need to install the package as dev, else it wont install


I will use GIT annotation tags. I have added annotation tag from master branch with the following command


Tip - tag in GIT is like releases ie if we want to mark any specific version of the project that it was working awesome then you can use it.


git tag -a v1.0 -m "MVP Stable Release"


This will create v1.0 tag. We can push this tag version similar to that as of your normal branch as shown below


git push origin v1.0


Now you package is ready for its first release.


4) Push Package To Setup


i. Create Account In Packagist


First go to packagist.org & create your account.



ii. Get Packagist API Token For WebHook Management in GitHub


First go to your Packagist Profile Name -> (1) Profile -> (2) Click On Show API Token as shown in the following image to get your API token


Packagist Get API Token

Packagist Get API Token



iii. Setup WebHooks Of Packagist To GitHub


Login To GitHub and go to the project repository settings as shown in the following image


GitHub RSS Project Repository Settings

GitHub RSS Project Repository Settings


Now click on WebHooks from the left sidebar to add new WebHooks as shown in the following image


Add Packagist Web Hooks To GitHub

Add Packagist Web Hooks To GitHub


  1. Click on WebHooks in left sidebar
  2. Add payload URL as - https://packagist.org/api/github
  3. Content Type - application/json
  4. Secret - Add the API token of Packagist
  5. Click Add WebHook button to successfully add the GitHook



iv. Submit Package


Go to you Packagist dashboard. Now you can publish your new packages by clicking on Submit in Packagist.


Add your GitHub package URL here as shown in the following image. It may ask you for GitHub verification allow it to access your repos.


Submit Package In Packagist

Submit Package In Packagist


Once you successfully submit your package you will get the following page if everything is okay


Successfully Submitted RSS Package

Successfully Submitted RSS Package


Conclusion


You have successfully uploaded your 1st composer package.


WHATS NEXT?


You might be interest to learn more on composer please find my whole article on it


How To Install Packages Parallel For Faster Development In Composer

What Is Composer? How Does It Work? Useful Composer Commands And Usage

composer.json v/s composer.lock

Composer Install v/s Composer Update


Happy Coding :)








Author Image
AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing