Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial version of ElasticPress DI Container #3559

Merged
merged 13 commits into from
Jul 28, 2023
Next Next commit
Initial version of ElasticPress DI Container
  • Loading branch information
felipeelia committed Jul 26, 2023
commit 4dfe4e2fd7271ea2b8085d245c6e572466e5277f
31 changes: 17 additions & 14 deletions elasticpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,28 @@ function( $class ) {
define( 'EP_IS_NETWORK', true );
}

/**
* Return the ElasticPress container
*
* @since 4.7.0
* @return Container
*/
function get_container() {
static $container = null;

if ( ! $container ) {
$container = new Container();
}

return $container;
}

/**
* Sets up the indexables and features.
*
* @return void
*/
function register_indexable_posts() {
global $wp_version;

/**
* Handle indexables
*/
Expand Down Expand Up @@ -177,18 +191,7 @@ function register_indexable_posts() {
SearchAlgorithms::factory()->register( new SearchAlgorithm\Version_350() );
SearchAlgorithms::factory()->register( new SearchAlgorithm\Version_400() );

/**
* Filter the query logger object
*
* @since 4.4.0
* @hook ep_query_logger
* @param {QueryLogger} $query_logger Default query logger
* @return {QueryLogger} New query logger
*/
$query_logger = apply_filters( 'ep_query_logger', new \ElasticPress\QueryLogger() );
if ( method_exists( $query_logger, 'setup' ) ) {
$query_logger->setup();
}
get_container()->set( '\ElasticPress\QueryLogger', new \ElasticPress\QueryLogger(), true );
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\register_indexable_posts' );

Expand Down
94 changes: 94 additions & 0 deletions includes/classes/Container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Container class
*
* @since 4.7.0
* @package elasticpress
* @see https://github.com/php-fig/container
*/

namespace ElasticPress;

/**
* (semi-)PSR11 compliant container class
*
* Although type hinting will work, we do not implement the interfaces yet.
*/
final class Container {
/**
* Hold all instances
*
* @var array<object>
*/
private $instances = [];

/**
* Finds an entry of the container by its identifier and returns it.
*
* @param string $id Identifier of the entry to look for.
*
* @throws \Exception No entry was found for **this** identifier.
*
* @return mixed Entry.
*/
public function get( string $id ) {
if ( ! isset( $this->instances[ $id ] ) ) {
throw new \Exception( 'Class not found' );
}

return $this->instances[ $id ];
}

/**
* Returns true if the container can return an entry for the given identifier.
* Returns false otherwise.
*
* @param string $id Identifier of the entry to look for.
*
* @return bool
*/
public function has( string $id ): bool {
return isset( $this->instances[ $id ] );
}

/**
* Register an instance.
*
* @param string $id Identifier of the entry.
* @param object $instance The new instance.
* @param boolean $setup Whether the setup() method should be called or not.
* @return object The instance.
*/
public function set( string $id, $instance, bool $setup = false ) {
/**
* Filter an instance before it is added to the container
*
* @since 4.7.0
* @hook ep_container_set
* @param {object} $instance Object instance
* @param {string} $id Id
* @return {object} New object
*/
$instance = apply_filters( 'ep_container_set', $instance, $id );

if ( '\ElasticPress\QueryLogger' === $id ) {
/**
* Filter the query logger object
*
* @since 4.4.0
* @hook ep_query_logger
* @param {QueryLogger} $query_logger Default query logger
* @return {QueryLogger} New query logger
*/
$instance = apply_filters( 'ep_query_logger', new \ElasticPress\QueryLogger() );
}

$this->instances[ $id ] = $instance;

if ( $setup && method_exists( $instance, 'setup' ) ) {
$instance->setup();
}

return $instance;
}
}
3 changes: 1 addition & 2 deletions includes/classes/Screen/StatusReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public function admin_enqueue_scripts() {
public function get_reports() : array {
$reports = [];

/* this filter is documented in elasticpress.php */
$query_logger = apply_filters( 'ep_query_logger', new \ElasticPress\QueryLogger() );
$query_logger = \ElasticPress\get_container()->get( '\ElasticPress\QueryLogger' );

if ( $query_logger ) {
$reports['failed-queries'] = new \ElasticPress\StatusReport\FailedQueries( $query_logger );
Expand Down