Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
- wp-launchpad/core
- wp-launchpad/dispatcher
- wp-launchpad/uninstaller
- wp-launchpad/uninstaller-take-off
- wp-launchpad/updater
- wp-launchpad/cli
- wp-launchpad/bud-assets
Expand Down
3 changes: 2 additions & 1 deletion packages/core/tests/Integration/inc/boot/deactivate.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ public function testShouldDoAsExpected($config, $expected)
do_action("deactivate_{$activate_plugin_path}");

$this->assertFalse(get_option('demo_option', false), "demo_option should be unregistered");
$this->assertFalse(get_option('demo_option_2', false), "demo_option_2 should be unregistered"); }
$this->assertFalse(get_option('demo_option_2', false), "demo_option_2 should be unregistered");
}
}
2 changes: 1 addition & 1 deletion packages/uninstaller-take-off/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"install": true,
"clean": true,
"libraries": {
"wp-launchpad/uninstaller": "^0.0"
"wp-launchpad/uninstaller": "^3.1"
}
}
},
Expand Down
10 changes: 9 additions & 1 deletion packages/uninstaller-take-off/templates/uninstall.php.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

defined( 'WP_UNINSTALL_PLUGIN' ) || exit;

use {{ base_namespace }}Dependencies\LaunchpadDispatcher\Dispatcher;
use {{ base_namespace }}Dependencies\LaunchpadUninstaller\Uninstall\Uninstall;
use {{ base_namespace }}Dependencies\League\Container\Container;

Expand All @@ -13,7 +14,14 @@ require_once $plugin_root_dir . '/vendor/autoload.php';
$params = require_once $plugin_root_dir . '/configs/parameters.php';
$providers = require_once $plugin_root_dir . '/configs/providers.php';

Uninstall::set_container(new Container());
$container = new Container();
if( key_exists('autowiring', $params) && $params['autowiring']) {
$reflection_container = new \LaunchpadCore\Container\Autowiring\Container();
$container->delegate( $reflection_container );
}

