Merge lp://staging/~miguel.trias/gnupo/20090910_ArgumentInterpretation into lp://staging/gnupo

Proposed by Miguel Trias
Status: Merged
Merged at revision: not available
Proposed branch: lp://staging/~miguel.trias/gnupo/20090910_ArgumentInterpretation
Merge into: lp://staging/gnupo
Diff against target: None lines
To merge this branch: bzr merge lp://staging/~miguel.trias/gnupo/20090910_ArgumentInterpretation
Reviewer Review Type Date Requested Status
Fernando Ipar Approve
Review via email: mp+11541@code.staging.launchpad.net
To post a comment you must log in.
Revision history for this message
Miguel Trias (miguel.trias) wrote :

I've Added ArgumentInterpreter class for specific gnupo execution arguments interpretation, based on method calling instead of specific argument query. This separates argument parsing and naming of argument using (referred now as "running configuration") in our code.

I also modified gnupo.cli.php according this. Added help parameter interpretation and a minimum help message only for illustration purpose.

Example:

Prior to this in the code we have to write:
$MainContext = Main::getInstance();
$MainContext->ParameterHolder->get('performSQL');

Now we can use this way:
$MainContext = Main::getInstance();
$MainContext->ArgumentInterpreter->userWantsToPerformSQL();

This enables us to change the parameter name for future releases among other things.

Revision history for this message
Fernando Ipar (fipar) wrote :

I'm about to do the merge.
I'd vote for shorter method names though, i.e.,

$MainContext->ArgumentInterpreter->performSQL();

Still, that's eventually the end developer's choice so :)

review: Approve
Revision history for this message
Miguel Trias (miguel.trias) wrote :

