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

Commit db8b1c7

Browse files
committed
Added index-delete index commands
1 parent b893f22 commit db8b1c7

File tree

4 files changed

+479
-1
lines changed

4 files changed

+479
-1
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":3492158054,"DependencyInjection\/CompilerPass\/TagCompilerPass.php":1857409043,"DependencyInjection\/ApisearchExtension.php":2861843414,"DependencyInjection\/ApisearchConfiguration.php":2562035961,"ApisearchBundle.php":452340142,"Translator\/AggregationTranslator.php":2950948888,"Command\/ExportIndexCommand.php":3751299339,"Command\/ImportIndexCommand.php":1722481554,"Command\/PrintTokensCommand.php":3931081773,"Command\/ApisearchCommand.php":1883682797,"Command\/ApisearchFormattedCommand.php":2617090809,"Command\/WithRepositoryBucketCommand.php":835091977,"Command\/WithAppRepositoryBucketCommand.php":3329637861,"Command\/ResetIndexCommand.php":4289827141,"Tests\/Functional\/DependencyInjection\/ApisearchConfigurationTest.php":379272062,"Command\/AddTokenCommand.php":4146793222,"Command\/DeleteTokenCommand.php":462070402}}
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":3492158054,"DependencyInjection\/CompilerPass\/TagCompilerPass.php":1857409043,"DependencyInjection\/ApisearchExtension.php":2861843414,"DependencyInjection\/ApisearchConfiguration.php":2562035961,"ApisearchBundle.php":452340142,"Translator\/AggregationTranslator.php":2950948888,"Command\/ExportIndexCommand.php":3751299339,"Command\/ImportIndexCommand.php":1722481554,"Command\/PrintTokensCommand.php":3931081773,"Command\/ApisearchCommand.php":1883682797,"Command\/ApisearchFormattedCommand.php":2617090809,"Command\/WithRepositoryBucketCommand.php":835091977,"Command\/WithAppRepositoryBucketCommand.php":3329637861,"Command\/ResetIndexCommand.php":4289827141,"Tests\/Functional\/DependencyInjection\/ApisearchConfigurationTest.php":379272062,"Command\/AddTokenCommand.php":89158944,"Command\/DeleteTokenCommand.php":2813018826,"Command\/CreateIndexCommand.php":153649105,"Command\/DeleteIndexCommand.php":1076402678}}

