1313 * @author Marc Morera <yuhu@mmoreram.com>
1414 */
1515
16- namespace PHPFormatter \Command ;
16+ namespace Mmoreram \ PHPFormatter \Command ;
1717
18- use PHPFormatter \Finder \FileFinder ;
19- use PHPFormatter \UseSorter ;
20- use Symfony \Component \Console \Command \Command as BaseCommand ;
18+ use Exception ;
19+ use Symfony \Component \Console \Command \Command ;
2120use Symfony \Component \Console \Input \InputArgument ;
2221use Symfony \Component \Console \Input \InputInterface ;
2322use Symfony \Component \Console \Input \InputOption ;
2423use Symfony \Component \Console \Output \OutputInterface ;
2524use Symfony \Component \Filesystem \Filesystem ;
2625
26+ use Mmoreram \PHPFormatter \Finder \ConfigFinder ;
27+ use Mmoreram \PHPFormatter \Finder \FileFinder ;
28+ use Mmoreram \PHPFormatter \Loader \ConfigLoader ;
29+ use Mmoreram \PHPFormatter \Sorter \UseSorter ;
30+
2731/**
2832 * Class UseSortCommand
2933 */
30- class UseSortCommand extends BaseCommand
34+ class UseSortCommand extends Command
3135{
36+ /**
37+ * @var string
38+ *
39+ * Command name
40+ */
41+ const COMMAND_NAME = 'use-sort ' ;
42+
3243 /**
3344 * configure
3445 */
@@ -52,22 +63,26 @@ protected function configure()
5263 'sort-type ' ,
5364 null ,
5465 InputOption::VALUE_OPTIONAL ,
55- "Sort type " ,
56- UseSorter::SORT_TYPE_ALPHABETIC
66+ "Sort type "
5767 )
5868 ->addOption (
5969 'sort-direction ' ,
6070 null ,
6171 InputOption::VALUE_OPTIONAL ,
62- "Sort direction " ,
63- UseSorter::SORT_DIRECTION_ASC
72+ "Sort direction "
6473 )
6574 ->addOption (
6675 'group-type ' ,
6776 null ,
6877 InputOption::VALUE_OPTIONAL ,
69- "Type of grouping " ,
70- UseSorter::GROUP_TYPE_EACH
78+ "Type of grouping "
79+ )
80+ ->addOption (
81+ '--config ' ,
82+ '-c ' ,
83+ InputOption::VALUE_OPTIONAL ,
84+ "Config file directory " ,
85+ getcwd ()
7186 )
7287 ->addOption (
7388 'dry-run ' ,
@@ -78,44 +93,123 @@ protected function configure()
7893 }
7994
8095 /**
96+ * Execute command
97+ *
8198 * @param InputInterface $input Input
8299 * @param OutputInterface $output Output
83100 *
84101 * @return int|null|void
102+ *
103+ * @throws Exception
85104 */
86105 protected function execute (InputInterface $ input , OutputInterface $ output )
87106 {
107+ $ verbose = $ output ->getVerbosity ();
88108 $ path = $ input ->getArgument ('path ' );
89- $ groups = $ input ->getOption ('group ' );
90- $ sortType = $ input ->getOption ('sort-type ' );
91- $ sortDirection = $ input ->getOption ('sort-direction ' );
92- $ groupType = $ input ->getOption ('group-type ' );
93109 $ dryRun = $ input ->getOption ('dry-run ' );
110+ $ fileFinder = new FileFinder ;
111+ $ configLoader = new ConfigLoader ;
112+ $ configFinder = new ConfigFinder ;
113+
114+ /**
115+ * This section is just for finding the right values to work with in
116+ * this execution.
117+ *
118+ * $options array will have, after this block, all these values
119+ */
120+ $ configPath = rtrim ($ input ->getOption ('config ' ), DIRECTORY_SEPARATOR );
121+
122+ $ options = $ configLoader ->loadConfigValues (
123+ self ::COMMAND_NAME ,
124+ $ configFinder ->findConfigFile ($ configPath ),
125+ array (
126+ 'group ' => $ input ->getOption ('group ' ),
127+ 'group-type ' => $ input ->getOption ('group-type ' ),
128+ 'sort-type ' => $ input ->getOption ('sort-type ' ),
129+ 'sort-direction ' => $ input ->getOption ('sort-direction ' )
130+ ),
131+ array (
132+ 'group ' => array ('_main ' ),
133+ 'group-type ' => UseSorter::GROUP_TYPE_EACH ,
134+ 'sort-type ' => UseSorter::SORT_TYPE_ALPHABETIC ,
135+ 'sort-direction ' => UseSorter::SORT_DIRECTION_ASC
136+ )
137+ );
138+
139+ /**
140+ * Building the real directory or file to work in
141+ */
142+ $ filesystem = new Filesystem ();
143+ if (!$ filesystem ->isAbsolutePath ($ path )) {
144+ $ path = getcwd () . DIRECTORY_SEPARATOR . $ path ;
145+ }
94146
95- if (null !== $ path ) {
147+ if (! is_file ( $ path ) && ! is_dir ( $ path) ) {
96148
97- $ filesystem = new Filesystem ();
98- if (!$ filesystem ->isAbsolutePath ($ path )) {
99- $ path = getcwd () . DIRECTORY_SEPARATOR . $ path ;
100- }
149+ throw new Exception ('Directory or file " ' . $ path . '" does not exist ' );
150+ }
151+
152+ /**
153+ * Dry-run message
154+ */
155+ if ($ dryRun && $ verbose >= OutputInterface::VERBOSITY_VERBOSE ) {
156+
157+ $ output ->writeln ('# This process has been executed in mode dry-run ' );
158+ }
159+
160+ if ($ verbose >= OutputInterface::VERBOSITY_VERBOSE ) {
161+
162+ $ output ->writeln ('# Executing process in ' . $ path );
101163 }
102164
165+ /**
166+ * Creates the new UseSorter file, given config values
167+ */
103168 $ useSorter = new UseSorter ();
104169 $ useSorter
105- ->setGroups ($ groups )
106- ->setSortType ($ sortType )
107- ->setSortDirection ($ sortDirection )
108- ->setGroupType ($ groupType );
170+ ->setGroups ($ options ['group ' ])
171+ ->setGroupType ($ options ['group-type ' ])
172+ ->setSortType ($ options ['sort-type ' ])
173+ ->setSortDirection ($ options ['sort-direction ' ]);
174+
175+ $ files = $ fileFinder ->findPHPFilesByPath ($ path );
176+
177+ /**
178+ * If verbose level is higher or equal than -vv, we print the config
179+ * file data, if is not empty.
180+ */
181+ if ($ verbose >= OutputInterface::VERBOSITY_VERBOSE ) {
182+
183+ $ output ->writeln ('# Executing process with this configuration ' );
184+ if (!empty ($ options ['group ' ])) {
109185
110- $ finder = new FileFinder ();
111- $ files = $ finder ->findPHPFilesByPath ($ path );
186+ foreach ($ options ['group ' ] as $ group ) {
112187
113- if ($ dryRun ) {
188+ $ output ->writeln ('# --group=" ' . $ group . '" ' );
189+ }
190+ }
191+
192+ if (!empty ($ options ['group-type ' ])) {
193+
194+ $ output ->writeln ('# --group-type=" ' . $ options ['group-type ' ] . '" ' );
195+ }
114196
115- $ output ->writeln ('This process is Dry-run ' );
116- $ output ->writeln ('' );
197+ if (!empty ($ options ['sort-type ' ])) {
198+
199+ $ output ->writeln ('# --sort-type=" ' . $ options ['sort-type ' ] . '" ' );
200+ }
201+
202+ if (!empty ($ options ['sort-direction ' ])) {
203+
204+ $ output ->writeln ('# --sort-direction=" ' . $ options ['sort-direction ' ] . '" ' );
205+ }
117206 }
118207
208+ $ output ->writeln ('# ' );
209+
210+ /**
211+ * Each found php file is processed
212+ */
119213 foreach ($ files as $ file ) {
120214
121215 $ data = file_get_contents ($ file );
@@ -126,7 +220,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
126220 continue ;
127221 }
128222
129- $ output ->writeln ($ file );
223+ if ($ verbose >= OutputInterface::VERBOSITY_NORMAL ) {
224+
225+ $ output ->writeln ('# ' . $ file );
226+ }
130227
131228 if (!$ dryRun ) {
132229
0 commit comments