Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.

Commit 9bb4bb5

Browse files
committed
Added abstract apisearch command
1 parent 629f95c commit 9bb4bb5

File tree

6 files changed

+280
-9
lines changed

6 files changed

+280
-9
lines changed

.php_cs.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"php":"7.1.17-1+ubuntu16.04.1+deb.sury.org+1","version":"2.11.1:v2.11.1#ad94441c17b8ef096e517acccdbf3238af8a2da8","rules":{"blank_line_after_namespace":true,"braces":{"allow_single_line_closure":true},"class_definition":{"singleLine":true},"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_constants":true,"lowercase_keywords":true,"method_argument_space":true,"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":true,"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true,"binary_operator_spaces":true,"blank_line_after_opening_tag":true,"blank_line_before_statement":{"statements":["return"]},"cast_spaces":true,"class_attributes_separation":{"elements":["method"]},"concat_space":{"spacing":"none"},"declare_equal_normalize":true,"function_typehint_space":true,"include":true,"increment_style":true,"lowercase_cast":true,"magic_constant_casing":true,"native_function_casing":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_blank_lines_after_phpdoc":true,"no_empty_comment":true,"no_empty_phpdoc":true,"no_empty_statement":true,"no_extra_blank_lines":{"tokens":["curly_brace_block","extra","parenthesis_brace_block","square_brace_block","throw","use"]},"no_leading_import_slash":true,"no_leading_namespace_whitespace":true,"no_mixed_echo_print":{"use":"echo"},"no_multiline_whitespace_around_double_arrow":true,"no_short_bool_cast":true,"no_singleline_whitespace_before_semicolons":true,"no_spaces_around_offset":true,"no_trailing_comma_in_list_call":true,"no_trailing_comma_in_singleline_array":true,"no_unneeded_control_parentheses":true,"no_unneeded_curly_braces":true,"no_unneeded_final_method":true,"no_unused_imports":true,"no_whitespace_before_comma_in_array":true,"no_whitespace_in_blank_line":true,"normalize_index_brace":true,"object_operator_without_whitespace":true,"php_unit_fqcn_annotation":true,"phpdoc_align":{"tags":["method","param","property","return","throws","type","var"]},"phpdoc_annotation_without_dot":true,"phpdoc_indent":true,"phpdoc_inline_tag":true,"phpdoc_no_access":true,"phpdoc_no_alias_tag":true,"phpdoc_no_empty_return":true,"phpdoc_no_package":true,"phpdoc_no_useless_inheritdoc":true,"phpdoc_return_self_reference":true,"phpdoc_scalar":true,"phpdoc_separation":true,"phpdoc_single_line_var_spacing":true,"phpdoc_summary":true,"phpdoc_to_comment":true,"phpdoc_trim":true,"phpdoc_types":true,"phpdoc_var_without_name":true,"protected_to_private":true,"return_type_declaration":true,"semicolon_after_instruction":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_line_comment_style":{"comment_types":["hash"]},"single_quote":true,"space_after_semicolon":{"remove_in_empty_for_expressions":true},"standardize_increment":true,"standardize_not_equals":true,"ternary_operator_spaces":true,"trailing_comma_in_multiline_array":true,"trim_array_spaces":true,"unary_operator_spaces":true,"whitespace_after_comma_in_array":true,"yoda_style":true},"hashes":{"Twig\/MethodAccessorExtension.php":1937766330,"Twig\/AggregationTranslationExtension.php":1168064907,"Twig\/UrlBuilderExtension.php":1238295593,"DependencyInjection\/CompilerPass\/ExporterCompilerPass.php":3859400892,"DependencyInjection\/CompilerPass\/ReadTransformerCompilerPass.php":407060565,"DependencyInjection\/CompilerPass\/WriteTransformerCompilerPass.php":3787221228,"DependencyInjection\/CompilerPass\/RepositoryCompilerPass.php":3656156359,"DependencyInjection\/CompilerPass\/TagCompilerPass.php":1857409043,"DependencyInjection\/ApisearchExtension.php":2861843414,"DependencyInjection\/ApisearchConfiguration.php":2562035961,"ApisearchBundle.php":452340142,"Translator\/AggregationTranslator.php":2950948888,"Command\/ExportIndexCommand.php":3413220554,"Command\/ImportIndexCommand.php":327946495,"Command\/ResetIndexCommand.php":38662220,"Tests\/Functional\/DependencyInjection\/ApisearchConfigurationTest.php":379272062}}
1+
{"php":"7.1.12-1+ubuntu16.04.1+deb.sury.org+1","version":"2.11.1:v2.11.1#ad94441c17b8ef096e517acccdbf3238af8a2da8","rules":{"blank_line_after_namespace":true,"braces":{"allow_single_line_closure":true},"class_definition":{"singleLine":true},"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_constants":true,"lowercase_keywords":true,"method_argument_space":true,"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":true,"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true,"binary_operator_spaces":true,"blank_line_after_opening_tag":true,"blank_line_before_statement":{"statements":["return"]},"cast_spaces":true,"class_attributes_separation":{"elements":["method"]},"concat_space":{"spacing":"none"},"declare_equal_normalize":true,"function_typehint_space":true,"include":true,"increment_style":true,"lowercase_cast":true,"magic_constant_casing":true,"native_function_casing":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_blank_lines_after_phpdoc":true,"no_empty_comment":true,"no_empty_phpdoc":true,"no_empty_statement":true,"no_extra_blank_lines":{"tokens":["curly_brace_block","extra","parenthesis_brace_block","square_brace_block","throw","use"]},"no_leading_import_slash":true,"no_leading_namespace_whitespace":true,"no_mixed_echo_print":{"use":"echo"},"no_multiline_whitespace_around_double_arrow":true,"no_short_bool_cast":true,"no_singleline_whitespace_before_semicolons":true,"no_spaces_around_offset":true,"no_trailing_comma_in_list_call":true,"no_trailing_comma_in_singleline_array":true,"no_unneeded_control_parentheses":true,"no_unneeded_curly_braces":true,"no_unneeded_final_method":true,"no_unused_imports":true,"no_whitespace_before_comma_in_array":true,"no_whitespace_in_blank_line":true,"normalize_index_brace":true,"object_operator_without_whitespace":true,"php_unit_fqcn_annotation":true,"phpdoc_align":{"tags":["method","param","property","return","throws","type","var"]},"phpdoc_annotation_without_dot":true,"phpdoc_indent":true,"phpdoc_inline_tag":true,"phpdoc_no_access":true,"phpdoc_no_alias_tag":true,"phpdoc_no_empty_return":true,"phpdoc_no_package":true,"phpdoc_no_useless_inheritdoc":true,"phpdoc_return_self_reference":true,"phpdoc_scalar":true,"phpdoc_separation":true,"phpdoc_single_line_var_spacing":true,"phpdoc_summary":true,"phpdoc_to_comment":true,"phpdoc_trim":true,"phpdoc_types":true,"phpdoc_var_without_name":true,"protected_to_private":true,"return_type_declaration":true,"semicolon_after_instruction":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_line_comment_style":{"comment_types":["hash"]},"single_quote":true,"space_after_semicolon":{"remove_in_empty_for_expressions":true},"standardize_increment":true,"standardize_not_equals":true,"ternary_operator_spaces":true,"trailing_comma_in_multiline_array":true,"trim_array_spaces":true,"unary_operator_spaces":true,"whitespace_after_comma_in_array":true,"yoda_style":true},"hashes":{"Tests\/Functional\/DependencyInjection\/ApisearchConfigurationTest.php":379272062,"Translator\/AggregationTranslator.php":2950948888,"ApisearchBundle.php":452340142,"Twig\/MethodAccessorExtension.php":1937766330,"Twig\/AggregationTranslationExtension.php":1168064907,"Twig\/UrlBuilderExtension.php":1238295593,"Command\/ImportIndexCommand.php":3591097319,"Command\/ResetIndexCommand.php":3410842211,"Command\/ApisearchCommand.php":1883682797,"Command\/ExportIndexCommand.php":142010414,"DependencyInjection\/CompilerPass\/TagCompilerPass.php":1857409043,"DependencyInjection\/CompilerPass\/WriteTransformerCompilerPass.php":3787221228,"DependencyInjection\/CompilerPass\/ExporterCompilerPass.php":3859400892,"DependencyInjection\/CompilerPass\/RepositoryCompilerPass.php":3656156359,"DependencyInjection\/CompilerPass\/ReadTransformerCompilerPass.php":407060565,"DependencyInjection\/ApisearchConfiguration.php":2562035961,"DependencyInjection\/ApisearchExtension.php":2861843414}}

Command/ApisearchCommand.php

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Search PHP Bundle.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author Marc Morera <yuhu@mmoreram.com>
12+
* @author PuntMig Technologies
13+
*/
14+
15+
declare(strict_types=1);
16+
17+
namespace Apisearch\Command;
18+
19+
use Symfony\Component\Console\Command\Command;
20+
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
21+
use Symfony\Component\Console\Output\OutputInterface;
22+
use Symfony\Component\Stopwatch\Stopwatch;
23+
use Symfony\Component\Stopwatch\StopwatchEvent;
24+
25+
/**
26+
* Class ApisearchCommand.
27+
*/
28+
abstract class ApisearchCommand extends Command
29+
{
30+
/**
31+
* @var Stopwatch
32+
*
33+
* Stopwatch instance
34+
*/
35+
private $stopwatch;
36+
37+
/**
38+
* Start command.
39+
*
40+
* @param OutputInterface $output Output
41+
* @param bool $longCommand Show long time message
42+
*
43+
* @return $this Self object
44+
*/
45+
protected function startCommand(
46+
OutputInterface $output,
47+
$longCommand = false
48+
) {
49+
$this->configureFormatter($output);
50+
$this->stopwatch = new Stopwatch();
51+
$this->startStopWatch('command');
52+
$output->writeln('');
53+
$this
54+
->printSystemMessage(
55+
$output,
56+
$this->getProjectHeader(),
57+
'Command started at '.date('r')
58+
);
59+
if ($longCommand) {
60+
$this
61+
->printMessage(
62+
$output,
63+
$this->getProjectHeader(),
64+
'This process may take a few minutes. Please, be patient'
65+
);
66+
}
67+
$output->writeln('');
68+
69+
return $this;
70+
}
71+
72+
/**
73+
* Configure formatter with Elcodi specific style.
74+
*
75+
* @param OutputInterface $output Output
76+
*
77+
* @return $this Self object
78+
*/
79+
protected function configureFormatter(OutputInterface $output)
80+
{
81+
$formatter = $output->getFormatter();
82+
$formatter->setStyle('system', new OutputFormatterStyle('green'));
83+
$formatter->setStyle('line', new OutputFormatterStyle('yellow'));
84+
$formatter->setStyle('failLine', new OutputFormatterStyle('red'));
85+
$formatter->setStyle('info', new OutputFormatterStyle('blue'));
86+
$formatter->setStyle('body', new OutputFormatterStyle('white'));
87+
88+
return $this;
89+
}
90+
91+
/**
92+
* Start stopwatch.
93+
*
94+
* @param string $eventName Event name
95+
*
96+
* @return StopwatchEvent Event
97+
*/
98+
protected function startStopWatch($eventName)
99+
{
100+
return $this
101+
->stopwatch
102+
->start($eventName);
103+
}
104+
105+
/**
106+
* Print message.
107+
*
108+
* @param OutputInterface $output Output
109+
* @param string $header Message header
110+
* @param string $body Message body
111+
*
112+
* @return ApisearchCommand
113+
*/
114+
protected function printSystemMessage(
115+
OutputInterface $output,
116+
$header,
117+
$body
118+
): ApisearchCommand {
119+
$this->printStructureMessage(
120+
$output,
121+
$header,
122+
$body,
123+
'system'
124+
);
125+
126+
return $this;
127+
}
128+
129+
/**
130+
* Print message.
131+
*
132+
* @param OutputInterface $output Output
133+
* @param string $header Message header
134+
* @param string $body Message body
135+
*
136+
* @return ApisearchCommand
137+
*/
138+
protected function printMessage(
139+
OutputInterface $output,
140+
$header,
141+
$body
142+
) {
143+
$this->printStructureMessage(
144+
$output,
145+
$header,
146+
$body,
147+
'line'
148+
);
149+
150+
return $this;
151+
}
152+
153+
/**
154+
* Print message.
155+
*
156+
* @param OutputInterface $output Output
157+
* @param string $header Message header
158+
* @param string $body Message body
159+
*
160+
* @return ApisearchCommand
161+
*/
162+
protected function printInfoMessage(
163+
OutputInterface $output,
164+
$header,
165+
$body
166+
): ApisearchCommand {
167+
$this->printStructureMessage(
168+
$output,
169+
$header,
170+
$body,
171+
'info'
172+
);
173+
174+
return $this;
175+
}
176+
177+
/**
178+
* Print message.
179+
*
180+
* @param OutputInterface $output Output
181+
* @param string $header Message header
182+
* @param string $body Message body
183+
*
184+
* @return ApisearchCommand
185+
*/
186+
protected function printMessageFail(
187+
OutputInterface $output,
188+
$header,
189+
$body
190+
): ApisearchCommand {
191+
$this->printStructureMessage(
192+
$output,
193+
$header,
194+
$body,
195+
'failLine'
196+
);
197+
198+
return $this;
199+
}
200+
201+
/**
202+
* Print message.
203+
*
204+
* @param OutputInterface $output
205+
* @param string $header
206+
* @param string $body
207+
* @param string $type
208+
*/
209+
private function printStructureMessage(
210+
OutputInterface $output,
211+
$header,
212+
$body,
213+
$type
214+
) {
215+
$message = sprintf(
216+
"<$type>%s</$type> <body>%s</body>",
217+
'['.$header.']',
218+
$body
219+
);
220+
$output->writeln($message);
221+
}
222+
223+
/**
224+
* Finish command.
225+
*
226+
* @param OutputInterface $output Output
227+
*
228+
* @return $this Self object
229+
*/
230+
protected function finishCommand(OutputInterface $output)
231+
{
232+
$output->writeln('');
233+
$event = $this->stopStopWatch('command');
234+
$this
235+
->printSystemMessage(
236+
$output,
237+
$this->getProjectHeader(),
238+
'Command finished in '.$event->getDuration().' milliseconds'
239+
)
240+
->printSystemMessage(
241+
$output,
242+
$this->getProjectHeader(),
243+
'Max memory used: '.$event->getMemory().' bytes'
244+
);
245+
$output->writeln('');
246+
247+
return $this;
248+
}
249+
250+
/**
251+
* Stop stopwatch.
252+
*
253+
* @param string $eventName Event name
254+
*
255+
* @return StopwatchEvent Event
256+
*/
257+
protected function stopStopWatch($eventName)
258+
{
259+
return $this
260+
->stopwatch
261+
->stop($eventName);
262+
}
263+
264+
/**
265+
* Get project header.
266+
*
267+
* @return string Get project header
268+
*/
269+
protected function getProjectHeader()
270+
{
271+
return 'Apisearch';
272+
}
273+
}

Command/ExportIndexCommand.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@
2020
use Apisearch\Model\Item;
2121
use Apisearch\Query\Query;
2222
use Apisearch\Repository\RepositoryBucket;
23-
use Symfony\Component\Console\Command\Command;
2423
use Symfony\Component\Console\Input\InputArgument;
2524
use Symfony\Component\Console\Input\InputInterface;
2625
use Symfony\Component\Console\Output\OutputInterface;
2726

2827
/**
2928
* ExportIndexCommand.
3029
*/
31-
class ExportIndexCommand extends Command
30+
class ExportIndexCommand extends ApisearchCommand
3231
{
3332
/**
3433
* @var RepositoryBucket

Command/ImportIndexCommand.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@
1616

1717
namespace Apisearch\Command;
1818

19-
use Apisearch\Model\Coordinate;
2019
use Apisearch\Model\Item;
2120
use Apisearch\Repository\RepositoryBucket;
22-
use Symfony\Component\Console\Command\Command;
2321
use Symfony\Component\Console\Input\InputArgument;
2422
use Symfony\Component\Console\Input\InputInterface;
2523
use Symfony\Component\Console\Output\OutputInterface;
2624

2725
/**
2826
* ImportIndexCommand.
2927
*/
30-
class ImportIndexCommand extends Command
28+
class ImportIndexCommand extends ApisearchCommand
3129
{
3230
/**
3331
* @var RepositoryBucket

Command/ResetIndexCommand.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@
1717
namespace Apisearch\Command;
1818

1919
use Apisearch\Repository\RepositoryBucket;
20-
use Symfony\Component\Console\Command\Command;
2120
use Symfony\Component\Console\Input\InputArgument;
2221
use Symfony\Component\Console\Input\InputInterface;
2322
use Symfony\Component\Console\Output\OutputInterface;
2423

2524
/**
2625
* File header placeholder.
2726
*/
28-
class ResetIndexCommand extends Command
27+
class ResetIndexCommand extends ApisearchCommand
2928
{
3029
/**
3130
* @var RepositoryBucket

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616
"php": ">=7.1",
1717
"symfony/framework-bundle": "^2.8|^3.0|^4.0",
1818
"symfony/http-kernel": "^2.8|^3.0|^4.0",
19+
"symfony/stopwatch": "^2.8|^3.0|^4.0",
20+
"symfony/console": "^2.8|^3.0|^4.0",
1921
"apisearch-io/php-client": "^0.1.6"
2022
},
2123
"suggested": {
2224
"twig/twig": "In case you use Twig engine",
2325
"symfony/translator": "^2.8|^3.0|^4.0 in case you use Macros translations"
2426
},
2527
"require-dev": {
26-
"phpunit/phpunit": "^5.6.4",
28+
"phpunit/phpunit": "^5.6.5",
2729
"mmoreram/php-formatter": "1.3.1",
2830
"friendsofphp/php-cs-fixer": "^2.5.0",
2931
"mmoreram/symfony-bundle-dependencies": "^2.1",

0 commit comments

Comments
 (0)