Yes, as you said it's developers choice, but I have to say that this method
(for example \Gnupo\Runtime\ArgumentInterpretation::performSQL() does NOT
perform SQL itself. It only says "yes, the user wants you to perform the
SQL" or "no it does'nt". It interprets the arguments into a verb form. Form
example ( -h => userRequestedHelp() )
Is for that reason and to mantain independence between the argument name
(-h) from what it does ( showHelp() ) that i've decided to put that names
such as getAutoloadingPaths() instead of addPath()

Also this type of methods surely will be called no more than once or twice
from the core algorithm so we would'nt be tired of typing this.

I insist. I agree that It's developers choice and I agree with writing the
smallest code possible.

On Mon, Sep 14, 2009 at 4:09 PM, Fernando Ipar <email address hidden>wrote:

> Review: Approve
> I'm about to do the merge.
> I'd vote for shorter method names though, i.e.,
>
> $MainContext->ArgumentInterpreter->performSQL();
>
> Still, that's eventually the end developer's choice so :)
> --
>
> https://code.launchpad.net/~miguel.trias/gnupo/20090910_ArgumentInterpretation/+merge/11541
> You are the owner of
> lp:~miguel.trias/gnupo/20090910_ArgumentInterpretation.
>

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/Annotation/Docblock.class.php'
2--- lib/Annotation/Docblock.class.php 2009-09-09 14:54:18 +0000
3+++ lib/Annotation/Docblock.class.php 2009-09-10 15:46:31 +0000
4@@ -35,6 +35,11 @@
5 return ($this->getAnnotationStartingPosition($Annotation)!==FALSE);
6 }
7
8+ /**
9+ * Returns an integer representing the starting position of a the $Annotation annotation (the @ location)
10+ * @param string $Annotation
11+ * @return integer
12+ */
13 private function getAnnotationStartingPosition($Annotation)
14 {
15 return strpos(strtoupper($this->text),strtoupper('@'.$Annotation));
16
17=== modified file 'lib/CLI/OutputController.class.php'
18--- lib/CLI/OutputController.class.php 2009-09-09 18:41:47 +0000
19+++ lib/CLI/OutputController.class.php 2009-09-10 16:43:59 +0000
20@@ -98,6 +98,7 @@
21 $this->coloredMessage($message,$this->getColorByRelevancyLevel($m['relevancy']));
22 }
23 }
24+ echo PHP_EOL;
25 return true;
26 }
27
28
29=== added file 'lib/GnupoParser/ArgumentInterpreter.class.php'
30--- lib/GnupoParser/ArgumentInterpreter.class.php 1970-01-01 00:00:00 +0000
31+++ lib/GnupoParser/ArgumentInterpreter.class.php 2009-09-10 16:43:59 +0000
32@@ -0,0 +1,47 @@
33+<?php
34+
35+namespace Gnupo\Parser
36+{
37+
38+ use \Gnupo\Cli\ParameterHolder as ParameterHolder;
39+
40+ /**
41+ * GnupoArgumentInterpreter Interprets a set of values of arguments parsed from $argv and given by a ParameterHolder object to the constructor, and gives a OO, Gnupo-specific interface to this values. This is to encapsulate argument interpretation and separate it from argument parsing, and to separate naming argument from using its values.
42+ * For example, the argument controlling verbosity is --v / --vv / --vvv this could be changed in the future without need to change client code. All we need is a method to query the verbosity level independent of the argument used for this.
43+ * So in client code you must GnupoArgumentInterpreter::getVerbosityLevel() use instead of ParameterHolder::get('vv') / ParameterHolder->get('vvv'), etc.
44+ * @author miguel.trias
45+ * @version 2009-09-10
46+ */
47+ class ArgumentInterpreter
48+ {
49+
50+ /**
51+ * @var ParameterHolder
52+ */
53+ private $Arguments;
54+
55+ public function __construct(ParameterHolder $Arguments)
56+ {
57+ $this->Arguments = $Arguments;
58+ }
59+
60+ /**
61+ * Returns the verbosity level specified by arguments or the default value
62+ * @return integer
63+ */
64+ public function getVerbosityLevel()
65+ {
66+ if ($this->Arguments->get('v')) return 1;
67+ elseif ($this->Arguments->get('vv')) return 2;
68+ elseif ($this->Arguments->get('vvv')) return 3;
69+ else return 0; /* (This is normal level. If you want more silent levels you can define negative levels) */
70+ }
71+
72+ public function wasHelpRequested()
73+ {
74+ return (bool)($this->Arguments->get('h') || $this->Arguments->get('help'));
75+ }
76+
77+ }
78+
79+}
80\ No newline at end of file
81
82=== modified file 'lib/GnupoParser/gnupo.cli.php'
83--- lib/GnupoParser/gnupo.cli.php 2009-09-09 20:25:56 +0000
84+++ lib/GnupoParser/gnupo.cli.php 2009-09-10 16:43:59 +0000
85@@ -3,36 +3,55 @@
86 namespace Gnupo
87 {
88
89- use \Gnupo\Cli\Printer as Printer;
90- use \Autoloading\Loader as Loader;
91- use \Gnupo\Cli\ArgumentParser as ArgumentParser;
92- use \Gnupo\Cli\ParameterHolder as ParameterHolder;
93- use \Gnupo\Cli\OutputController as OutputController;
94- use \Gnupo\Event\Dispatcher as Dispatcher;
95- use \Gnupo\Event\Event as Event;
96+ use \Gnupo\Cli\Printer as Printer;
97+ use \Autoloading\Loader as Loader;
98+ use \Gnupo\Cli\ArgumentParser as ArgumentParser;
99+ use \Gnupo\Cli\ParameterHolder as ParameterHolder;
100+ use \Gnupo\Cli\OutputController as OutputController;
101+ use \Gnupo\Event\Dispatcher as Dispatcher;
102+ use \Gnupo\Event\Event as Event;
103+ use \Gnupo\Parser\ArgumentInterpreter as ArgumentInterpreter;
104+
105+
106+ #------- Main execution -------
107+ {
108+ $Main = Main::getInstance();
109+ $Main->execute();
110+ }
111+ #------------------------------
112+
113
114 /**
115 * Main script to be runned from CLI environment
116- * @todo Set, parse and interpret a Help Parameter
117+ * This acts as a context class, is accessible from anywere through getInstance() method
118 */
119 class Main
120 {
121
122 /**
123+ * This is an event dispatcher. for communicating things among all objects in runtime
124 * @var Dispatcher
125 */
126 private $EventDispatcher;
127
128 /**
129+ * This object controls all the output of the gnupo parser
130 * @var OutputController
131 */
132 private $OutputController;
133
134 /**
135+ * This object holds the arguments received
136 * @var ParameterHolder
137 */
138 private $Arguments;
139
140+ /**
141+ * This object provides a convenient OO interface to the execution configuration desired based on arguments holded by the ParameterHolder. This should be used ever instead of directly using the parameter holder to maintain clarity in code.
142+ * @var ArgumentInterpreter
143+ */
144+ private $ArgumentInterpreter;
145+
146 #---- Singleton Pattern
147
148 private static $instance;
149@@ -73,29 +92,40 @@
150 $ParametersArray = ArgumentParser::parse($_SERVER['argv']);
151 $this->ParameterHolder = new ParameterHolder($ParametersArray);
152
153+ # Gnupo Argument Interpreter
154+ $this->ArgumentInterpreter = new ArgumentInterpreter($this->ParameterHolder);
155+
156 # Define the level of output according relevancy
157- $maxLoggingRelevancyLevel = OutputController::R_INFO;
158- if ($this->ParameterHolder->get('vv'))
159- {
160- $maxLoggingRelevancyLevel = OutputController::R_NOTICE;
161- }
162- if ($this->ParameterHolder->get('vvv'))
163- {
164- $maxLoggingRelevancyLevel = OutputController::R_DEBUG;
165- }
166-
167+ switch ( $this->ArgumentInterpreter->getVerbosityLevel() )
168+ {
169+ case 0: $maxLoggingRelevancyLevel = OutputController::R_WARNING; break;
170+ case 1: $maxLoggingRelevancyLevel = OutputController::R_INFO; break;
171+ case 2: $maxLoggingRelevancyLevel = OutputController::R_NOTICE; break;
172+ case 3: $maxLoggingRelevancyLevel = OutputController::R_DEBUG; break;
173+ }
174+
175 # Output controlling
176 $this->OutputController = OutputController::getInstance($this->EventDispatcher);
177 $this->OutputController->setMaxRelevancyLevel($maxLoggingRelevancyLevel);
178
179- # Outputs all received arguments if debugging
180+ # Outputs all received arguments if you are debugging
181 new Event('log',$this,array('type'=>'Debugging','relevancy'=>OutputController::R_DEBUG,'message'=>var_export($this->ParameterHolder->getAll(),1)));
182
183 }
184
185+ /**
186+ * Executes the main routine
187+ */
188 public function execute()
189 {
190
191+ if ( $this->ArgumentInterpreter->wasHelpRequested() )
192+ {
193+ $this->showHelp();
194+ exit;
195+ }
196+
197+
198 /*****************************************************************************************
199 * All this code is only for Output testing purposes and must be removed prior releasing
200 */
201@@ -145,9 +175,20 @@
202 }}}}#************************************************************************************
203 }
204
205+ /**
206+ * Executes the help section in a CLI environment
207+ */
208+ private function showHelp()
209+ {
210+ $text = "GnuPO - Gnu Php Orm";
211+ $E = new Event('log',$this,array('type'=>'Information','relevancy'=>OutputController::R_ERROR,'message'=>$text));
212+ $this->EventDispatcher->notify($E);
213+
214+ $helpText = "-h --help | Shows this help";
215+ $E = new Event('log',$this,array('type'=>'Usage','relevancy'=>OutputController::R_ERROR,'message'=>$helpText));
216+ $this->EventDispatcher->notify($E);
217+ }
218+
219 }
220
221- $Main = Main::getInstance();
222- $Main->execute();
223-
224 }
225\ No newline at end of file

Subscribers

People subscribed via source and target branches

to all changes: