Make a Composer package

Make a basic Composer package with PHPUnit.

Initialize folders and files

(Replace ~/Code with your own directory.)

mkdir -p ~/Code/bcp/{docs,src,tests}
touch ~/Code/bcp/{docs,src,tests}/.gitkeep
cd ~/Code/bcp

Add files

Create .gitignore

echo '/vendor/*
!/vendor/.*
Thumbs.db
.DS_Store
.env
.vagrant
composer.lock
composer.phar
' | tee .gitignore

Create README.md

echo '# Basic Composer package
## Prerequisites
## Installation
## Tests
## Usage
## References
' | tee README.md

Download composer.phar

curl -sS https://getcomposer.org/installer | php

Create composer.json

(Replace nevvix/bcp with your own vendor name and repository name.)

echo '{}' | tee composer.json
php composer.phar config name nevvix/bcp
php composer.phar config description "Basic Composer package"
php composer.phar config type library
php composer.phar config license MIT
php composer.phar config platform.php 7.2
php composer.phar config sort-packages true

Append autoload

nano composer.json

(Replace Vendor\\Namespace with your own vendor name and namespace.)

+   },
+   "autoload": {
+       "psr-4": {
+           "Vendor\\Namespace\\": "src/"
+       }
+   },
+   "autoload-dev": {
+       "psr-4": {
+           "Vendor\\Namespace\\Test\\": "tests/"
+       }
    }
}

Validate composer.json

php composer.phar validate

Create your files

In the docs, src and tests folders, create your own files.

src files

Under src, create the namespace for PHP files:

(Replace Vendor\Namespace and Classname with your own names.)

<?php
namespace Vendor\Namespace;

class Classname {
    // ...
}

tests files

Under tests, create the namespace for PHP unit test files:

(Replace Vendor\Namespace, Vendor\Namespace\Classname and ClassnameTest with your own names.)

<?php
namespace Vendor\Namespace\Test;

use PHPUnit\Framework\TestCase;
use Vendor\Namespace\Classname;

class ClassnameTest extends TestCase {
    // ...
}

Add PHP and PHPUnit

https://phpunit.de/getting-started/phpunit-8.html

php composer.phar require php ^7.2
php composer.phar require --dev phpunit/phpunit ^8
vendor/bin/phpunit --version

Resulting composer.json

cat composer.json
{
    "name": "nevvix/bcp",
    "description": "Basic Composer package",
    "type": "library",
    "license": ["MIT"],
    "config": {
        "platform": {
            "php": "7.2"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "Vendor\\Namespace\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Vendor\\Namespace\\Test\\": "tests/"
        }
    },
    "require": {
        "php": "^7.2"
    },
    "require-dev": {
        "phpunit/phpunit": "^8"
    }
}

Resulting files

cd ~/Code/bcp
ls -lAFh
total 3920
drwxr-xr-x   9 steven  staff   288B 14 Dec 15:25 .git/
-rw-r--r--   1 steven  staff    84B 14 Dec 15:23 .gitignore
-rw-r--r--   1 steven  staff    91B 14 Dec 13:07 README.md
-rw-r--r--   1 steven  staff   549B 14 Dec 15:14 composer.json
-rw-r--r--   1 steven  staff    53K 14 Dec 15:14 composer.lock
-rwxr-xr-x   1 steven  staff   1.8M 14 Dec 13:20 composer.phar*
drwxr-xr-x   3 steven  staff    96B 14 Dec 15:25 docs/
drwxr-xr-x   3 steven  staff    96B 14 Dec 15:25 src/
drwxr-xr-x   3 steven  staff    96B 14 Dec 15:25 tests/
drwxr-xr-x  15 steven  staff   480B 14 Dec 15:14 vendor/

Git push

Create a repository called bcp in your GitHub account.

(Replace nevvix/bcp with your own vendor name and repo name.)

cd ~/Code/bcp
git init
git remote add origin https://github.com/nevvix/bcp.git
git add -Av
git commit -m"Initial commit"
git push -u origin master

Usage

Require the package in a new project:

cd {new project}
php composer.phar require nevvix/bcp

If the package is not registered with Packagist.org, you have to add the repo URL:

php composer.phar config repositories.nevvix vcs https://github.com/nevvix/bcp