PHPVersion Control

Bitbucket Repository with Composer

Hi all,

In this article , you will find a tutorial about , how to use github or bitbucket repositories with composer.

Create Your Repository

Most important thing in this step, we will use the repository name in composer.json. I used “blog/tutorial” as a package name. I prefer to use bitbucket and at the below code copied from bitbucket repository page, following commands will init git repository to your local folder.

mkdir /path/to/your/project
cd /path/to/your/project
git init
git remote add origin [email protected]:yourname/blog-tutorial.git

Create Composer.json

Now, i defined a composer properties like , package name , description , authors , requirements etc.. For more information about composer.json please check Composer Basic Usage

Note: When you finished to edit “composer.json”  , dont forget to run

composer validate
{
    "name": "mtigdemir/blog-tutorial",
    "description": "Muharrem Tigdemir Blog Tutorial Package",
    "authors": [
        {
            "name": "Muharrem Tığdemir",
            "email": "[email protected]"
        }
    ],
    "require": {
        "php": ">=5.4",
    },
    "require-dev": {
        "phpunit/phpunit": "~4.6"
    },
    "autoload": {
        "psr-4": {
            "Mtigdemir\\Blog\\": "src/"
        }
    }
}

As you can see, I’ve defined autoload psr-4 property with namespace “Mtigdemir\\Blog\\”

!IMPORTANT! PSR-0 is deprecated , and PSR-4 standarts show us where to place files that will be autoloaded according to the specification. For more information about PSR-4 please check PHP Framework Interop Group

<?php namespace Mtigdemir\Blog;
// File must be under src directory
class User
{
	private $username; 
	private $email; 

	public function __constructor($username , $email){
		$this->username = $username;
		$this->email = $email;
	}

	public function getEmail(){
		return $this->email;
	}

	public function getUsername(){
		return $this->username;
	}
}

Let’s create a User.class in src/ directory with Mtigdemir\Blog namespace. Your package is ready to use with User class lets push it !

It’s time to include our package into the project

First of all, we have to include our repository to new project at the below composer.json definition is my new project and I included “[email protected]:mtigdemir/blog-tutorial.git” to my repositories.

Note : if you are using private repository. You have add your ssh-key to bitbucket!

{
   "name" : "mtigdemir/blog-tutorial-project",
   "description" : "Blog Tutorial Project",
   "repositories": [
   {
     "type": "vcs",
     "url":  "[email protected]:mtigdemir/blog-tutorial.git"
   }
   ],
   "require": {
       "php": ">=5.4",
    }
  }
composer search mtigdemir

This composer command will find “mtigdemir” packages including my private repository , as a result composer will find our package name and description if everything is cool you will see this kind of result in command line about your package definition: “mtigdemir/blog-tutorial Blog tutorial Package”

composer info mtigdemir/blog-tutorial

What about version ! I didn’t release any version , in that case if you pushed develop or master branch your requirement version will change that’s why composer info is very useful. My package version is “dev-master”

name     : mtigdemir/blog-tutorial                       
descrip. : Blog tutorial Package
keywords : 
versions : dev-master
type     : library
source   : [git] [email protected]:mtigdemir/blog-tutorial.git f37affd9a2f1500a4ff956aa727304ce66af951b
dist     : []  
names    : mtigdemir/blog-tutorial

autoload
psr-4
Bumin\Psp\Account\ => src/

requires
php >=5.4

requires (dev)
phpunit/phpunit ~4.6

Now , you can add your package like at the below or add package to composer.json and update it !

composer require mtigdemir/blog-tutorial:dev-master

Finally, I’m able to use my class in project — Yea only User Class user.php :((

Your package will be under vendor/ directory like other packages from packagist.org.

<?php
	
require "vendor/autoload.php";

$user = new \Mtigdemir\Blog\User("muharrem" , "muh[email protected]");

echo $user->getUserName().'__'.$user->getEmail();

I’m able to use my User Class with namespace ,

I want to thank to my team-mate Aykut Aras for share with me all about this know-how..

Muharrem Tığdemir

Author

Muharrem Tığdemir

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.