Uninstall::set_container($container);
Uninstall::set_dispatcher(new Dispatcher());
Uninstall::set_params($params);
Uninstall::set_providers($providers);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@
'exists' => true,
'content' => file_get_contents(__DIR__ . '/files/configs/providers.php')
],
'composer.json' => [
'exists' => true,
'content' => file_get_contents(__DIR__ . '/files/composer.json')
],
'bin/generator' => [
'exists' => true,
'content' => file_get_contents(__DIR__ . '/files/bin/generator')
],
'uninstall.php' => [
'exists' => true,
'content' => file_get_contents(__DIR__ . '/files/uninstall.php')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

defined( 'WP_UNINSTALL_PLUGIN' ) || exit;

use RocketLauncher\Dependencies\RocketLauncherUninstaller\Uninstall\Uninstall;
use RocketLauncher\Dependencies\LaunchpadDispatcher\Dispatcher;
use RocketLauncher\Dependencies\LaunchpadUninstaller\Uninstall\Uninstall;
use RocketLauncher\Dependencies\League\Container\Container;

$plugin_root_dir = dirname( __FILE__ );
Expand All @@ -13,7 +14,14 @@
$params = require_once $plugin_root_dir . '/configs/parameters.php';
$providers = require_once $plugin_root_dir . '/configs/providers.php';

Uninstall::set_container(new Container());
$container = new Container();
if( key_exists('autowiring', $params) && $params['autowiring']) {
$reflection_container = new \LaunchpadCore\Container\Autowiring\Container();
$container->delegate( $reflection_container );
}

Uninstall::set_container($container);
Uninstall::set_dispatcher(new Dispatcher());
Uninstall::set_params($params);
Uninstall::set_providers($providers);

Expand Down
4 changes: 2 additions & 2 deletions packages/uninstaller/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"wp-media/phpunit": "^3.0"
},
"scripts": {
"test-unit": "\"vendor/bin/phpunit\" --testsuite unit --colors=always --configuration tests/Unit/phpunit.xml.dist",
"test-integration": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --exclude-group AdminOnly",
"run-tests": [
"@test-unit"
"@test-integration"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace LaunchpadUninstaller\Uninstall;

use LaunchpadCore\Container\Registration\DeactivatorRegistration;
use LaunchpadCore\Container\Registration\Registration;

trait HasUninstallerServiceProviderTrait {
/**
* Returns list of uninstallers.
*
* @return string[]
*/
public function get_uninstallers(): array {
$this->load();

$uninstallers = [];

foreach ( $this->get_services_to_load() as $registration ) {
if ( ! $registration instanceof UninstallerRegistration ) {
continue;
}

$uninstallers [] = $registration->get_id();
}

return $uninstallers;
}

/**
* Register uninstaller.
*
* @param string $classname Classname from the uninstaller.
* @return UninstallerRegistration
*/
public function register_uninstaller( string $classname ): UninstallerRegistration {
$registration = new UninstallerRegistration( $classname );

$this->add_service_to_load( $registration );

return $registration;
}

/**
* Loads definitions.
*
* @return void
*/
abstract protected function load();

/**
* Get the service to load.
*
* @return Registration[]
*/
abstract protected function get_services_to_load(): array;

/**
* Add to the list of service to load.
*
* @param Registration $registration Registration from the service to add.
* @return void
*/
abstract protected function add_service_to_load( Registration $registration ): void;
}
72 changes: 62 additions & 10 deletions packages/uninstaller/inc/Uninstall/Uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
namespace LaunchpadUninstaller\Uninstall;

use LaunchpadCore\Container\HasInflectorInterface;
use LaunchpadCore\Container\PrefixAwareInterface;
use LaunchpadCore\Dispatcher\DispatcherAwareInterface;
use LaunchpadDispatcher\Dispatcher;
use LaunchpadUninstaller\Uninstall\Wrapper\UninstallerWrapper;
use League\Container\Argument\Literal\StringArgument;
use Psr\Container\ContainerInterface;

class Uninstall
Expand All @@ -12,6 +17,13 @@ class Uninstall

protected static $container;

/**
* Hook dispatcher.
*
* @var Dispatcher
*/
protected static $dispatcher;

public static function set_providers(array $providers) {
self::$providers = $providers;
}
Expand All @@ -24,14 +36,53 @@ public static function set_container(ContainerInterface $container) {
self::$container = $container;
}

/**
* Set hook dispatcher.
*
* @param Dispatcher $dispatcher Hook dispatcher.
* @return void
*/
public static function set_dispatcher( Dispatcher $dispatcher ): void {
self::$dispatcher = $dispatcher;
}

public static function uninstall_plugin() {
foreach (self::$params as $key => $value) {
self::$container->add( $key, $value);
}
$providers = array_filter(self::$providers, function ($provider) {
if(is_string($provider)) {
$provider = new $provider();
}
foreach ( self::$params as $key => $value ) {
if ( is_string( $value ) && ! class_exists( $value ) ) {
$value = new StringArgument( $value );
}

self::$container->addShared( $key, $value );
}


self::$container->addShared( 'dispatcher', self::$dispatcher );

self::$container->inflector( PrefixAwareInterface::class )->invokeMethod( 'set_prefix', [ key_exists( 'prefix', self::$params ) ? self::$params['prefix'] : '' ] );
self::$container->inflector( DispatcherAwareInterface::class )->invokeMethod( 'set_dispatcher', [ self::$container->get( 'dispatcher' ) ] );

$providers = array_map(
function ( $provider ) {
if ( is_string( $provider ) ) {
return new $provider();
}
return $provider;
},
self::$providers
);

foreach ( $providers as $provider ) {
self::$container->addServiceProvider( $provider );
}

foreach ( $providers as $service_provider ) {
if ( ! $service_provider instanceof HasInflectorInterface ) {
continue;
}
$service_provider->register_inflectors();
}

$providers = array_filter($providers, function ($provider) {

if(! $provider instanceof UninstallServiceProviderInterface) {
return false;
Expand All @@ -51,6 +102,8 @@ public static function uninstall_plugin() {
self::$container->addServiceProvider($provider);
}

$wrapper = new UninstallerWrapper();

foreach ( $providers as $service_provider ) {
if( ! $service_provider instanceof HasInflectorInterface ) {
continue;
Expand All @@ -65,9 +118,8 @@ public static function uninstall_plugin() {

foreach ( $provider->get_uninstallers() as $uninstaller ) {
$uninstaller_instance = self::$container->get( $uninstaller );
if(! $uninstaller_instance instanceof UninstallerInterface) {
continue;
}
$uninstaller_instance = $wrapper->wrap($uninstaller_instance);

$uninstaller_instance->uninstall();
}
}
Expand Down
11 changes: 11 additions & 0 deletions packages/uninstaller/inc/Uninstall/UninstallerRegistration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace LaunchpadUninstaller\Uninstall;

use LaunchpadCore\Container\Registration\Autowiring\AutowireAwareInterface;
use LaunchpadCore\Container\Registration\Autowiring\AutowireAwareTrait;
use LaunchpadCore\Container\Registration\Registration;

class UninstallerRegistration extends Registration implements AutowireAwareInterface {
use AutowireAwareTrait;
}
42 changes: 42 additions & 0 deletions packages/uninstaller/inc/Uninstall/Wrapper/UninstallerProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace LaunchpadUninstaller\Uninstall\Wrapper;

use LaunchpadUninstaller\Uninstall\UninstallerInterface;

class UninstallerProxy implements UninstallerInterface {

/**
* List of method to call.
*
* @var string[]
*/
protected $uninstall_methods = [];

/**
* Any class uninstaller.
*
* @var object
*/
protected $instance;

/**
* Instantiate the proxy.
*
* @param object $instance Any class uninstaller.
* @param array $uninstall_methods List of method to call.
*/
public function __construct( $instance, array $uninstall_methods ) {
$this->instance = $instance;
$this->uninstall_methods = $uninstall_methods;
}

/**
* @inheritDoc
*/
public function uninstall() {
foreach ( $this->uninstall_methods as $method ) {
$this->instance->{$method}();
}
}
}
44 changes: 44 additions & 0 deletions packages/uninstaller/inc/Uninstall/Wrapper/UninstallerWrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace LaunchpadUninstaller\Uninstall\Wrapper;

use LaunchpadUninstaller\Uninstall\UninstallerInterface;
use ReflectionClass;

class UninstallerWrapper {
/**
* Wrap an uninstaller will the common interface for uninstallers.
*
* @param object $instance Any class uninstaller.
*
* @return UninstallerInterface
*/
public function wrap( $instance ): UninstallerInterface {
if ( $instance instanceof UninstallerInterface ) {
return $instance;
}

$methods = get_class_methods( $instance );
$reflection_class = new ReflectionClass( get_class( $instance ) );
$uninstall_methods = [];

foreach ( $methods as $method ) {
$method_reflection = $reflection_class->getMethod( $method );
$doc_comment = $method_reflection->getDocComment();
if ( ! $doc_comment ) {
continue;
}
$pattern = '#@uninstall#';

$matched = preg_match_all( $pattern, $doc_comment, $matches, PREG_PATTERN_ORDER );

if ( ! $matched ) {
continue;
}

$uninstall_methods[] = $method;
}

return new UninstallerProxy( $instance, $uninstall_methods );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [
'prefix' => 'demo_',
'translation_key' => 'demo',
'is_mu_plugin' => false,
'key_param' => 'key_param',
'cache' => \LaunchpadUninstaller\Tests\Fixtures\inc\boot\autowiring\inc\Cache::class,
'autowiring' => true
];
Loading
Loading