Command/CreateIndexCommand.php

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
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 Apisearch\Config\ImmutableConfig;
20+
use Apisearch\Event\EventRepositoryBucket;
21+
use Apisearch\Exception\ResourceNotAvailableException;
22+
use Apisearch\Log\LogRepositoryBucket;
23+
use Apisearch\Repository\RepositoryBucket;
24+
use Symfony\Component\Console\Input\InputArgument;
25+
use Symfony\Component\Console\Input\InputInterface;
26+
use Symfony\Component\Console\Input\InputOption;
27+
use Symfony\Component\Console\Output\OutputInterface;
28+
29+
/**
30+
* Class CreateIndexCommand.
31+
*/
32+
class CreateIndexCommand extends WithRepositoryBucketCommand
33+
{
34+
/**
35+
* @var EventRepositoryBucket
36+
*
37+
* Event repository bucket
38+
*/
39+
private $eventRepositoryBucket;
40+
41+
/**
42+
* @var LogRepositoryBucket
43+
*
44+
* Log repository bucket
45+
*/
46+
private $logRepositoryBucket;
47+
48+
/**
49+
* CreateIndexCommand constructor.
50+
*
51+
* @param RepositoryBucket $repositoryBucket
52+
* @param EventRepositoryBucket $eventRepositoryBucket
53+
* @param LogRepositoryBucket $logRepositoryBucket
54+
*/
55+
public function __construct(
56+
RepositoryBucket $repositoryBucket,
57+
EventRepositoryBucket $eventRepositoryBucket,
58+
LogRepositoryBucket $logRepositoryBucket
59+
) {
60+
parent::__construct($repositoryBucket);
61+
62+
$this->eventRepositoryBucket = $eventRepositoryBucket;
63+
$this->logRepositoryBucket = $logRepositoryBucket;
64+
}
65+
66+
/**
67+
* Configures the current command.
68+
*/
69+
protected function configure()
70+
{
71+
$this
72+
->setName('apisearch:create-index')
73+
->setDescription('Create an index')
74+
->addArgument(
75+
'repository',
76+
InputArgument::REQUIRED,
77+
'Repository'
78+
)
79+
->addArgument(
80+
'index',
81+
InputArgument::REQUIRED,
82+
'Index'
83+
)
84+
->addOption(
85+
'language',
86+
null,
87+
InputOption::VALUE_OPTIONAL,
88+
'Index language',
89+
null
90+
)
91+
->addOption(
92+
'no-store-searchable-metadata',
93+
null,
94+
InputOption::VALUE_NONE,
95+
'Store searchable metadata'
96+
)
97+
->addOption(
98+
'with-events',
99+
null,
100+
InputOption::VALUE_NONE,
101+
'Create events as well'
102+
)
103+
->addOption(
104+
'with-logs',
105+
null,
106+
InputOption::VALUE_NONE,
107+
'Create logs as well'
108+
)
109+
->addOption(
110+
'synonym',
111+
null,
112+
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
113+
'Synonym'
114+
);
115+
}
116+
117+
/**
118+
* Dispatch domain event.
119+
*
120+
* @return string
121+
*/
122+
protected function getHeader(): string
123+
{
124+
return 'Create index';
125+
}
126+
127+
/**
128+
* Dispatch domain event.
129+
*
130+
* @param InputInterface $input
131+
* @param OutputInterface $output
132+
*
133+
* @return mixed|null
134+
*/
135+
protected function runCommand(InputInterface $input, OutputInterface $output)
136+
{
137+
$repository = $input->getArgument('repository');
138+
$index = $input->getArgument('index');
139+
140+
try {
141+
$this
142+
->repositoryBucket
143+
->findRepository($repository, $index)
144+
->createIndex(
145+
ImmutableConfig::createFromArray([
146+
'language' => $input->getOption('language'),
147+
'store_searchable_metadata' => !$input->getOption('no-store-searchable-metadata'),
148+
'synonyms' => array_map(function (string $synonym) {
149+
return ['words' => array_map('trim', explode(',', $synonym))];
150+
}, $input->getOption('synonym')),
151+
])
152+
);
153+
} catch (ResourceNotAvailableException $exception) {
154+
$this->printInfoMessage(
155+
$output,
156+
$this->getHeader(),
157+
'Index is already created. Skipping.'
158+
);
159+
}
160+
161+
if ($input->getOption('with-events')) {
162+
$this->createEvents(
163+
$repository,
164+
$index,
165+
$output
166+
);
167+
}
168+
169+
if ($input->getOption('with-logs')) {
170+
$this->createLogs(
171+
$repository,
172+
$index,
173+
$output
174+
);
175+
}
176+
}
177+
178+
/**
179+
* Get success message.
180+
*
181+
* @param InputInterface $input
182+
* @param mixed $result
183+
*
184+
* @return string
185+
*/
186+
protected function getSuccessMessage(
187+
InputInterface $input,
188+
$result
189+
): string {
190+
return 'Indices created properly';
191+
}
192+
193+
/**
194+
* Create events index.
195+
*
196+
* @param string $repositoryName
197+
* @param string $index
198+
* @param OutputInterface $output
199+
*/
200+
private function createEvents(
201+
string $repositoryName,
202+
string $index,
203+
OutputInterface $output
204+
) {
205+
try {
206+
$this
207+
->eventRepositoryBucket
208+
->findRepository($repositoryName, $index)
209+
->createIndex();
210+
} catch (ResourceNotAvailableException $exception) {
211+
$this->printInfoMessage(
212+
$output,
213+
$this->getHeader(),
214+
'Events index is already created. Skipping.'
215+
);
216+
}
217+
}
218+
219+
/**
220+
* Create logs index.
221+
*
222+
* @param string $repositoryName
223+
* @param string $index
224+
* @param OutputInterface $output
225+
*/
226+
private function createLogs(
227+
string $repositoryName,
228+
string $index,
229+
OutputInterface $output
230+
) {
231+
try {
232+
$this
233+
->logRepositoryBucket
234+
->findRepository($repositoryName, $index)
235+
->createIndex();
236+
} catch (ResourceNotAvailableException $exception) {
237+
$this->printInfoMessage(
238+
$output,
239+
$this->getHeader(),
240+
'Logs index is already created. Skipping.'
241+
);
242+
}
243+
}
244+
}

0 commit comments

Comments
 (0)