Skip to content

Commit

Permalink
:feat: Add factory class for service provider :closes: #7246
Browse files Browse the repository at this point in the history
  • Loading branch information
Khadreal committed Feb 13, 2025
1 parent 5b018e4 commit 74a587c
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 0 deletions.
1 change: 1 addition & 0 deletions inc/Engine/Common/PerformanceHints/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function register(): void {
$factory_array = [
$this->getContainer()->get( 'atf_factory' ),
$this->getContainer()->get( 'lrc_factory' ),
$this->getContainer()->get( 'preconnect_factory' ),
];

foreach ( $factory_array as $factory ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
namespace WP_Rocket\Engine\Media\PreconnectExternalDomains\Database\Queries;

use WP_Rocket\Engine\Common\PerformanceHints\Database\Queries\AbstractQueries;
use WP_Rocket\Engine\Common\PerformanceHints\Database\Queries\QueriesInterface;

class PreconnectExternalDomains extends AbstractQueries implements QueriesInterface {

/**
* Name of the database table to query.
*
* @var string
*/
protected $table_name = 'wpr_preconnect_external_domains';

/**
* String used to alias the database table in MySQL statement.
*
* Keep this short, but descriptive. I.E. "tr" for term relationships.
*
* This is used to avoid collisions with JOINs.
*
* @var string
*/
protected $table_alias = 'wpr_pre';

/**
* Name of class used to setup the database schema.
*
* @var string
*/
protected $table_schema = '';

/** Item ******************************************************************/

/**
* Name for a single item.
*
* Use underscores between words. I.E. "term_relationship"
*
* This is used to automatically generate action hooks.
*
* @var string
*/
protected $item_name = 'above_the_fold';

/**
* Plural version for a group of items.
*
* Use underscores between words. I.E. "term_relationships"
*
* This is used to automatically generate action hooks.
*
* @var string
*/
protected $item_name_plural = 'external_preconnect_domains';

/**
* Name of class used to turn IDs into first-class objects.
*
* This is used when looping through return values to guarantee their shape.
*
* @var mixed
*/
protected $item_shape = '';

/**
* Delete all rows which were not accessed in the last month.
*
* @return bool|int
*/
public function delete_old_rows() {
return false;
}
}
94 changes: 94 additions & 0 deletions inc/Engine/Media/PreconnectExternalDomains/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\PreconnectExternalDomains;

use WP_Rocket\Engine\Common\Context\ContextInterface;
use WP_Rocket\Engine\Common\PerformanceHints\AJAX\ControllerInterface as AjaxControllerInterface;
use WP_Rocket\Engine\Common\PerformanceHints\Database\Queries\QueriesInterface;
use WP_Rocket\Engine\Common\PerformanceHints\Database\Table\TableInterface;
use WP_Rocket\Engine\Common\PerformanceHints\FactoryInterface;
use WP_Rocket\Engine\Common\PerformanceHints\Frontend\ControllerInterface as FrontendControllerInterface;

class Factory implements FactoryInterface {
/**
* Ajax Controller instance.
*
* @var AjaxControllerInterface
*/
protected $ajax_controller;

/**
* Frontend Controller instance.
*
* @var FrontendControllerInterface
*/
protected $frontend_controller;

/**
* Table instance.
*
* @var TableInterface
*/
protected $table;

/**
* Queries instance.
*
* @var QueriesInterface
*/
protected $queries;

/**
* Context instance.
*
* @var ContextInterface
*/
protected $context;


/**
* Provides an Ajax controller object.
*
* @return AjaxControllerInterface
*/
public function get_ajax_controller(): AjaxControllerInterface {
return $this->ajax_controller;
}

/**
* Provides a Frontend controller object.
*
* @return FrontendControllerInterface
*/
public function get_frontend_controller(): FrontendControllerInterface {
return $this->frontend_controller;
}

/**
* Provides a Table interface object.
*
* @return TableInterface
*/
public function table(): TableInterface {
return $this->table;
}

/**
* Provides a Queries object.
*
* @return QueriesInterface
*/
public function queries(): QueriesInterface {
return $this->queries;
}

/**
* Provides Context object.
*
* @return ContextInterface
*/
public function get_context(): ContextInterface {
return $this->context;
}
}
45 changes: 45 additions & 0 deletions inc/Engine/Media/PreconnectExternalDomains/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\Media\PreconnectExternalDomains;

use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;
use WP_Rocket\Engine\Media\PreconnectExternalDomains\Database\Queries\PreconnectExternalDomains as Query;

class ServiceProvider extends AbstractServiceProvider {
/**
* The provides array is a way to let the container
* know that a service is provided by this service
* provider. Every service that is registered via
* this service provider must have an alias added
* to this array or it will be ignored.
*
* @var array
*/
protected $provides = [
'preconnect_factory',
'preconnect_query',
];

/**
* Check if the service provider provides a specific service.
*
* @param string $id The id of the service.
*
* @return bool
*/
public function provides( string $id ): bool {
return in_array( $id, $this->provides, true );
}


/**
* Registers the classes in the container
*
* @return void
*/
public function register(): void {
$this->getContainer()->add( 'preconnect_query', Query::class );
$this->getContainer()->addShared( 'preconnect_factory', Factory::class );
}
}
2 changes: 2 additions & 0 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
use WP_Rocket\Engine\Common\PerformanceHints\ServiceProvider as PerformanceHintsServiceProvider;
use WP_Rocket\Engine\Optimization\LazyRenderContent\ServiceProvider as LRCServiceProvider;
use WP_Rocket\Engine\Media\Fonts\ServiceProvider as MediaFontsServiceProvider;
use WP_Rocket\Engine\Media\PreconnectExternalDomains\ServiceProvider as PreconnectDomainsServiceProvider;


/**
Expand Down Expand Up @@ -311,6 +312,7 @@ private function init_common_subscribers() {
$this->container->addServiceProvider( new PerformanceHintsServiceProvider() );
$this->container->addServiceProvider( new LRCServiceProvider() );
$this->container->addServiceProvider( new MediaFontsServiceProvider() );
$this->container->addServiceProvider( new PreconnectDomainsServiceProvider() );

$common_subscribers = [
'license_subscriber',
Expand Down

0 comments on commit 74a587c

Please sign in to comment.