My First Contribution OSS Laravel Composer Package Published

kosa3
3 min readApr 14, 2019

--

In the begining

I wrote an article about my first contribution package development of Laravel.
This article is package development of Laravel for beginners.

As a background to this article.
First, I wanted to develop package of laravel. Because I have never done it before.
Second, I wanted to contribute OSS. In the future, I wanted to be contributor make more OSS contributions.
For these reason, I developed and published my first package kosa3/migration-stub-extention .

Package Introduction

https://packagist.org/packages/kosa3/migration-stub-extention

This package is extention package for migration features.
By using this, we can make customized migration file to edit the template migration file.

why it is maked?

I am operating the laravel project in bussiness.
When I make migration file, I describe table information.
But, default migration file is simple information. ex) id, timestamp
My operationg the laravel system must define columns create_type, create_id update_type….
If I make migration file by artisan command, I have to edit migration file adding column and alter column type.
I hate this task. So, I made this package to solve problem.
Next, I try to introduce the procedure of development.

Introduce the procedure of development

  1. Make directory packages/{User}/{PackageName} in my Laravel Project.
  2. Describe settings on composer.json.
  3. Place the developed source under the specified src written on composer.json
  4. Control version and tag with git.
  5. Register with packagist, specify url of github and register.
  1. Make directory packages/{User}/{PackageName} in my Laravel Project.

First, I make directory packages/kosa3/migrationStubExtentionin the root path of the project.
I create composer.json at packages/kosa3/migrationStubExtention.

$ composer init

The hierarchy was made in this way.

packages
└── kosa3
├── migrationStubExtention
├── LICENSE.md
├── composer.json
├── composer.lock
├── readme.md
├── src
└── vendor

2. Describe settings on composer.json.

When We typed command composer init , We will be asking the settings on CLI.

Finaly, I created a file composer.json .

{
"name": "kosa3/migration-stub-extention",
"license": "MIT",
"authors": [
{
"name": "kosa3",
"email": "suga.tech3@gmail.com"
}
],
"require": {
"php": "^7.0",
"laravel/framework": "^5.5"
},
"autoload": {
"psr-4": {
"Kosa3\\MigrationStubExtention\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Kosa3\\MigrationStubExtention\\MigrationServiceProvider"
]
}
}
}

https://getcomposer.org/doc/04-schema.md

name: package name.
license: package license. ex) MIT, BSD, GPL
authors: authors of the package.
require: lists packages required for developing this package.
autoload:
PSR-4 and PSR-0 autoloading, classmap generation and files includes are supported.
extra: automatic detection of packages.

3. Place the developed source under the specified src written on composer.json

I created MigrationServiceProvider.php.

<?php

namespace Kosa3\MigrationStubExtention;

use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Migrations\MigrationCreator;

class MigrationServiceProvider extends ServiceProvider
{
/**
* Register services.
*
*
@return void
*/
public function register()
{
$this->app->extend('migration.creator', function ($creator, $app) {
return new class ($app['files']) extends MigrationCreator
{
public function stubPath()
{
return resource_path() . '/migration';
}
};
});
}

/**
* Bootstrap services.
*
*
@return void
*/
public function boot()
{
$targetPath = resource_path() . '/migration';
if (! \File::isDirectory($targetPath)) {
if (\File::makeDirectory($targetPath)) {
\File::copyDirectory(base_path().'/vendor/laravel/framework/src/Illuminate/Database/Migrations/stubs', $targetPath);
}
}
}
}

・MigrationServiceProvider.php Inherited Illuminate\Database\Migrations\MigrationCreator .

・ OverridestubPath function.

・When booting, It make of copy default migration stub files to override stubPath.

4. Control version and tag with git.

Manage packages with git. Create and push a repository in advance.

$ git init
$ git add .
$ git commit -m 'first commit'
$ git remote add origin {hogehoge:gitRepositoryPath}

Next, tag control version. ex) 1.0.0, v_1.0 …

$ git tag 1.0.0
$ git push origin 1.0.0

5. Register with packagist, specify url of github and register.

Sign in to packagist and access the registration page.

registration page on packagist

We Just enter the URL of the git client that you managed in step 4 and press submit.

I was able to publish my first package.
Finally, I confirm the behavior in other laravel project.

I did not do unit test this time. Next time, I want to test automatic CI.

That’s it. Thank you for watching!

--

--

No responses yet