8000 [Console] refactored helpers · Dahipster/symfony@7a0586e · GitHub
[go: up one dir, main page]

Skip to content

Commit 7a0586e

Browse files
committed
[Console] refactored helpers
1 parent e7dfdab commit 7a0586e

File tree

7 files changed

+72
-82
lines changed

7 files changed

+72
-82
lines changed

src/Symfony/Components/Console/Application.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Symfony\Components\Console\Command\Command;
1515
use Symfony\Components\Console\Command\HelpCommand;
1616
use Symfony\Components\Console\Command\ListCommand;
17+
use Symfony\Components\Console\Helper\HelperSet;
18+
use Symfony\Components\Console\Helper\FormatterHelper;
19+
use Symfony\Components\Console\Helper\DialogHelper;
1720

1821
/*
1922
* This file is part of the symfony framework.
@@ -53,6 +56,7 @@ class Application
5356
protected $catchExceptions;
5457
protected $autoExit;
5558
protected $definition;
59+
protected $helperSet;
5660

5761
/**
5862
* Constructor.
@@ -68,6 +72,10 @@ public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
6872
$this->autoExit = true;
6973
$this->commands = array();
7074
$this->aliases = array();
75+
$this->helperSet = new HelperSet(array(
76+
new FormatterHelper(),
77+
new DialogHelper(),
78+
));
7179

7280
$this->addCommand(new HelpCommand());
7381
$this->addCommand(new ListCommand());
@@ -200,6 +208,26 @@ public function doRun(InputInterface $input, OutputInterface $output)
200208
return is_numeric($statusCode) ? $statusCode : 0;
201209
}
202210

211+
/**
212+
* Set a helper set to be used with the command.
213+
*
214+
* @param HelperSet $helperSet The helper set
215+
*/
216+
public function setHelperSet(HelperSet $helperSet)
217+
{
218+
$this->helperSet = $helperSet;
219+
}
220+
221+
/**
222+
* Get the helper set associated with the command
223+
*
224+
* @return HelperSet The HelperSet isntance associated with this command
225+
*/
226+
public function getHelperSet()
227+
{
228+
return $this->helperSet;
229+
}
230+
203231
/**
204232
* Gets the InputDefinition related to this Application.
205233
*

src/Symfony/Components/Console/Command/Command.php

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
use Symfony\Components\Console\Input\InputInterface;
99
use Symfony\Components\Console\Output\OutputInterface;
1010
use Symfony\Components\Console\Application;
11-
use Symfony\Components\Console\Helper\HelperSet;
12-
use Symfony\Components\Console\Helper\FormatterHelper;
13-
use Symfony\Components\Console\Helper\InteractHelper;
1411

1512
/*
1613
* This file is part of the symfony framework.
@@ -45,26 +42,15 @@ class Command
4542
/**
4643
* Constructor.
4744
*
48-
* @param string $name
49-
* @param HelperSet $helperSet A helper set instance
45+
* @param string $name The name of the command
5046
*/
51-
public function __construct($name = null, HelperSet $helperSet = null)
47+
public function __construct($name = null)
5248
{
5349
$this->definition = new InputDefinition();
5450
$this->ignoreValidationErrors = false;
5551
$this->applicationDefinitionMerged = false;
5652
$this->aliases = array();
5753

58-
if (null === $helperSet)
59-
{
60-
$helperSet = new HelperSet(array(
61-
new FormatterHelper(),
62-
new InteractHelper(),
63-
));
64-
}
65-
66-
$this->setHelperSet($helperSet);
67-
6854
if (null !== $name)
6955
{
7056
$this->setName($name);
@@ -403,39 +389,17 @@ public function getSynopsis()
403389
}
404390

405391
/**
406-
* Set a helper set to be used with the command.
407-
*
408-
* @param HelperSet $helperSet The helper set
409-
*/
410-
public function setHelperSet(HelperSet $helperSet)
411-
{
412-
$this->helperSet = $helperSet;
413-
414-
$helperSet->setCommand($this);
415-
}
416-
417-
/**
418-
* Get the helper set associated with the command
419-
*
420-
* @return HelperSet
421-
*/
422-
public function getHelperSet()
423-
{
424-
return $this->helperSet;
425-
}
426-
427-
/**
428-
* Gets a helper value.
392+
* Gets a helper instance by name.
429393
*
430-
* @param string $name The helper name
394+
* @param string $name The helper name
431395
*
432396
* @return mixed The helper value
433397
*
434398
* @throws \InvalidArgumentException if the helper is not defined
435399
*/
436-
public function __get($name)
400+
protected function getHelper($name)
437401
{
438-
return $this->helperSet->get($name);
402+
return $this->application->getHelperSet()->get($name);
439403
}
440404

441405
/**

src/Symfony/Components/Console/Helper/InteractHelper.php renamed to src/Symfony/Components/Console/Helper/DialogHelper.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,14 @@
1414
*/
1515

1616
/**
17-
* The Interact class provides helpers to interact with the user.
17+
* The Dialog class provides helpers to interact with the user.
1818
*
1919
* @package symfony
20-
* @subpackage cli
20+
* @subpackage console
2121
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
2222
*/
23-
class InteractHelper extends Helper
23+
class DialogHelper extends Helper
2424
{
25-
/**
26-
* Returns the helper's canonical name
27-
*/
28-
public function getName()
29-
{
30-
return 'interact';
31-
}
32-
3325
/**
3426
* Asks a question to the user.
3527
*
@@ -116,4 +108,12 @@ public function askAndValidate(OutputInterface $output, $question, \Closure $val
116108
throw $error;
117109
// @codeCoverageIgnoreEnd
118110
}
111+
112+
/**
113+
* Returns the helper's canonical name
114+
*/
115+
public function getName()
116+
{
117+
return 'dialog';
118+
}
119119
}

src/Symfony/Components/Console/Helper/FormatterHelper.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@
2020
*/
2121
class FormatterHelper extends Helper
2222
{
23-
/**
24-
* Returns the helper's canonical name
25-
*/
26-
public function getName()
27-
{
28-
return 'formatter';
29-
}
30-
3123
/**
3224
* Formats a message within a section.
3325
*
@@ -86,5 +78,12 @@ protected function strlen($string)
8678
{
8779
return function_exists('mb_strlen') ? mb_strlen($string) : strlen($string);
8880
}
89-
}
9081

82+
/**
83+
* Returns the helper's canonical name
84+
*/
85+
public function getName()
86+
{
87+
return 'formatter';
88+
}
89+
}

src/Symfony/Components/Console/Helper/Helper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
namespace Symfony\Components\Console\Helper;
44

55
/*
6-
* This file is part of the symfony package.
6+
* This file is part of the symfony framework.
77
*
88
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
99
*
10-
* For the full copyright and license information, please view the LICENSE
11-
* file that was distributed with this source code.
10+
* This source file is subject to the MIT license that is bundled
11+
* with this source code in the file LICENSE.
1212
*/
1313

1414
/**
1515
* Helper is the base class for all helper classes.
1616
*
1717
* @package symfony
18-
* @subpackage templating
18+
* @subpackage console
1919
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
2020
*/
2121
abstract class Helper implements HelperInterface

src/Symfony/Components/Console/Helper/HelperInterface.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,23 @@
33
namespace Symfony\Components\Console\Helper;
44

55
/*
6-
* This file is part of the symfony package.
6+
* This file is part of the symfony framework.
77
*
88
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
99
*
10-
* For the full copyright and license information, please view the LICENSE
11-
* file that was distributed with this source code.
10+
* This source file is subject to the MIT license that is bundled
11+
* with this source code in the file LICENSE.
1212
*/
1313

1414
/**
1515
* HelperInterface is the interface all helpers must implement.
1616
*
1717
* @package symfony
18-
* @subpackage templating
18+
* @subpackage console
1919
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
2020
*/
2121
interface HelperInterface
2222
{
23-
/**
24-
* Returns the canonical name of this helper.
25-
*
26-
* @return string The canonical name
27-
*/
28-
function getName();
29-
3023
/**
3124
* Sets the helper set associated with this helper.
3225
*
@@ -40,4 +33,11 @@ function setHelperSet(HelperSet $helperSet = null);
4033
* @return HelperSet A HelperSet instance
4134
*/
4235
function getHelperSet();
36+
37+
/**
38+
* Returns the canonical name of this helper.
39+
*
40+
* @return string The canonical name
41+
*/
42+
function getName();
4343
}

src/Symfony/Components/Console/Helper/HelperSet.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
use Symfony\Components\Console\Command\Command;
66

77
/*
8-
* This file is part of the symfony package.
8+
* This file is part of the symfony framework.
99
*
1010
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
1111
*
12-
* For the full copyright and license information, please view the LICENSE
13-
* file that was distributed with this source code.
12+
* This source file is subject to the MIT license that is bundled
13+
* with this source code in the file LICENSE.
1414
*/
1515

1616
/**
1717
* HelperSet represents a set of helpers to be used with a command.
1818
*
1919
* @package symfony
20-
* @subpackage templating
20+
* @subpackage console
2121
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
2222
*/
2323
class HelperSet
@@ -102,4 +102,3 @@ public function getCommand()
102102
return $this->command;
103103
}
104104
}
105-

0 commit comments

Comments
 (0)
0