Skip to content

Commit

Permalink
Refactor caching routes registration
Browse files Browse the repository at this point in the history
* Eliminate WP_Service_Worker_Registry and WP_Service_Worker_Registry_Aware interfaces.
* Let second argument to WP_Service_Worker_Caching_Routes::register() be strategy name and third argument be strategy args.
  • Loading branch information
westonruter committed Dec 4, 2020
1 parent f1381df commit 5f1c00a
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 146 deletions.
6 changes: 0 additions & 6 deletions pwa.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,6 @@ function _pwa_check_disabled_navigation_preload() {
/** WP_HTTPS_UI Class */
require_once PWA_PLUGIN_DIR . '/wp-includes/class-wp-https-ui.php';

/** WP_Service_Worker_Registry Interface */
require_once PWA_PLUGIN_DIR . '/wp-includes/interface-wp-service-worker-registry.php';

/** WP_Service_Worker_Registry_Aware Interface */
require_once PWA_PLUGIN_DIR . '/wp-includes/interface-wp-service-worker-registry-aware.php';

/** WP_Service_Workers Class */
require_once PWA_PLUGIN_DIR . '/wp-includes/class-wp-service-workers.php';

Expand Down
66 changes: 37 additions & 29 deletions wp-includes/class-wp-service-worker-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
* @since 0.1
*
* @see WP_Dependencies
*
* @method WP_Service_Worker_Precaching_Routes precaching_routes()
* @method WP_Service_Worker_Caching_Routes caching_routes()
*/
class WP_Service_Worker_Scripts extends WP_Scripts implements WP_Service_Worker_Registry {
class WP_Service_Worker_Scripts extends WP_Scripts {

/**
* Service worker components.
Expand All @@ -27,30 +24,36 @@ class WP_Service_Worker_Scripts extends WP_Scripts implements WP_Service_Worker_
protected $components = array();

/**
* Service worker component registries.
* Caching routes.
*
* @since 0.2
* @var array
* @since 0.6
* @var WP_Service_Worker_Caching_Routes
*/
protected $registries = array();
protected $caching_routes;

/**
* Precaching routes.
*
* @since 0.6
* @var WP_Service_Worker_Precaching_Routes
*/
protected $precaching_routes;

/**
* Constructor.
*
* @since 0.2
*
* @param array $components Optional. Service worker components as $slug => $instance pairs.
* Each component must implement `WP_Service_Worker_Component`.
* Default empty array.
* @param WP_Service_Worker_Caching_Routes $caching_routes Caching routes.
* @param WP_Service_Worker_Precaching_Routes $precaching_routes Precaching routes.
* @param array $components Optional. Service worker components as $slug => $instance pairs.
* Each component must implement `WP_Service_Worker_Component`.
* Default empty array.
*/
public function __construct( $components = array() ) {
$this->components = $components;
foreach ( $this->components as $slug => $instance ) {
if ( $instance instanceof WP_Service_Worker_Registry_Aware ) {
$this->registries[ $slug ] = $instance->get_registry();
}
}

public function __construct( $caching_routes, $precaching_routes, $components = array() ) {
$this->caching_routes = $caching_routes;
$this->precaching_routes = $precaching_routes;
$this->components = $components;
parent::__construct();
}

Expand All @@ -77,20 +80,25 @@ public function init() {
}

/**
* Magic call method. Allows accessing component registries.
* Get caching routes registry.
*
* @since 0.2
* @since 0.6
*
* @param string $method Method name. Should be the identifier for a component registry.
* @param array $args Method arguments.
* @return WP_Service_Worker_Registry|null Registry instance if valid method name, otherwise null.
* @return WP_Service_Worker_Caching_Routes Registry.
*/
public function __call( $method, $args ) {
if ( isset( $this->registries[ $method ] ) ) {
return $this->registries[ $method ];
}
public function caching_routes() {
return $this->caching_routes;
}

return null;
/**
* Get precaching routes registry.
*
* @since 0.6
*
* @return WP_Service_Worker_Precaching_Routes Registry.
*/
public function precaching_routes() {
return $this->precaching_routes;
}

/**
Expand Down
27 changes: 23 additions & 4 deletions wp-includes/class-wp-service-workers.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @see WP_Dependencies
*/
class WP_Service_Workers implements WP_Service_Worker_Registry_Aware {
class WP_Service_Workers {

/**
* Param for service workers.
Expand Down Expand Up @@ -50,24 +50,43 @@ class WP_Service_Workers implements WP_Service_Worker_Registry_Aware {
*/
protected $scripts;

/**
* Caching routes.
*
* @since 0.6
* @var WP_Service_Worker_Caching_Routes
*/
protected $caching_routes;

/**
* Precaching routes.
*
* @since 0.6
* @var WP_Service_Worker_Precaching_Routes
*/
protected $precaching_routes;

/**
* Constructor.
*
* Instantiates the service worker scripts registry.
*/
public function __construct() {
$this->precaching_routes = new WP_Service_Worker_Precaching_Routes();
$this->caching_routes = new WP_Service_Worker_Caching_Routes();

$components = array(
'configuration' => new WP_Service_Worker_Configuration_Component(),
'navigation_routing' => new WP_Service_Worker_Navigation_Routing_Component(),
'core_asset_caching' => new WP_Service_Worker_Core_Asset_Caching_Component(),
'theme_asset_caching' => new WP_Service_Worker_Theme_Asset_Caching_Component(),
'plugin_asset_caching' => new WP_Service_Worker_Plugin_Asset_Caching_Component(),
'uploaded_image_caching' => new WP_Service_Worker_Uploaded_Image_Caching_Component(),
'precaching_routes' => new WP_Service_Worker_Precaching_Routes_Component(),
'caching_routes' => new WP_Service_Worker_Caching_Routes_Component(),
'precaching_routes' => new WP_Service_Worker_Precaching_Routes_Component( $this->precaching_routes ),
'caching_routes' => new WP_Service_Worker_Caching_Routes_Component( $this->caching_routes ),
);

$this->scripts = new WP_Service_Worker_Scripts( $components );
$this->scripts = new WP_Service_Worker_Scripts( $this->caching_routes, $this->precaching_routes, $components );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @since 0.2
*/
class WP_Service_Worker_Caching_Routes_Component implements WP_Service_Worker_Component, WP_Service_Worker_Registry_Aware {
class WP_Service_Worker_Caching_Routes_Component implements WP_Service_Worker_Component {

/**
* Caching routes registry.
Expand All @@ -26,9 +26,11 @@ class WP_Service_Worker_Caching_Routes_Component implements WP_Service_Worker_Co
* Instantiates the registry.
*
* @since 0.2
*
* @param WP_Service_Worker_Caching_Routes $registry Registry.
*/
public function __construct() {
$this->registry = new WP_Service_Worker_Caching_Routes();
public function __construct( WP_Service_Worker_Caching_Routes $registry ) {
$this->registry = $registry;
}

/**
Expand Down Expand Up @@ -59,15 +61,6 @@ public function get_priority() {
return 999999;
}

/**
* Gets the registry.
*
* @return WP_Service_Worker_Caching_Routes Caching routes registry instance.
*/
public function get_registry() {
return $this->registry;
}

/**
* Gets the script that registers the caching routes.
*
Expand Down
49 changes: 28 additions & 21 deletions wp-includes/components/class-wp-service-worker-caching-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @since 0.2
*/
class WP_Service_Worker_Caching_Routes implements WP_Service_Worker_Registry {
class WP_Service_Worker_Caching_Routes {

/**
* Stale while revalidate caching strategy.
Expand Down Expand Up @@ -79,36 +79,43 @@ class WP_Service_Worker_Caching_Routes implements WP_Service_Worker_Registry {
*
* @since 0.2
*
* @param string $route Route regular expression, without delimiters.
* @param array|string $args {
* Additional route arguments, or else just the strategy name as a string.
* @param string $route Route regular expression, without delimiters.
* @param string|array $strategy Strategy, can be WP_Service_Worker_Caching_Routes::STRATEGY_STALE_WHILE_REVALIDATE,
* WP_Service_Worker_Caching_Routes::STRATEGY_CACHE_FIRST, WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_FIRST,
* WP_Service_Worker_Caching_Routes::STRATEGY_CACHE_ONLY, WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_ONLY.
* Deprecated usage: supplying strategy args as an array.
* @param array $args {
* Additional caching strategy route arguments.
*
* @type string $strategy Strategy, can be WP_Service_Worker_Caching_Routes::STRATEGY_STALE_WHILE_REVALIDATE,
* WP_Service_Worker_Caching_Routes::STRATEGY_CACHE_FIRST, WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_FIRST,
* WP_Service_Worker_Caching_Routes::STRATEGY_CACHE_ONLY, WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_ONLY.
* Required.
* @type string $cache_name Name to use for the cache.
* @type array|null $expiration Expiration plugin configuration. See <https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-expiration.ExpirationPlugin>.
* @type array|null $broadcast_update Broadcast update plugin configuration. See <https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-broadcast-update.BroadcastUpdatePlugin>.
* @type array|null $cacheable_response Cacheable response plugin configuration. See <https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-cacheable-response.CacheableResponsePlugin>.
* @type array|null $background_sync Background sync plugin configuration. See <https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-background-sync.BackgroundSyncPlugin>.
* @type array|null $plugins Deprecated. Array of plugins with configuration. The key of each plugin in the array must match the plugin's name.
* This is deprecated in favor of defining the plugins in the top-level.
* See <https://developers.google.com/web/tools/workbox/guides/using-plugins#workbox_plugins>.
* @type string $cache_name Name to use for the cache.
* @type array $expiration Expiration plugin configuration. See <https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-expiration.ExpirationPlugin>.
* @type array $broadcast_update Broadcast update plugin configuration. See <https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-broadcast-update.BroadcastUpdatePlugin>.
* @type array $cacheable_response Cacheable response plugin configuration. See <https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-cacheable-response.CacheableResponsePlugin>.
* @type array $background_sync Background sync plugin configuration. See <https://developers.google.com/web/tools/workbox/reference-docs/latest/module-workbox-background-sync.BackgroundSyncPlugin>.
* @type array $plugins Deprecated. Array of plugins with configuration. The key of each plugin in the array must match the plugin's name.
* This is deprecated in favor of defining the plugins in the top-level.
* See <https://developers.google.com/web/tools/workbox/guides/using-plugins#workbox_plugins>.
* @return bool Whether the registration was successful.
* }
*/
public function register( $route, $args = array() ) {
if ( is_string( $args ) ) {
$args = array( 'strategy' => $args );
} elseif ( ! is_array( $args ) ) {
public function register( $route, $strategy, $args = array() ) {

// In versions prior to 0.6, this argument could either be a strategy string or an array of strategy args containing the strategy name.
if ( is_array( $strategy ) ) {
$args = $strategy;
$strategy = isset( $args['strategy'] ) ? $args['strategy'] : null;
unset( $args['strategy'] );
}

if ( empty( $strategy ) || ! is_string( $strategy ) ) {
_doing_it_wrong(
__METHOD__,
esc_html__( 'Args must either be an array or a strategy name as a string.', 'pwa' ),
esc_html__( 'Strategy must be supplied.', 'pwa' ),
'0.6'
);
return false;
}
$args['strategy'] = $strategy;

if ( empty( $route ) || ! is_string( $route ) ) {
_doing_it_wrong(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ public function serve( WP_Service_Worker_Scripts $scripts ) {
$route = $config['route'];
unset( $config['route'] );

$scripts->caching_routes()->register( $route, $config );
$strategy = $config['strategy'];
unset( $config['strategy'] );

$scripts->caching_routes()->register( $route, $strategy, $config );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ public function serve( WP_Service_Worker_Scripts $scripts ) {
$route = $config['route'];
unset( $config['route'] );

$scripts->caching_routes()->register( $route, $config );
$strategy = $config['strategy'];
unset( $config['strategy'] );

$scripts->caching_routes()->register( $route, $strategy, $config );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @since 0.2
*/
class WP_Service_Worker_Precaching_Routes_Component implements WP_Service_Worker_Component, WP_Service_Worker_Registry_Aware {
class WP_Service_Worker_Precaching_Routes_Component implements WP_Service_Worker_Component {

/**
* Precaching routes registry.
Expand All @@ -23,12 +23,12 @@ class WP_Service_Worker_Precaching_Routes_Component implements WP_Service_Worker
/**
* Constructor.
*
* Instantiates the registry.
*
* @since 0.2
*
* @param WP_Service_Worker_Precaching_Routes $registry Registry.
*/
public function __construct() {
$this->registry = new WP_Service_Worker_Precaching_Routes();
public function __construct( WP_Service_Worker_Precaching_Routes $registry ) {
$this->registry = $registry;
}

/**
Expand Down Expand Up @@ -64,15 +64,6 @@ public function get_priority() {
return -99999;
}

/**
* Gets the registry.
*
* @return WP_Service_Worker_Precaching_Routes Precaching routes registry instance.
*/
public function get_registry() {
return $this->registry;
}

/**
* Gets the script that registers the precaching routes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @since 0.2
*/
class WP_Service_Worker_Precaching_Routes implements WP_Service_Worker_Registry {
class WP_Service_Worker_Precaching_Routes {

/**
* Registered caching routes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function serve( WP_Service_Worker_Scripts $scripts ) {
'strategy' => WP_Service_Worker_Caching_Routes::STRATEGY_NETWORK_FIRST,
'cache_name' => self::CACHE_NAME,
'expiration' => array(
'maxEntries' => 34,
'max_entries' => 34,
),
);

Expand Down Expand Up @@ -83,7 +83,10 @@ public function serve( WP_Service_Worker_Scripts $scripts ) {
$route = $config['route'];
unset( $config['route'] );

$scripts->caching_routes()->register( $route, $config );
$strategy = $config['strategy'];
unset( $config['strategy'] );

$scripts->caching_routes()->register( $route, $strategy, $config );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ static function ( $image_extension ) {
$route = $config['route'];
unset( $config['route'] );

$scripts->caching_routes()->register( $route, $config );
$strategy = $config['strategy'];
unset( $config['strategy'] );

$scripts->caching_routes()->register( $route, $strategy, $config );
}

/**
Expand Down
Loading

0 comments on commit 5f1c00a

Please sign in to comment.