Skip to content

Commit

Permalink
push first prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
wordpressfan committed Jan 12, 2024
1 parent 8b64400 commit e86dc8e
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 11 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"brain/monkey": "^2.0",
"coenjacobs/mozart": "^0.7",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
"friendsofphp/proxy-manager-lts": "^1.0",
"league/container": "^3.3",
"mikey179/vfsstream": "1.6.11",
"mnsami/composer-custom-directory-installer": "^2.0",
Expand All @@ -62,6 +63,7 @@
"woocommerce/action-scheduler": "^3.4",
"wp-coding-standards/wpcs": "^3",
"wp-media/background-processing": "^1.3",
"wp-media/monolog": "^0.0",
"wp-media/phpunit": "^3",
"wp-media/rocket-lazyload-common": "^3.0.11",
"wp-media/wp-imagify-partner": "^1.0",
Expand All @@ -72,8 +74,7 @@
"wpackagist-plugin/simple-custom-css": "^4.0.3",
"wpackagist-plugin/spinupwp": "^1.1",
"wpackagist-plugin/woocommerce": "^7",
"wpackagist-plugin/wp-smushit": "^3",
"wp-media/monolog": "^0.0"
"wpackagist-plugin/wp-smushit": "^3"
},
"autoload": {
"classmap": [
Expand Down
5 changes: 3 additions & 2 deletions inc/Dependencies/League/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,11 @@ public function addServiceProvider($provider) : self
/**
* {@inheritdoc}
*/
public function get($id, bool $new = false)
public function get($id, bool $new = false, bool $lazy = false)
{
//error_log( 'ASA Get: ' . $id );
if ($this->definitions->has($id)) {
$resolved = $this->definitions->resolve($id, $new);
$resolved = $this->definitions->resolve($id, $new, $lazy);
return $this->inflectors->inflect($resolved);
}

Expand Down
35 changes: 30 additions & 5 deletions inc/Dependencies/League/Container/Definition/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function addMethodCalls(array $methods = []) : DefinitionInterface
/**
* {@inheritdoc}
*/
public function resolve(bool $new = false)
public function resolve(bool $new = false, bool $lazy = false)
{
$concrete = $this->concrete;

Expand All @@ -209,7 +209,7 @@ public function resolve(bool $new = false)
}

if (is_string($concrete) && class_exists($concrete)) {
$concrete = $this->resolveClass($concrete);
$concrete = $this->resolveClass($concrete, $lazy);
}

if (is_object($concrete)) {
Expand Down Expand Up @@ -248,14 +248,39 @@ protected function resolveCallable(callable $concrete)
*
* @throws ReflectionException
*/
protected function resolveClass(string $concrete)
protected function resolveClass(string $concrete, bool $lazy = false)
{
//error_log($concrete);
$resolved = $this->resolveArguments($this->arguments);
$reflection = new ReflectionClass($concrete);
if ( ! $lazy ) {
return $this->createInstance($concrete, $resolved);
}

return $reflection->newInstanceArgs($resolved);
// for lazyload
return $this->createProxy( $concrete, $resolved );
}

private function createInstance( $concrete, $resolved )
{
$reflection = new ReflectionClass($concrete);

return $reflection->newInstanceArgs($resolved);
}

private function createProxy( $concrete, $resolved )
{
$factory = new \ProxyManager\Factory\LazyLoadingValueHolderFactory();
return $factory->createProxy(
$concrete,
function (& $wrappedObject, $proxy, $method, $params, & $initializer) use ($concrete, $resolved) {
$wrappedObject = $this->createInstance($concrete, $resolved);
$initializer = null; // turning off further lazy initialization

return true;
}
);
}

/**
* Invoke methods on resolved instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ public function getDefinition(string $id) : DefinitionInterface
/**
* {@inheritdoc}
*/
public function resolve(string $id, bool $new = false)
public function resolve(string $id, bool $new = false, bool $lazy = false)
{
return $this->getDefinition($id)->resolve($new);
//error_log( var_export( gettype( $this->getDefinition($id) ), true ) );
return $this->getDefinition($id)->resolve($new, $lazy);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions inc/Engine/TestLazyload/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);

namespace WP_Rocket\Engine\TestLazyload;

use WP_Rocket\Dependencies\League\Container\ServiceProvider\AbstractServiceProvider;

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 = [
'test_lazyload_subscriber',
'test_lazyload_class',
];

/**
* Registers the subscribers in the container
*
* @return void
*/
public function register() {
$this->getContainer()
->share( 'test_lazyload_class', TestClass::class );

$this->getContainer()
->share( 'test_lazyload_subscriber', Subscriber::class )
->addArgument( $this->getContainer()->get( 'test_lazyload_class', true, true ) );

Check notice on line 35 in inc/Engine/TestLazyload/ServiceProvider.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/TestLazyload/ServiceProvider.php#L35

Functions must not contain multiple empty lines in a row; found 2 empty lines

}

Check notice on line 37 in inc/Engine/TestLazyload/ServiceProvider.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/TestLazyload/ServiceProvider.php#L37

Function closing brace must go on the next line following the body; found 2 blank lines before brace
}
25 changes: 25 additions & 0 deletions inc/Engine/TestLazyload/Subscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace WP_Rocket\Engine\TestLazyload;

use WP_Rocket\Event_Management\Subscriber_Interface;

class Subscriber implements Subscriber_Interface {

private $test_class;

Check notice on line 8 in inc/Engine/TestLazyload/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/TestLazyload/Subscriber.php#L8

Missing member variable doc comment

public function __construct( TestClass $test_class ) {

Check notice on line 10 in inc/Engine/TestLazyload/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/TestLazyload/Subscriber.php#L10

Missing doc comment for function __construct()
$this->test_class = $test_class;
}

public static function get_subscribed_events() {

Check notice on line 14 in inc/Engine/TestLazyload/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/TestLazyload/Subscriber.php#L14

Missing doc comment for function get_subscribed_events()
return [
'admin_init' => 'log_message',
];
}

public function log_message()

Check notice on line 20 in inc/Engine/TestLazyload/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/TestLazyload/Subscriber.php#L20

Missing doc comment for function log_message()
{

Check notice on line 21 in inc/Engine/TestLazyload/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/TestLazyload/Subscriber.php#L21

Opening brace should be on the same line as the declaration
$this->test_class->test1();
}

}

Check notice on line 25 in inc/Engine/TestLazyload/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/TestLazyload/Subscriber.php#L25

The closing brace for the class must go on the next line after the body
18 changes: 18 additions & 0 deletions inc/Engine/TestLazyload/TestClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace WP_Rocket\Engine\TestLazyload;

class TestClass {

public function __construct() {

Check notice on line 6 in inc/Engine/TestLazyload/TestClass.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/TestLazyload/TestClass.php#L6

Missing doc comment for function __construct()
error_log( 'log message from TestClass::__construct' );

Check notice on line 7 in inc/Engine/TestLazyload/TestClass.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/TestLazyload/TestClass.php#L7

error_log() found. Debug code should not normally be used in production.
error_log( var_export( wp_debug_backtrace_summary(), true ) );

Check notice on line 8 in inc/Engine/TestLazyload/TestClass.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/TestLazyload/TestClass.php#L8

error_log() found. Debug code should not normally be used in production.
error_log( '-------------------------' );

Check notice on line 9 in inc/Engine/TestLazyload/TestClass.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/TestLazyload/TestClass.php#L9

error_log() found. Debug code should not normally be used in production.
}

public function test1() {

Check notice on line 12 in inc/Engine/TestLazyload/TestClass.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/TestLazyload/TestClass.php#L12

Missing doc comment for function test1()
error_log( 'log message from TestClass::test1' );

Check notice on line 13 in inc/Engine/TestLazyload/TestClass.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/TestLazyload/TestClass.php#L13

error_log() found. Debug code should not normally be used in production.
error_log( var_export( wp_debug_backtrace_summary(), true ) );
error_log( '-------------------------' );
}

}

Check notice on line 18 in inc/Engine/TestLazyload/TestClass.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/TestLazyload/TestClass.php#L18

The closing brace for the class must go on the next line after the body
2 changes: 2 additions & 0 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ private function init_common_subscribers() {
$this->container->addServiceProvider( APIServiceProvider::class );
$this->container->addServiceProvider( CommmonExtractCSSServiceProvider::class );
$this->container->addServiceProvider( LazyloadCSSServiceProvider::class );
$this->container->addServiceProvider( \WP_Rocket\Engine\TestLazyload\ServiceProvider::class );

$common_subscribers = [
'license_subscriber',
Expand Down Expand Up @@ -382,6 +383,7 @@ private function init_common_subscribers() {
'shoptimizer',
'weglot',
'contactform7',
'test_lazyload_subscriber',
];

$host_type = HostResolver::get_host_service();
Expand Down

0 comments on commit e86dc8e

Please sign in to comment.