Skip to content

Commit f46cbc2

Browse files
committed
Added autowiring support
Having an apisearch configuration like this one ```yml apisearch: repositories: repo1: indices: index1: xxx ``` You can inject by autowiring the repositories as it follows. Because you can have multiple instances of the same interface, you must use named aliases, so it means that you will have to couple both layers ```php public function __construct( AppRepository $apisearchRepo1AppRepository, UserRepository $apisearchRepo1UserRepository, Repository $apisearchRepo1Index1Repository, TransformableRepository $apisearchRepo1Index1Repository ) ```
1 parent 406dc8f commit f46cbc2

File tree

7 files changed

+372
-260
lines changed

7 files changed

+372
-260
lines changed

.php_cs.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"php":"7.3.8-1+ubuntu16.04.1+deb.sury.org+1","version":"2.15.1:v2.15.1#20064511ab796593a3990669eff5f5b535001f7c","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":{"allow_single_line_closure":true},"class_definition":{"single_line":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,"lowercase_static_reference":true,"magic_constant_casing":true,"magic_method_casing":true,"native_function_casing":true,"native_function_type_declaration_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_types_order":{"null_adjustment":"always_last","sort_algorithm":"none"},"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,"single_trait_insert_per_statement":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\/ProductBothTransformer.php":3061253314,"Tests\/Functional\/ProductReadTransformer.php":1666987200,"Tests\/Functional\/ApisearchBundleFunctionalTest.php":1130132530,"Tests\/Functional\/ProductWriteTransformer.php":4090808924,"Tests\/Functional\/Command\/IndexCommandTest.php":4092266881,"Tests\/Functional\/Command\/PrintIndicesCommandTest.php":481741707,"Tests\/Functional\/Command\/QueryCommandTest.php":2580406218,"Tests\/Functional\/Command\/AddDeleteTokensCommandTest.php":1868952970,"Tests\/Functional\/DependencyInjection\/EnableCommandsTest.php":1149363544,"Tests\/Functional\/DependencyInjection\/CompilerPass\/InMemoryRepositoryCompilerPassTest.php":3052848166,"Tests\/Functional\/DependencyInjection\/CompilerPass\/TransformersCompilerPassTest.php":1388027657,"Tests\/Functional\/DependencyInjection\/CompilerPass\/HttpRepositoryCompilerPassTest.php":2500445244,"Tests\/Functional\/DependencyInjection\/DisableCommandsTest.php":2956180423,"Tests\/Functional\/DependencyInjection\/ApisearchConfigurationTest.php":271608124,"Translator\/AggregationTranslator.php":4238166597,"ApisearchBundle.php":3640212495,"Twig\/MethodAccessorExtension.php":2939807874,"Twig\/AggregationTranslationExtension.php":2415810000,"Twig\/UrlBuilderExtension.php":2201893333,"Command\/ImportIndexCommand.php":3461230433,"Command\/CreateIndexCommand.php":3731774129,"Command\/ApisearchFormattedCommand.php":1111995920,"Command\/ResetIndexCommand.php":3426927232,"Command\/DeleteIndexCommand.php":1350714493,"Command\/ApisearchCommand.php":3872958448,"Command\/WithAppRepositoryBucketCommand.php":2197592132,"Command\/PrintIndicesCommand.php":2224987197,"Command\/DeleteTokensCommand.php":290859330,"Command\/AddTokenCommand.php":684681072,"Command\/PrintTokensCommand.php":1079623314,"Command\/ExportIndexCommand.php":1133721139,"Command\/ConfigureIndexCommand.php":2680853722,"Command\/WithRepositoryBucketCommand.php":3434324818,"Command\/DeleteTokenCommand.php":121828308,"Command\/QueryCommand.php":3024631832,"DependencyInjection\/CompilerPass\/WriteTransformerCompilerPass.php":2530587380,"DependencyInjection\/CompilerPass\/ExporterCompilerPass.php":857546720,"DependencyInjection\/CompilerPass\/RepositoryCompilerPass.php":863427065,"DependencyInjection\/CompilerPass\/ReadTransformerCompilerPass.php":3948110591,"DependencyInjection\/ApisearchConfiguration.php":187997719,"DependencyInjection\/ApisearchExtension.php":112225700}}
1+
{"php":"7.3.9-1+ubuntu18.04.1+deb.sury.org+1","version":"2.16.0:v2.16.0#ceaff36bee1ed3f1bbbedca36d2528c0826c336d","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":{"allow_single_line_closure":true},"class_definition":{"single_line":true},"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":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,"array_syntax":{"syntax":"short"},"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":true,"declare_equal_normalize":true,"function_typehint_space":true,"include":true,"increment_style":true,"lowercase_cast":true,"lowercase_static_reference":true,"magic_constant_casing":true,"magic_method_casing":true,"native_function_casing":true,"native_function_type_declaration_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":true,"no_multiline_whitespace_around_double_arrow":true,"no_short_bool_cast":true,"no_singleline_whitespace_before_semicolons":true,"no_spaces_around_offset":true,"no_superfluous_phpdoc_tags":{"allow_mixed":true,"allow_unused_params":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,"ordered_imports":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_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_trim_consecutive_blank_line_separation":true,"phpdoc_types":true,"phpdoc_types_order":{"null_adjustment":"always_last","sort_algorithm":"none"},"phpdoc_var_without_name":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_line_throw":true,"single_quote":true,"single_trait_insert_per_statement":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\/AService.php":3371820063,"Tests\/Functional\/DependencyInjection\/EnableCommandsTest.php":1794882875,"Tests\/Functional\/DependencyInjection\/CompilerPass\/InMemoryRepositoryCompilerPassTest.php":3620326332,"Tests\/Functional\/DependencyInjection\/CompilerPass\/TransformersCompilerPassTest.php":2286676217,"Tests\/Functional\/DependencyInjection\/CompilerPass\/HttpRepositoryCompilerPassTest.php":3050954938,"Tests\/Functional\/DependencyInjection\/ApisearchConfigurationTest.php":271608124,"Tests\/Functional\/DependencyInjection\/DisableCommandsTest.php":2271614667,"Tests\/Functional\/ApisearchBundleFunctionalTest.php":1563748362,"Tests\/Functional\/ProductWriteTransformer.php":2899478219,"Tests\/Functional\/ProductBothTransformer.php":479133393,"Tests\/Functional\/Command\/PrintIndicesCommandTest.php":481741707,"Tests\/Functional\/Command\/QueryCommandTest.php":2580406218,"Tests\/Functional\/Command\/AddDeleteTokensCommandTest.php":1868952970,"Tests\/Functional\/Command\/IndexCommandTest.php":4092266881,"Tests\/Functional\/ProductReadTransformer.php":1717369761,"Tests\/Functional\/AutowiringTest.php":1216361311,"DependencyInjection\/ApisearchExtension.php":4206190540,"DependencyInjection\/CompilerPass\/ReadTransformerCompilerPass.php":3948110591,"DependencyInjection\/CompilerPass\/ExporterCompilerPass.php":857546720,"DependencyInjection\/CompilerPass\/WriteTransformerCompilerPass.php":2530587380,"DependencyInjection\/CompilerPass\/RepositoryCompilerPass.php":3605104821,"DependencyInjection\/ApisearchConfiguration.php":1136730770,"Twig\/UrlBuilderExtension.php":1095204211,"Twig\/AggregationTranslationExtension.php":257790318,"Twig\/MethodAccessorExtension.php":2939807874,"Command\/ApisearchCommand.php":3928891909,"Command\/AddTokenCommand.php":3051589506,"Command\/WithRepositoryBucketCommand.php":1083077272,"Command\/DeleteTokensCommand.php":877678679,"Command\/ImportIndexCommand.php":3607079391,"Command\/ApisearchFormattedCommand.php":3862548494,"Command\/PrintTokensCommand.php":3348297778,"Command\/DeleteIndexCommand.php":1546190123,"Command\/DeleteTokenCommand.php":3627377100,"Command\/QueryCommand.php":2332984996,"Command\/WithAppRepositoryBucketCommand.php":759489361,"Command\/PrintIndicesCommand.php":885255525,"Command\/ResetIndexCommand.php":4125855051,"Command\/ConfigureIndexCommand.php":2323095110,"Command\/CreateIndexCommand.php":435275942,"Command\/ExportIndexCommand.php":3253784633,"Translator\/AggregationTranslator.php":3711919960,"ApisearchBundle.php":4293551145}}

DependencyInjection/CompilerPass/RepositoryCompilerPass.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Apisearch\DependencyInjection\CompilerPass;
1717

18+
use Apisearch\App\AppRepository;
1819
use Apisearch\App\DiskAppRepository;
1920
use Apisearch\App\HttpAppRepository;
2021
use Apisearch\App\InMemoryAppRepository;
@@ -27,11 +28,13 @@
2728
use Apisearch\Repository\DiskRepository;
2829
use Apisearch\Repository\HttpRepository;
2930
use Apisearch\Repository\InMemoryRepository;
31+
use Apisearch\Repository\Repository;
3032
use Apisearch\Repository\RepositoryReference;
3133
use Apisearch\Repository\TransformableRepository;
3234
use Apisearch\User\DiskUserRepository;
3335
use Apisearch\User\HttpUserRepository;
3436
use Apisearch\User\InMemoryUserRepository;
37+
use Apisearch\User\UserRepository;
3538
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
3639
use Symfony\Component\DependencyInjection\ContainerBuilder;
3740
use Symfony\Component\DependencyInjection\Definition;
@@ -110,7 +113,8 @@ private function createAppRepositories(
110113
'app',
111114
InMemoryAppRepository::class,
112115
DiskAppRepository::class,
113-
HttpAppRepository::class
116+
HttpAppRepository::class,
117+
AppRepository::class
114118
);
115119

116120
$this->createStandardRepository(
@@ -121,7 +125,8 @@ private function createAppRepositories(
121125
'user',
122126
InMemoryUserRepository::class,
123127
DiskUserRepository::class,
124-
HttpUserRepository::class
128+
HttpUserRepository::class,
129+
UserRepository::class
125130
);
126131
}
127132

@@ -162,7 +167,6 @@ private function createIndexRepositories(
162167
*
163168
* @param ContainerBuilder $container
164169
* @param string $name
165-
* @param string $name
166170
* @param array $repositoryConfiguration
167171
*/
168172
private function createClientRetryMap(
@@ -226,19 +230,20 @@ private function createClient(
226230
* Create a repository by connection configuration.
227231
*
228232
* @param ContainerBuilder $container
229-
* @param string $name
233+
* @param string $appName
230234
* @param array $repositoryConfiguration
231235
* @param string $indexName
232236
*/
233237
private function createSearchRepository(
234238
ContainerBuilder $container,
235-
string $name,
239+
string $appName,
236240
array $repositoryConfiguration,
237241
string $indexName
238242
) {
239-
$repositoryName = "apisearch.repository_$name.$indexName";
240-
$repositoryTransformableName = "apisearch.repository_transformable_$name.$indexName";
241-
$clientName = "apisearch.client_$name";
243+
$repositoryName = "apisearch.repository_$appName.$indexName";
244+
$aliasName = rtrim(sprintf("apisearch %s %s repository", $appName, $indexName), '.');
245+
$repositoryTransformableName = "apisearch.repository_transformable_$appName.$indexName";
246+
$clientName = "apisearch.client_$appName";
242247

243248
switch ($repositoryConfiguration['adapter']) {
244249
case 'in_memory':
@@ -250,7 +255,7 @@ private function createSearchRepository(
250255
case 'disk':
251256
$repositoryDefinition = $container
252257
->register($repositoryName, DiskRepository::class)
253-
->addArgument($repositoryConfiguration['disk_file'].'.'.$name)
258+
->addArgument($repositoryConfiguration['disk_file'].'.'.$appName)
254259
->setPublic($this->repositoryIsTest($repositoryConfiguration));
255260
break;
256261

@@ -272,7 +277,7 @@ private function createSearchRepository(
272277

273278
$this->injectRepositoryCredentials(
274279
$repositoryDefinition,
275-
$name,
280+
$appName,
276281
$repositoryConfiguration,
277282
$indexName
278283
);
@@ -286,7 +291,7 @@ private function createSearchRepository(
286291

287292
$this->injectRepositoryCredentials(
288293
$definition,
289-
$name,
294+
$appName,
290295
$repositoryConfiguration,
291296
$indexName
292297
);
@@ -295,9 +300,11 @@ private function createSearchRepository(
295300
->getDefinition('apisearch.repository_bucket')
296301
->addMethodCall(
297302
'addRepository',
298-
[$name, $indexName, new Reference($repositoryName)]
303+
[$appName, $indexName, new Reference($repositoryName)]
299304
)
300305
->setPublic($this->repositoryIsTest($repositoryConfiguration));
306+
307+
$container->registerAliasForArgument($repositoryName, Repository::class, $aliasName);
301308
}
302309

303310
/**
@@ -311,6 +318,7 @@ private function createSearchRepository(
311318
* @param string $inMemoryRepositoryNamespace
312319
* @param string $diskRepositoryNamespace
313320
* @param string $httpRepositoryNamespace
321+
* @param string $interfaceNamespace
314322
*/
315323
private function createStandardRepository(
316324
ContainerBuilder $container,
@@ -320,9 +328,11 @@ private function createStandardRepository(
320328
string $prefix,
321329
string $inMemoryRepositoryNamespace,
322330
string $diskRepositoryNamespace,
323-
string $httpRepositoryNamespace
331+
string $httpRepositoryNamespace,
332+
string $interfaceNamespace
324333
) {
325334
$repositoryName = rtrim("apisearch.{$prefix}_repository_$appName.$indexName", '.');
335+
$aliasName = rtrim(sprintf("apisearch %s %s %s repository", $appName, $indexName, $prefix), '.');
326336
$tokenUUIDName = rtrim("apisearch.token_uuid.$appName");
327337
$clientName = rtrim("apisearch.client_$appName", '.');
328338

@@ -391,6 +401,8 @@ private function createStandardRepository(
391401
[$appName, new Reference($repositoryName)]
392402
)
393403
->setPublic($this->repositoryIsTest($repositoryConfiguration));
404+
405+
$container->registerAliasForArgument($repositoryName, $interfaceNamespace, $aliasName);
394406
}
395407

396408
/**

Tests/Functional/AService.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
4+
namespace Apisearch\Tests\Functional;
5+
6+
use Apisearch\App\AppRepository;
7+
use Apisearch\Repository\Repository;
8+
use Apisearch\User\UserRepository;
9+
10+
/**
11+
* Class AService
12+
*/
13+
class AService
14+
{
15+
public $appRepo1;
16+
public $appRepo2;
17+
public $appRepo3;
18+
19+
public $userRepo1;
20+
public $userRepo2;
21+
22+
public $repo1;
23+
public $repo2;
24+
25+
/**
26+
* AService constructor.
27+
*
28+
* @param AppRepository $apisearchMain2AppRepository
29+
* @param AppRepository $apisearchApp123nameAppRepository
30+
* @param AppRepository $apisearchMainAppRepository
31+
*
32+
* @param UserRepository $apisearchMainUserRepository
33+
* @param UserRepository $apisearchApp123nameUserRepository
34+
*
35+
* @param Repository $apisearchMain2DefaultRepository
36+
* @param Repository $apisearchApp123nameIndex123nameRepository
37+
*/
38+
public function __construct(
39+
AppRepository $apisearchMain2AppRepository,
40+
AppRepository $apisearchApp123nameAppRepository,
41+
AppRepository $apisearchMainAppRepository,
42+
43+
UserRepository $apisearchMainUserRepository,
44+
UserRepository $apisearchApp123nameUserRepository,
45+
46+
Repository $apisearchMain2DefaultRepository,
47+
Repository $apisearchApp123nameIndex123nameRepository
48+
)
49+
{
50+
$this->appRepo1 = $apisearchMain2AppRepository;
51+
$this->appRepo2 = $apisearchApp123nameAppRepository;
52+
$this->appRepo3 = $apisearchMainAppRepository;
53+
54+
$this->userRepo1 = $apisearchMainUserRepository;
55+
$this->userRepo2 = $apisearchApp123nameUserRepository;
56+
57+
$this->repo1 = $apisearchMain2DefaultRepository;
58+
$this->repo2 = $apisearchApp123nameIndex123nameRepository;
59+
}
60+
}

0 commit comments

Comments
 (0)