-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #290 from cakephp/add-locator
3.x - Add locator
- Loading branch information
Showing
8 changed files
with
585 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
version: "3.9" | ||
services: | ||
console: | ||
build: "." | ||
environment: | ||
DB_URL: "Cake\\ElasticSearch\\Datasource\\Connection://elasticsearch:9200?driver=Cake\\ElasticSearch\\Datasource\\Connection" | ||
volumes: | ||
- .:/code | ||
elasticsearch: | ||
image: "elasticsearch:7.17.4" | ||
ports: | ||
- 9200/tcp | ||
environment: | ||
discovery.type: single-node | ||
ES_JAVA_OPTS: -Xms500m -Xmx500m | ||
healthcheck: | ||
test: "curl -f http://127.0.0.1:9200/_cluster/health || exit 1" | ||
interval: "10s" | ||
timeout: "5s" | ||
retries: 10 | ||
start_period: "30s" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @since 3.5.0 | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace Cake\ElasticSearch\Datasource; | ||
|
||
use Cake\Core\App; | ||
use Cake\Datasource\ConnectionManager; | ||
use Cake\Datasource\Locator\AbstractLocator; | ||
use Cake\ElasticSearch\Exception\MissingIndexClassException; | ||
use Cake\ElasticSearch\Index; | ||
use Cake\Utility\Inflector; | ||
|
||
/** | ||
* Datasource FactoryLocator compatible locater implementation. | ||
*/ | ||
class IndexLocator extends AbstractLocator | ||
{ | ||
/** | ||
* Fallback class to use | ||
* | ||
* @var string | ||
* @psalm-var class-string<\Cake\Elasticsearch\Index> | ||
*/ | ||
protected $fallbackClassName = Index::class; | ||
|
||
/** | ||
* Whether fallback class should be used if a Index class could not be found. | ||
* | ||
* @var bool | ||
*/ | ||
protected $allowFallbackClass = true; | ||
|
||
/** | ||
* Set fallback class name. | ||
* | ||
* The class that should be used to create a table instance if a concrete | ||
* class for alias used in `get()` could not be found. Defaults to | ||
* `Cake\Elasticsearch\Index`. | ||
* | ||
* @param string $className Fallback class name | ||
* @return $this | ||
* @psalm-param class-string<\Cake\Elasticsearch\Index> $className | ||
*/ | ||
public function setFallbackClassName(string $className) | ||
{ | ||
$this->fallbackClassName = $className; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set if fallback class should be used. | ||
* | ||
* Controls whether a fallback class should be used to create a index | ||
* instance if a concrete class for alias used in `get()` could not be found. | ||
* | ||
* @param bool $allow Flag to enable or disable fallback | ||
* @return $this | ||
*/ | ||
public function allowFallbackClass(bool $allow) | ||
{ | ||
$this->allowFallbackClass = $allow; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function createInstance(string $alias, array $options) | ||
{ | ||
[, $classAlias] = pluginSplit($alias); | ||
$options += [ | ||
'name' => Inflector::underscore($classAlias), | ||
'className' => Inflector::camelize($alias), | ||
]; | ||
$className = App::className($options['className'], 'Model/Index', 'Index'); | ||
if ($className) { | ||
$options['className'] = $className; | ||
} elseif ($this->allowFallbackClass) { | ||
if (!isset($options['name']) && strpos($options['className'], '\\') === false) { | ||
[, $name] = pluginSplit($options['className']); | ||
$options['name'] = Inflector::underscore($name); | ||
} | ||
$options['className'] = $this->fallbackClassName; | ||
} else { | ||
throw new MissingIndexClassException(['name' => $alias]); | ||
} | ||
|
||
if (empty($options['connection'])) { | ||
$connectionName = $options['className']::defaultConnectionName(); | ||
$options['connection'] = ConnectionManager::get($connectionName); | ||
} | ||
$options['registryAlias'] = $alias; | ||
|
||
return new $options['className']($options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* MissingDocumentException file | ||
* | ||
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | ||
* @link https://cakephp.org CakePHP(tm) Project | ||
* @since 3.0.0 | ||
* @license https://opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace Cake\ElasticSearch\Exception; | ||
|
||
use Cake\Core\Exception\CakeException; | ||
|
||
/** | ||
* Exception raised when an index class cannot be found | ||
*/ | ||
class MissingIndexClassException extends CakeException | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $_messageTemplate = 'Index class %s could not be found.'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.