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

Commit cc0cd46

Browse files
committed
Added some commands
- Added list tokens command in bundle - Created base symfony command template - Added static buckets creation and dynamic population - Refactored commands
1 parent 66082b2 commit cc0cd46

File tree

11 files changed

+407
-98
lines changed

11 files changed

+407
-98
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.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}}
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":3313771574,"Command\/ApisearchCommand.php":1883682797,"Command\/ApisearchFormattedCommand.php":894019063,"Command\/WithRepositoryBucketCommand.php":4111986054,"Command\/WithAppRepositoryBucketCommand.php":4212053559,"Command\/ResetIndexCommand.php":4289827141,"Tests\/Functional\/DependencyInjection\/ApisearchConfigurationTest.php":379272062}}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 Exception;
20+
use Symfony\Component\Console\Input\InputInterface;
21+
use Symfony\Component\Console\Output\OutputInterface;
22+
23+
/**
24+
* Class ApisearchFormattedCommand.
25+
*/
26+
abstract class ApisearchFormattedCommand extends ApisearchCommand
27+
{
28+
/**
29+
* Executes the current command.
30+
*
31+
* This method is not abstract because you can use this class
32+
* as a concrete class. In this case, instead of defining the
33+
* execute() method, you set the code to execute by passing
34+
* a Closure to the setCode() method.
35+
*
36+
* @return null|int null or 0 if everything went fine, or an error code
37+
*/
38+
protected function execute(InputInterface $input, OutputInterface $output)
39+
{
40+
$this->startCommand($output);
41+
try {
42+
$result = $this->runCommand(
43+
$input,
44+
$output
45+
);
46+
$successfulMessage = $this->getSuccessMessage($input, $result);
47+
if (!empty($successfulMessage)) {
48+
$this->printMessage(
49+
$output,
50+
$this->getHeader(),
51+
$this->getSuccessMessage($input, $result)
52+
);
53+
}
54+
} catch (Exception $e) {
55+
$this->printMessageFail(
56+
$output,
57+
$this->getHeader(),
58+
$e->getMessage()
59+
);
60+
}
61+
62+
$this->finishCommand($output);
63+
}
64+
65+
/**
66+
* Dispatch domain event.
67+
*
68+
* @return string
69+
*/
70+
abstract protected function getHeader(): string;
71+
72+
/**
73+
* Dispatch domain event.
74+
*
75+
* @param InputInterface $input
76+
* @param OutputInterface $output
77+
*
78+
* @return mixed|null
79+
*/
80+
abstract protected function runCommand(InputInterface $input, OutputInterface $output);
81+
82+
/**
83+
* Get success message.
84+
*
85+
* @param InputInterface $input
86+
* @param mixed $result
87+
*
88+
* @return string
89+
*/
90+
abstract protected function getSuccessMessage(
91+
InputInterface $input,
92+
$result
93+
): string;
94+
}

Command/ExportIndexCommand.php

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,15 @@
1919
use Apisearch\Model\Coordinate;
2020
use Apisearch\Model\Item;
2121
use Apisearch\Query\Query;
22-
use Apisearch\Repository\RepositoryBucket;
2322
use Symfony\Component\Console\Input\InputArgument;
2423
use Symfony\Component\Console\Input\InputInterface;
2524
use Symfony\Component\Console\Output\OutputInterface;
2625

2726
/**
2827
* ExportIndexCommand.
2928
*/
30-
class ExportIndexCommand extends ApisearchCommand
29+
class ExportIndexCommand extends WithRepositoryBucketCommand
3130
{
32-
/**
33-
* @var RepositoryBucket
34-
*
35-
* Repository bucket
36-
*/
37-
private $repositoryBucket;
38-
39-
/**
40-
* ResetIndexCommand constructor.
41-
*
42-
* @param RepositoryBucket $repositoryBucket
43-
*/
44-
public function __construct(RepositoryBucket $repositoryBucket)
45-
{
46-
parent::__construct();
47-
48-
$this->repositoryBucket = $repositoryBucket;
49-
}
50-
5131
/**
5232
* Configures the current command.
5333
*/
@@ -74,21 +54,24 @@ protected function configure()
7454
}
7555

7656
/**
77-
* Executes the current command.
78-
*
79-
* This method is not abstract because you can use this class
80-
* as a concrete class. In this case, instead of defining the
81-
* execute() method, you set the code to execute by passing
82-
* a Closure to the setCode() method.
57+
* Dispatch domain event.
8358
*
84-
* @param InputInterface $input An InputInterface instance
85-
* @param OutputInterface $output An OutputInterface instance
59+
* @return string
60+
*/
61+
protected function getHeader(): string
62+
{
63+
return 'Export index';
64+
}
65+
66+
/**
67+
* Dispatch domain event.
8668
*
87-
* @return null|int null or 0 if everything went fine, or an error code
69+
* @param InputInterface $input
70+
* @param OutputInterface $output
8871
*
89-
* @see setCode()
72+
* @return mixed|null
9073
*/
91-
protected function execute(InputInterface $input, OutputInterface $output)
74+
protected function runCommand(InputInterface $input, OutputInterface $output)
9275
{
9376
$repositoryName = $input->getArgument('repository');
9477
$indexName = $input->getArgument('index');
@@ -121,6 +104,21 @@ protected function execute(InputInterface $input, OutputInterface $output)
121104
fclose($resource);
122105
}
123106

