Skip to content

Commit e584eed

Browse files
committed
feat: Add FQDN class support to IndexLocator and IndexLocatorAwareTrait
This reflects newly added FQDN support in TableLocator (CakePHP 5.2.9)
1 parent ca1a3a3 commit e584eed

4 files changed

Lines changed: 74 additions & 13 deletions

File tree

src/Datasource/IndexLocator.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,22 @@ public function allowFallbackClass(bool $allow = true)
8181
*/
8282
protected function createInstance(string $alias, array $options): RepositoryInterface
8383
{
84-
[, $classAlias] = pluginSplit($alias);
85-
$options += [
86-
'name' => Inflector::underscore($classAlias),
87-
'className' => Inflector::camelize($alias),
88-
];
89-
$className = App::className($options['className'], 'Model/Index', 'Index');
84+
if (!str_contains($alias, '\\')) {
85+
[, $classAlias] = pluginSplit($alias);
86+
$options += [
87+
'name' => Inflector::underscore($classAlias),
88+
'className' => Inflector::camelize($alias),
89+
];
90+
} elseif (!isset($options['name'])) {
91+
$options['className'] = $alias;
92+
}
93+
94+
$className = App::className($options['className'] ?? $alias, 'Model/Index', 'Index');
9095
if ($className) {
9196
$options['className'] = $className;
9297
} elseif ($this->allowFallbackClass) {
93-
if (!isset($options['name']) && strpos($options['className'], '\\') === false) {
94-
[, $name] = pluginSplit($options['className']);
98+
if (!isset($options['name']) && strpos($options['className'] ?? $alias, '\\') === false) {
99+
[, $name] = pluginSplit($options['className'] ?? $alias);
95100
$options['name'] = Inflector::underscore($name);
96101
}
97102

src/Datasource/IndexLocatorAwareTrait.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@ public function getIndexLocator(): LocatorInterface
7070
/**
7171
* Convenience method to get an index instance.
7272
*
73-
* @param string|null $alias The alias name you want to get. Should be in CamelCase format.
73+
* @template T of \Cake\ElasticSearch\Index
74+
* @param class-string<T>|string|null $alias The alias name you want to get. Should be in CamelCase format.
7475
* If `null` then the value of $defaultIndex property is used.
7576
* @param array<string, mixed> $options The options you want to build the index with.
7677
* If an index has already been loaded the registry options will be ignored.
78+
* @return ($alias is class-string<T> ? T : \Cake\ElasticSearch\Index)
7779
* @throws \UnexpectedValueException If `$alias` argument and `$defaultIndex` property both are `null`.
7880
* @see \Cake\ElasticSearch\Datasource\IndexLocator::get()
7981
*/
@@ -86,9 +88,8 @@ public function fetchIndex(?string $alias = null, array $options = []): Index
8688
);
8789
}
8890

89-
$index = $this->getIndexLocator()->get($alias, $options);
90-
assert($index instanceof Index);
91-
92-
return $index;
91+
// phpcs:ignore
92+
/** @var T */
93+
return $this->getIndexLocator()->get($alias, $options);
9394
}
9495
}

tests/TestCase/Datasource/IndexLocatorAwareTraitTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,25 @@ public function testDefaultIndexProperty(): void
295295
$this->subject->setDefaultIndex(null);
296296
$this->assertNull($this->subject->getDefaultIndex());
297297
}
298+
299+
/**
300+
* Test fetchIndex passes FQDN class names through to locator
301+
*/
302+
public function testFetchIndexWithFqdnClassName(): void
303+
{
304+
$mockIndex = $this->createMock(Index::class);
305+
$fqdnClass = Index::class;
306+
307+
$this->mockLocator
308+
->expects($this->once())
309+
->method('get')
310+
->with($fqdnClass, [])
311+
->willReturn($mockIndex);
312+
313+
$this->subject->setIndexLocator($this->mockLocator);
314+
315+
$result = $this->subject->fetchIndex($fqdnClass);
316+
317+
$this->assertSame($mockIndex, $result);
318+
}
298319
}

tests/TestCase/Datasource/IndexLocatorTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,40 @@ public function testCreateInstanceWithFullNamespaceClassName(): void
373373
$this->assertSame('custom_name', $result->getName());
374374
}
375375

376+
/**
377+
* Test get() with FQDN class name
378+
*/
379+
public function testGetWithFqdnClassName(): void
380+
{
381+
// Using FQDN should work directly
382+
$result = $this->locator->get(UsersIndex::class);
383+
384+
$this->assertInstanceOf(UsersIndex::class, $result);
385+
$this->assertSame('users', $result->getName());
386+
387+
// Should be cached under FQDN alias
388+
$this->assertTrue($this->locator->exists(UsersIndex::class));
389+
390+
// Getting again should return same instance
391+
$result2 = $this->locator->get(UsersIndex::class);
392+
$this->assertSame($result, $result2);
393+
}
394+
395+
/**
396+
* Test get() with FQDN class name and custom options
397+
*/
398+
public function testGetWithFqdnClassNameAndOptions(): void
399+
{
400+
// Using FQDN with custom name option
401+
// Use Index::class which respects the name option
402+
$result = $this->locator->get(Index::class, [
403+
'name' => 'custom_users_index',
404+
]);
405+
406+
$this->assertInstanceOf(Index::class, $result);
407+
$this->assertSame('custom_users_index', $result->getName());
408+
}
409+
376410
/**
377411
* Test createInstance with fallback disabled and missing className
378412
*/

0 commit comments

Comments
 (0)