47
Symfony & Cli App Symfony ile Commandline Uygulama Geliştirmek

Symfony ile command line uygulama geliştirme

Embed Size (px)

Citation preview

Page 1: Symfony ile command line uygulama geliştirme

Symfony & Cli App

Symfony ile Commandline Uygulama Geliştirmek

Page 3: Symfony ile command line uygulama geliştirme

Neden?

Php ile commanline uygulama geliştirebilirsiniz

Ancak bu commandline uygulamaları Php ile

geliştirmeniz için bir tavsiye değildir.

Page 4: Symfony ile command line uygulama geliştirme

Temel Yöntemler

http://php.net/manual/en/features.commandline.php

Page 5: Symfony ile command line uygulama geliştirme

Popüler Araçlar ve Yöntemler

1. phar

2. composer

3. symfony/console

Page 6: Symfony ile command line uygulama geliştirme

PHAR

(PHP Archive)

Page 7: Symfony ile command line uygulama geliştirme

1. PHAR (PHP Archive)

Bir PHP ugulamasındaki tüm dosyaları tek bir

dosyada toplayarak “easy distribution”

sağlayan arşivleme metodu

http://us1.php.net/manual/en/book.phar.php

Page 8: Symfony ile command line uygulama geliştirme

Phar: Kurulum & Ayar

Built-in >= PHP v5.3.0

Pecl Extension < PHP v5.3.0

php.ini : phar.readonly = 0

Page 9: Symfony ile command line uygulama geliştirme

Phar: Paketleme

$sourcePath = "./src";$buildPath = "./build";

$phar = new Phar($buildPath . "/app.phar", FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME, "app.phar");$phar["index.php"] = file_get_contents($srcRoot . "/index.php");$phar["helper.php"] = file_get_contents($srcRoot . "/helper.php");$phar->setStub($phar->createDefaultStub("index.php"));

Page 10: Symfony ile command line uygulama geliştirme

phar-composer

https://github.com/clue/phar-composer

Page 11: Symfony ile command line uygulama geliştirme

Composer

Page 12: Symfony ile command line uygulama geliştirme

2. Composer

Php için “dependency manager”

https://getcomposer.org/

Page 13: Symfony ile command line uygulama geliştirme

Composer: Kurulum

Curl ile$ curl -sS https://getcomposer.org/installer | php

Php ile$ php -r "readfile('https://getcomposer.org/installer');" | php

Source https://getcomposer.org/download/

Page 14: Symfony ile command line uygulama geliştirme

Composer: composer init

$ composer init

Page 15: Symfony ile command line uygulama geliştirme

Composer: composer.json

{"name": "okulbilisim/sample","description": "Sample composer project","require": {

"guzzle/guzzle": "~3.9@dev"},"license": "MIT","authors": [

{"name": "hasantayyar","email": "[email protected]"

}],"minimum-stability": "dev"

}

Page 16: Symfony ile command line uygulama geliştirme

Composer: bin folder

{"bin": ["bin/script_demo", "bin/other_script_demo"]

}

Page 17: Symfony ile command line uygulama geliştirme

symfony/console

Page 18: Symfony ile command line uygulama geliştirme

3. symfony/console

Page 19: Symfony ile command line uygulama geliştirme

symfony/console

"require": {

"php": ">=5.4.1",

"symfony/console": "2.4.x-dev"

}

$ composer install

$ touch console && chmod +x console

$ echo "#/usr/bin/env php" > console

$ ./console

Page 20: Symfony ile command line uygulama geliştirme

time_limit

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

Page 21: Symfony ile command line uygulama geliştirme

timezone

#!/usr/bin/env php<?phpset_time_limit(0);date_default_timezone_set('Europe/Istanbul');

Page 22: Symfony ile command line uygulama geliştirme

Sf Console: hello

#!/usr/bin/env php<?phpset_time_limit(0);date_default_timezone_set('Europe/Istanbul');require_once __DIR__ . '/vendor/autoload.php';use Symfony\Component\Console\Application;$app = new Application();$app->run();

Page 23: Symfony ile command line uygulama geliştirme

Sf Console: Default Command

use Symfony\Component\Console\Application;use Demo\Commands\HelloCommand;

$command = new HelloCommand();$app = new Application();$application->setDefaultCommand($command->getName());$application->run();

Page 24: Symfony ile command line uygulama geliştirme

Sf Console: input

Option & Argument

./console say:hello jesus --yell

Page 25: Symfony ile command line uygulama geliştirme

Sf Console: input

Option & Argument

./console say:hello jesus --yellcommand arg. option

use Symfony\Component\Console\Input\InputInterface;use Symfony\Component\Console\Input\InputArgument;use Symfony\Component\Console\Input\InputOption;

Page 26: Symfony ile command line uygulama geliştirme

Sf Console: input / arguments

…->addArgument(

'name',InputArgument::OPTIONAL, // InputArgument::REQUIRED | InputArgument::IS_ARRAY'Who do you want to say hello?'

)

Page 27: Symfony ile command line uygulama geliştirme

Sf Console: input / options

…->addOption(

'yell',null,InputOption::VALUE_NONE,/*InputOption::VALUE_IS_ARRAY , InputOption::VALUE_NONEInputOption::VALUE_REQUIRED , InputOption::VALUE_OPTIONAL*/'ALL CAPS'

)

Page 28: Symfony ile command line uygulama geliştirme

Sf Console: output

