In example below, we're going to create a console command as a standalone application and use it in an another application. We'll also include basic build packages as an example.


Project structure


my-standalone-app
Command
HelloCommand.php
bin
console
.gitignore
build.xml
composer.json

Composer.json file


After creating the composer.json in project root, you need to run composer install command.


{
"name": "inanzzz/hello-console-command",
"description": "Say hello package.",
"type": "library",
"version": "1.0.0",
"autoload": {
"psr-0": {
"Command": ""
}
},
"require": {
"symfony/console": "2.*"
},
"require-dev": {
"phing/phing": "2.11.0",
"fabpot/php-cs-fixer": "1.9"
},
"minimum-stability": "stable",
"bin": [
"bin/console"
],
"config": {
"bin-dir": "bin"
}
}

Console file


Create bin/console file and grant executable permissions to it with chmod +x bin/console command.


#!/usr/bin/env php
<?php
set_time_limit(0);

(@include_once __DIR__ . '/../vendor/autoload.php') || @include_once __DIR__ . '/../../../autoload.php';

use Command\HelloCommand;
use Symfony\Component\Console\Application;

$app = new Application('My CLI Application', '1.0.0');
$app->add(new HelloCommand());
$app->run();

HelloCommand.php file


# my-standalone-app/Command/HelloCommand.php
<?php

namespace Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class HelloCommand extends Command
{
protected function configure()
{
$this
->setName('say:hello')
->setDescription('Say hello to someone')
->addArgument(
'name',
InputArgument::OPTIONAL,
'Who do you want to say hello?'
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
$hello = $name ? 'Hello '.$name : 'Hello';

$output->writeln($hello);
}
}

.gitignore file


composer.lock
/vendor/
/bin/*
!/bin/console

Build.xml file


<?xml version="1.0" encoding="UTF-8"?>

<project name="hello" default="default" basedir=".">
<!-- GLOBAL VARIABLES -->
<property name="source.folder" value="Command" />
<!-- END -->

<!-- FILESET -->
<fileset id="sourcecode" dir="${project.basedir}/${source.folder}">
<include name="*.php" />
<include name="**/*.php" />
</fileset>
<!-- END -->

<!-- AVAILABLE COMMANDS -->
<target name="default"
description="Running full test suite ..."
depends="composer.update, composer.install, test.phpcsfixer" />
<!-- END -->

<!-- COMPOSER SELF UPDATE -->
<target name="composer.update">
<echo msg="Running composer self-update ..." />
<exec logoutput="true" checkreturn="true" passthru="true" command="composer self-update" dir="./" />
</target>
<!-- END -->

<!-- COMPOSER INSTALL -->
<target name="composer.install">
<echo msg="Running composer install ..." />
<exec logoutput="true" checkreturn="true" passthru="true" command="composer install" dir="./" />
</target>
<!-- END -->

<!-- PHP-CS-FIXER -->
<target name="test.phpcsfixer">
<echo msg="Checking coding standards ..." />
<exec logoutput="true" checkreturn="true" passthru="true"
command="bin/php-cs-fixer fix ${source.folder} --dry-run --diff --verbose
--fixers=-yoda_conditions,-phpdoc_align,short_array_syntax"
dir="./" />
</target>
<!-- END -->
</project>

Test


my-standalone-app$ bin/console say:hello inanzzz
Hello inanzzz

Composer.json file of main application


After including your standalone package in main composer.json file, run composer update inanzzz/hello-console-command command. This will create a symlink as bin/console in main project.


{
"name": "my/main-app",
"type": "project",
"description": "Main application",
"autoload": {
"psr-0": { "": "src/" }
},
"repositories": [
{
"type": "git",
"url": "git@github.com:inanzzz/hello-console-command.git"
}
],
"require": {
"inanzzz/hello-console-command": "dev-master"
},
"minimum-stability": "stable"
}

Test


main-app$ bin/console say:hello inanzzz
Hello inanzzz