Skip to content

Commit 9c84e09

Browse files
committed
initial commit
0 parents  commit 9c84e09

File tree

7 files changed

+234
-0
lines changed

7 files changed

+234
-0
lines changed

Knp/Command/Command.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Knp\Command;
4+
5+
use Symfony\Component\Console\Command\Command as BaseCommand;
6+
7+
class Command extends BaseCommand
8+
{
9+
public function getSilexApplication()
10+
{
11+
return $this->getApplication()->getSilexApplication();
12+
}
13+
14+
public function getProjectDirectory()
15+
{
16+
return $this->getApplication()->getProjectDirectory();
17+
}
18+
}

Knp/Console/Application.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Knp\Console;
4+
5+
use Symfony\Component\Console\Application as BaseApplication;
6+
use Silex\Application as SilexApplication;
7+
8+
class Application extends BaseApplication
9+
{
10+
private $silexApplication;
11+
12+
private $rootDirectory;
13+
14+
public function __construct(SilexApplication $application, $projectDirectory, $name = 'UNKNOWN', $version = 'UNKNOWN')
15+
{
16+
parent::__construct($name, $version);
17+
18+
$this->silexApplication = $application;
19+
$this->projectDirectory = $projectDirectory;
20+
}
21+
22+
public function getSilexApplication()
23+
{
24+
return $this->silexApplication;
25+
}
26+
27+
public function getProjectDirectory()
28+
{
29+
return $this->projectDirectory;
30+
}
31+
}

Knp/Console/Event.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Knp\Console;
4+
5+
use Symfony\Component\EventDispatcher\Event as BaseEvent;
6+
use Knp\Console\Application;
7+
8+
class Event extends BaseEvent
9+
{
10+
private $application;
11+
12+
public function __construct(Application $application)
13+
{
14+
$this->application = $application;
15+
}
16+
17+
public function getApplication()
18+
{
19+
return $this->application;
20+
}
21+
}

Knp/Console/Events.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Knp\Console;
4+
5+
class Events
6+
{
7+
const INIT = 'console.init';
8+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Knp\Provider;
4+
5+
use Silex\ServiceProviderInterface;
6+
use Silex\Application;
7+
8+
use Knp\Console\Application as ConsoleApplication;
9+
use Knp\Console\Events as ConsoleEvents;
10+
use Knp\Console\Event as ConsoleEvent;
11+
12+
class ConsoleServiceProvider implements ServiceProviderInterface
13+
{
14+
public function register(Application $app)
15+
{
16+
$app['console'] = $app->share(function() use ($app) {
17+
$application = new ConsoleApplication(
18+
$app,
19+
$app['console.project_directory'],
20+
$app['console.name'],
21+
$app['console.version']
22+
);
23+
24+
$app['dispatcher']->dispatch(ConsoleEvents::INIT, new ConsoleEvent($application));
25+
26+
return $application;
27+
});
28+
}
29+
}

README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# ConsoleServiceProvider
2+
3+
Provides a `Symfony\Component\Console` based console for Silex.
4+
5+
## Install
6+
7+
Add `knplabs/console-service-provider` to your `composer.json` and register the service:
8+
9+
```php
10+
<?php
11+
12+
use Knp\Provider\ConsoleServiceProvider;
13+
14+
$app->register(new ConsoleServiceProvider(), array(
15+
'console.name' => 'MyApplication',
16+
'console.version' => '1.0.0',
17+
'console.project_directory' => __DIR__.'/..'
18+
));
19+
20+
?>
21+
```
22+
23+
You can now copy the `console` executable in whatever place you see fit, and tweak it to your needs. You will need a way to fetch your silex application, the most common way is to return it from your bootstrap:
24+
25+
```php
26+
<?php
27+
28+
$app = new Silex\Application();
29+
30+
// your beautiful silex bootstrap
31+
32+
return $app;
33+
34+
?>
35+
```
36+
37+
For the rest of this document, we will assume you do have an `app` directory, so the `console` executable will be located at `app/console`.
38+
39+
## Usage
40+
41+
Use the console just like any `Symfony\Component` based console:
42+
43+
```
44+
$ app/console my:command
45+
```
46+
47+
## Write commands
48+
49+
Your commands should extend `Knp\Command\Command` to have access to the 2 useful following commands:
50+
51+
* `getSilexApplication`, which returns the silex application
52+
* `getProjectDirectory`, which returns your project's root directory (as configured earlier)
53+
54+
I know, it's a lot to learn, but it's worth the pain.
55+
56+
## Register commands
57+
58+
There are two ways of registering commands to the console application.
59+
60+
### Directly access the console application from the `console` executable
61+
62+
Open up `app/console`, and stuff your commands directly into the console application:
63+
64+
```php
65+
#!/usr/bin/env php
66+
<?php
67+
68+
set_time_limit(0);
69+
70+
$app = require_once __DIR__.'/bootstrap.php';
71+
72+
use My\Command\MyCommand;
73+
74+
$application = $app['console'];
75+
$application->add(new MyCommand());
76+
$application->run();
77+
78+
?>
79+
```
80+
81+
### Use the Event Dispatcher
82+
83+
This way is intended for use by provider developers and exposes an unobstrusive way to register commands in 3 simple steps:
84+
85+
1. Register a listener to the `ConsoleEvents::INIT` event
86+
2. ???
87+
3. PROFIT!
88+
89+
Example:
90+
91+
```php
92+
<?php
93+
94+
use My\Command\MyCommand;
95+
use Knp\Console\Events as ConsoleEvents;
96+
use Knp\Console\Event as ConsoleEvent;
97+
98+
$app['dispatcher']->addListener(ConsoleEvents::INIT, function(ConsoleEvent $event) {
99+
$app = $event->getApplication();
100+
$app->add(new MyCommand());
101+
});
102+
103+
?>
104+
```

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "knplabs/console-service-provider",
3+
"type": "library",
4+
"description": "console service provider for Silex",
5+
"keywords": [ "console", "silex" ],
6+
"homepage": "http://knplabs.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "KnpLabs",
11+
"homepage": "http://knplabs.com"
12+
}
13+
],
14+
"require": {
15+
"php": ">=5.3.2",
16+
"symfony/console": "2.1.*"
17+
},
18+
"autoload": {
19+
"psr-0": {
20+
"Knp\\": ""
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)