107+
/**
108+
* Get success message.
109+
*
110+
* @param InputInterface $input
111+
* @param mixed $result
112+
*
113+
* @return string
114+
*/
115+
protected function getSuccessMessage(
116+
InputInterface $input,
117+
$result
118+
): string {
119+
return 'Index exported properly';
120+
}
121+
124122
/**
125123
* Echo items as CSV.
126124
*

Command/ImportIndexCommand.php

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,15 @@
1717
namespace Apisearch\Command;
1818

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

2524
/**
2625
* ImportIndexCommand.
2726
*/
28-
class ImportIndexCommand extends ApisearchCommand
27+
class ImportIndexCommand extends WithRepositoryBucketCommand
2928
{
30-
/**
31-
* @var RepositoryBucket
32-
*
33-
* Repository bucket
34-
*/
35-
private $repositoryBucket;
36-
37-
/**
38-
* ResetIndexCommand constructor.
39-
*
40-
* @param RepositoryBucket $repositoryBucket
41-
*/
42-
public function __construct(RepositoryBucket $repositoryBucket)
43-
{
44-
parent::__construct();
45-
46-
$this->repositoryBucket = $repositoryBucket;
47-
}
48-
4929
/**
5030
* Configures the current command.
5131
*/
@@ -72,21 +52,24 @@ protected function configure()
7252
}
7353

7454
/**
75-
* Executes the current command.
76-
*
77-
* This method is not abstract because you can use this class
78-
* as a concrete class. In this case, instead of defining the
79-
* execute() method, you set the code to execute by passing
80-
* a Closure to the setCode() method.
55+
* Dispatch domain event.
8156
*
82-
* @param InputInterface $input An InputInterface instance
83-
* @param OutputInterface $output An OutputInterface instance
57+
* @return string
58+
*/
59+
protected function getHeader(): string
60+
{
61+
return 'Import index';
62+
}
63+
64+
/**
65+
* Dispatch domain event.
8466
*
85-
* @return null|int null or 0 if everything went fine, or an error code
67+
* @param InputInterface $input
68+
* @param OutputInterface $output
8669
*
87-
* @see setCode()
70+
* @return mixed|null
8871
*/
89-
protected function execute(InputInterface $input, OutputInterface $output)
72+
protected function runCommand(InputInterface $input, OutputInterface $output)
9073
{
9174
$repositoryName = $input->getArgument('repository');
9275
$indexName = $input->getArgument('index');
@@ -124,4 +107,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
124107
fclose($handle);
125108
}
126109
}
110+
111+
/**
112+
* Get success message.
113+
*
114+
* @param InputInterface $input
115+
* @param mixed $result
116+
*
117+
* @return string
118+
*/
119+
protected function getSuccessMessage(
120+
InputInterface $input,
121+
$result
122+
): string {
123+
return 'Index imported properly';
124+
}
127125
}

Command/PrintTokensCommand.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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\Token\Token;
20+
use Symfony\Component\Console\Helper\Table;
21+
use Symfony\Component\Console\Input\InputArgument;
22+
use Symfony\Component\Console\Input\InputInterface;
23+
use Symfony\Component\Console\Output\OutputInterface;
24+
25+
/**
26+
* Class PrintTokensCommand.
27+
*/
28+
class PrintTokensCommand extends WithAppRepositoryBucketCommand
29+
{
30+
/**
31+
* Configures the current command.
32+
*/
33+
protected function configure()
34+
{
35+
$this
36+
->setName('apisearch:print-tokens')
37+
->setDescription('Print all tokens of an app-id')
38+
->addArgument(
39+
'repository',
40+
InputArgument::REQUIRED,
41+
'Repository name'
42+
);
43+
}
44+
45+
/**
46+
* Dispatch domain event.
47+
*
48+
* @return string
49+
*/
50+
protected function getHeader(): string
51+
{
52+
return 'Print tokens';
53+
}
54+
55+
/**
56+
* Get success message.
57+
*
58+
* @param InputInterface $input
59+
* @param mixed $result
60+
*
61+
* @return string
62+
*/
63+
protected function getSuccessMessage(
64+
InputInterface $input,
65+
$result
66+
): string {
67+
return '';
68+
}
69+
70+
/**
71+
* Dispatch domain event.
72+
*
73+
* @param InputInterface $input
74+
* @param OutputInterface $output
75+
*
76+
* @return mixed|null
77+
*/
78+
protected function runCommand(InputInterface $input, OutputInterface $output)
79+
{
80+
$tokens = $this
81+
->repositoryBucket->findRepository(
82+
$input->getArgument('repository')
83+
)
84+
->getTokens();
85+
86+
/**
87+
* @var Token
88+
*/
89+
$table = new Table($output);
90+
$table->setHeaders(['UUID', 'Indices', 'Seconds Valid', 'Max hits per query', 'HTTP Referrers', 'endpoints', 'plugins', 'ttl']);
91+
foreach ($tokens as $token) {
92+
$table->addRow([
93+
$token->getTokenUUID()->composeUUID(),
94+
implode(', ', $token->getIndices()),
95+
$token->getSecondsValid(),
96+
$token->getMaxHitsPerQuery(),
97+
implode(', ', $token->getHttpReferrers()),
98+
implode(', ', $token->getEndpoints()),
99+
implode(', ', $token->getPlugins()),
100+
$token->getTtl(),
101+
]);
102+
}
103+
$table->render();
104+
}
105+
}

0 commit comments

Comments
 (0)