Aşağıdaki örnekte bağımsız bir konsol komut uygulaması yaratacağız ve onu başka bir uygulama içinde kullanacağız. Ayrıca ek ve örnek olarak extradan paketlerde yükleyeceğiz.


Proje yapısı


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

Composer.json dosyası


Composer.json dosyasını yarattıktan sonra, composer install komutunu çalıştırın.


{
"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 dosyası


Önce bin/console dosyasını yaratın ve ona chmod +x bin/console komutu ile çalıştırılma izni verin.


#!/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 dosyası


# 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 dosyası


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

Build.xml dosyası


<?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

Diğer uygulamanın composer.json dosyası


Bağımsız paketi ana projenin composer.json dosyasına ekledikten sonra, composer update inanzzz/hello-console-command komutunu çalıştırın. Bu ana projede bin/console sembolik linkini oluşturur.


{
"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