33namespace PhpZone \Shell ;
44
55use PhpZone \PhpZone \Extension \Extension ;
6- use PhpZone \Shell \Exception \Command \NoScriptFoundException ;
76use PhpZone \Shell \Process \ProcessFactory ;
87use Symfony \Component \DependencyInjection \ContainerBuilder ;
98use Symfony \Component \DependencyInjection \Definition ;
9+ use Symfony \Component \OptionsResolver \Exception \InvalidOptionsException ;
10+ use Symfony \Component \OptionsResolver \Exception \MissingOptionsException ;
11+ use Symfony \Component \OptionsResolver \OptionsResolver ;
1012
1113class Shell implements Extension
1214{
@@ -16,16 +18,37 @@ class Shell implements Extension
1618 /** @var ProcessFactory */
1719 private $ processFactory ;
1820
21+ /** @var OptionsResolver */
22+ private $ optionsResolver ;
23+
1924 public function load (ContainerBuilder $ container )
2025 {
2126 $ this ->container = $ container ;
2227 $ this ->processFactory = new ProcessFactory ();
2328
29+ $ this ->optionsResolver = new OptionsResolver ();
30+ $ this ->configureOptions ($ this ->optionsResolver );
31+
2432 $ config = $ container ->getParameter (get_class ($ this ));
2533
2634 $ this ->createAndRegisterDefinitions ($ config );
2735 }
2836
37+ private function configureOptions (OptionsResolver $ optionsResolver )
38+ {
39+ $ optionsResolver ->setRequired (array (
40+ 'script ' ,
41+ ));
42+
43+ $ optionsResolver ->setDefaults (array (
44+ 'description ' => null ,
45+ ));
46+
47+ $ optionsResolver ->setAllowedTypes (array (
48+ 'script ' => 'array ' ,
49+ ));
50+ }
51+
2952 private function createAndRegisterDefinitions (array $ config = array ())
3053 {
3154 foreach ($ config as $ commandName => $ commandOptions ) {
@@ -41,20 +64,12 @@ private function createAndRegisterDefinitions(array $config = array())
4164 *
4265 * @return Definition
4366 *
44- * @throws NoScriptFoundException
67+ * @throws MissingOptionsException
68+ * @throws InvalidOptionsException
4569 */
4670 private function generateCommandDefinition ($ commandName , array $ commandOptions )
4771 {
48- if (empty ($ commandOptions ['script ' ])) {
49- throw new NoScriptFoundException (sprintf (
50- 'Defined command "%s" does not have any script ' ,
51- $ commandName
52- ));
53- }
54-
55- if (empty ($ commandOptions ['description ' ])) {
56- $ commandOptions ['description ' ] = null ;
57- }
72+ $ commandOptions = $ this ->optionsResolver ->resolve ($ commandOptions );
5873
5974 $ definition = new Definition ('PhpZone\Shell\Command\ScriptCommand ' );
6075 $ definition ->setArguments (
0 commit comments