use Symfony\Component\Console\Output\OutputInterface;

Page 29: Symfony ile command line uygulama geliştirme

Sf Console: Renklendirme

$output->writeln('<info>foo</info>');

$output->writeln('<comment>foo</comment>');

$output->writeln('<question>foo</question>');

$output->writeln('<error>foo</error>');

Page 30: Symfony ile command line uygulama geliştirme

Sf Console: Renklendirme

$style = new OutputFormatterStyle('red', 'yellow', array('bold', 'blink'));

$output->getFormatter()->setStyle('yanyan', $style);

$output->writeln('<yanyan>yanmalı</yanyan>');

Page 31: Symfony ile command line uygulama geliştirme

Sf Console: Renklendirme

$output->writeln('<fg=black;bg=cyan;options=bold>'.'ateşli'.'</fg=black;bg=cyan;options=bold>');

Page 32: Symfony ile command line uygulama geliştirme

Sf Console: Helpers

1. Question Helper

2. Formatter Helper

3. ProgressBar

4. Table

Page 33: Symfony ile command line uygulama geliştirme

Sf Console: Question Helper

use Symfony\Component\Console\Question\ConfirmationQuestion;$helper = $this->getHelper('question');

$helper = $this->getHelper('question');$question = new ConfirmationQuestion('Continue with this action?', false);

if (!$helper->ask($input, $output, $question)) {return;

}

Page 34: Symfony ile command line uygulama geliştirme

Sf Console: Question Helper

use Symfony\Component\Console\Question\ChoiceQuestion;

$helper = $this->getHelper('question');$question = new ChoiceQuestion(

'Please select your favorite color (defaults to red)',array('red', 'blue', 'yellow'),0

);$question->setErrorMessage('Color %s is invalid.');$color = $helper->ask($input, $output, $question);$output->writeln('You have just selected: '.$color);

Page 35: Symfony ile command line uygulama geliştirme

Sf Console: Question Helper

// Autocomplete

$bundles = array('AcmeDemoBundle', 'AcmeBlogBundle', 'AcmeStoreBundle');$question = new Question('Please enter the name of a bundle', 'FooBundle');$question->setAutocompleterValues($bundles);

Page 36: Symfony ile command line uygulama geliştirme

Sf Console: Question Helper

// Hidden input

$question->setHidden(true);$question->setHiddenFallback(false);

Page 37: Symfony ile command line uygulama geliştirme

Sf Console: Question Helper

// Validation

$question->setValidator(function ($answer) {if (strlen($answer)<10 ) {throw new \RuntimeException(

'The answer is too short');}

return $answer;});$question->setMaxAttempts(2);$name = $helper->ask($input, $output, $question);

Page 38: Symfony ile command line uygulama geliştirme

Çıktı renklendirmesini özelleştirmek için kullanılır.

$formatter = $this->getHelper('formatter');$formattedLine = $formatter->formatSection('Adım 1','Bu adımla ilgili metin');$output->writeln($formattedLine);

[Adım 1] Bu adımla ilgili metin .

Sf Console: Formatter Helper

Page 39: Symfony ile command line uygulama geliştirme

$errorMsg = array('Error!', 'Hata olustu');$block = $formatter->formatBlock($errorMsg, 'error');$output->writeln($block);

Sf Console: Formatter Helper

[Error!]Hata Oluştu

Page 40: Symfony ile command line uygulama geliştirme

Sf Console: Progress Helper

Progress bar göstermek için

use Symfony\Component\Console\Helper\ProgressBar;$progress = new ProgressBar($output, 50);$progress->start();$i = 0;while($i++ < 50) {

$progress->advance();$progress->finish();

}

Page 41: Symfony ile command line uygulama geliştirme

Sf Console: Table Helper

use Symfony\Component\Console\Helper\Table;

$table = new Table($output);

$table->setHeaders(array('ISBN', 'Title', 'Author'))->setRows(array(

array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),

));

$table->render();

Page 42: Symfony ile command line uygulama geliştirme

Sf Console: Table Helper

use Symfony\Component\Console\Helper\Table;use Symfony\Component\Console\Helper\TableSeparator;

$table->setRows(array(

array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),new TableSeparator(),array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),

));

Page 43: Symfony ile command line uygulama geliştirme

Sf Console: Table Helper

use Symfony\Component\Console\Helper\Table;

$table->setStyle('default');//$table->setStyle('compact');$table->render();

Page 44: Symfony ile command line uygulama geliştirme

Sf Console: Table Helper

setPaddingChar()setHorizontalBorderChar()setVerticalBorderChar()setCrossingChar()setCellHeaderFormat()setCellRowFormat()setBorderFormat()setPadType()

Page 45: Symfony ile command line uygulama geliştirme

Sf Console: Dahası

1. Cli app içinde EventDispatcher kullanılabilir

2. Psr\Log\LoggerInterface

3. $this->getHelper('debug_formatter');

4. $helper = $this->getHelper('process'); // >= symfony 2.6

Page 46: Symfony ile command line uygulama geliştirme

1. https://github.com/maximebf/ConsoleKit

2. https://github.com/wp-cli/php-cli-tools

3. https://github.com/ulrichsg/getopt-php

4. https://github.com/nette/command-line

5. https://github.com/MrRio/shellwrap

6. https://github.com/g4code/commando

Diğer Paketler

Page 47: Symfony ile command line uygulama geliştirme

Sorular?

@htayyar

github.com/hasantayyar

hasantayyar.net