- php >= 5.3.4
- git
In this tutorial we're going to see how to create a private composer repository package.
This will allow us to re-use code easily with composer dependency management.
first we need to install composer and add composer.phar archive file location to $PATH variable
more instructions can be found on offical website
download composer installer
wget https://getcomposer.org/installer
Run installer
php installer
install composer globally (optionnaly rename file with --filename=[your_file_name])
php composer-setup.php --filename=composer
sudo mv composer /usr/local/bin/composer
test if it work by calling the cli tool without argument
composer
you shoud see something like
If you got any errors, refer to composer documentation
In my case, i will create a dead-simple config class that will allow me to create php config file containing multidimensionnal array with config value and easily retrieve and edit them inside my application.
first we're going to init the projet with composer
composer init
Now, let's add some code. We're going to create the Config class.
mkdir src
touch src/Config.php
<?php
class Config {
private $vars;
private static $instance;
private function __construct(){
$this->vars = require CONFIG_FILE;
}
public static function instance(){
if(!isset(self::$instance))
self::$instance = new Config;
return self::$instance;
}
private function get_item_rec($arr, $path){
if(!isset($arr) || empty($arr))
return NULL;
if(strpos($path, '.') === FALSE)
{
if(isset($arr[$path]))
return ($arr[$path]);
return NULL;
}
$curr_key = substr($path, 0, strpos($path, '.'));
$next_path = substr($path, strpos($path, '.') + 1);
return $this->get_item_rec($arr[$curr_key], $next_path);
}
private function set_item_rec(&$arr, $path, $value){
if(!isset($arr) || empty($arr)) return FALSE;
if(!isset($value)) return FALSE;
if(strpos($path, '.') === FALSE)
{
if(isset($arr[$path]))
{
$arr[$path] = $value;
return TRUE;
}
return FALSE;
}
$curr_key = substr($path, 0, strpos($path, '.'));
$next_path = substr($path, strpos($path, '.') + 1);
return $this->set_item_rec($arr[$curr_key], $next_path, $value);
}
public function get($name){
$item = $this->get_item_rec($this->vars, $name);
return ($item);
}
public function set($name, $value){
return $this->set_item_rec($this->vars, $name);
}
public function dump(){
print_r($this);
}
// Config
}
Then you will need to tell composer that we want to use PSR-4 autoloading.
Open composer.json and add the following at the root of the json object :
"autoload": {
"psr-4": {"YourVendorName\\": ""}
},
my full composer.json file:
{
"name": "erwan/easy-config",
"description": "easy configuration PHP class coupled with multi-dimensional array config php file",
"type": "PHP config class helper",
"authors": [
{
"name": "Erwan Touba",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {"Erwan\\": "src/"}
},
"require": {}
}
install composer autoloaded & vendor directory
composer install
2.Create your repository folder with satis
First we will install satis, more detailed info here
composer create-project composer/satis ~/satis --stability=dev --keep-vcs
Now we're gonna edit ~/satis/satis.json configuration file to setup repository infos
{
"name": "Config class composer repository",
"homepage": "https://google.fr",
"repositories": [
{ "type": "vcs", "url": "https://bitbucket.org/erwan_touba/apitech.git" }
],
"require-all": true
}
Then we generate the composer repository : php bin/satis build <configuration file> <build-dir>
example:
php ~/satis/bin/satis build ~/satis/satis.json satis_www
you can now enter satis_www directory and run php local dev server in order to test
cd satis_www
php -S localhost:8000
then open your web browser and go to the following url: localhost:8000
you should see something like this
we're going to create a new projet that will use our config class