Skip to content

Commit

Permalink
Created a example of usage
Browse files Browse the repository at this point in the history
  • Loading branch information
luismulinari committed Jan 27, 2016
1 parent 9e44f67 commit 0069c1e
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
83 changes: 83 additions & 0 deletions example/application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';

use Lcobucci\DependencyInjection\ContainerConfig;
use LuisMulinari\Consoleful\Command\ContainerAwareCommand;
use LuisMulinari\Consoleful\Application;
use Lcobucci\DependencyInjection\ContainerInjector;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;

class ExampleCommand extends ContainerAwareCommand
{
use ContainerInjector;

protected function configure()
{
$this->setName("example");
$this->setDescription('* Simple example');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$container = $this->getContainer();

$progress = new ProgressBar($output, 5);

$progress->start();

$i = 0;
while ($i++ < 5) {
$progress->advance();

sleep(1);
}

$progress->finish();

$output->writeln($container->getParameter('parameter.example'));
}
}

class DatabaseCommand extends ContainerAwareCommand
{
use ContainerInjector;

protected function configure()
{
$this->setName('database');
$this->setDescription('* Exemple with database');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
if (!class_exists('Doctrine\DBAL\Connection')) {
$output->write('You need to include "doctrine/dbal" package in your composer.json');

return false;
}

$databaseTables = $this->getContainer()->get('database.connection')->query('SHOW TABLES')->fetchAll();

$tableHelper = $this->getHelper('table');

$tableHelper->setHeaders(['Table name']);
$tableHelper->setRows($databaseTables);

$tableHelper->render($output);
}
}


$application = new Application(
'Application name',
'Version',
new ContainerConfig(__DIR__ . '/services.xml') // services.[xml|yml|php]
);

$application->add(new ExampleCommand());
$application->add(new DatabaseCommand());

$application->run();
24 changes: 24 additions & 0 deletions example/database.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="database.connection.params" type="collection">
<parameter key="dbname">__dbname__</parameter>
<parameter key="user">__user__</parameter>
<parameter key="password">__password__</parameter>
<parameter key="host">127.0.0.1</parameter>
<parameter key="driver">pdo_mysql</parameter>
</parameter>
</parameters>

<services>
<service id="database.connection" alias="doctrine.dbal.connection"/>

<service id="doctrine.dbal.configuration" class="Doctrine\DBAL\Configuration" />
<service id="doctrine.dbal.connection" class="Doctrine\DBAL\Connection" factory-class="Doctrine\DBAL\DriverManager" factory-method="getConnection">
<argument>%database.connection.params%</argument>
<argument type="service" id="doctrine.dbal.configuration" />
</service>
</services>
</container>
11 changes: 11 additions & 0 deletions example/services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<imports>
<import resource="database.xml"/>
</imports>

<parameters>
<parameter key="parameter.example">Example param from container</parameter>
</parameters>
</container>

0 comments on commit 0069c1e

Please sign in to comment.