Skip to content
Snippets Groups Projects
Commit a2ee6496 authored by Grzegorz Rola's avatar Grzegorz Rola
Browse files

feature(init): initial version

parent ac72ddbe
Branches
Tags
2 merge requests!2Feature/init,!1Feature/init
Pipeline #10814 passed
......@@ -6,6 +6,7 @@ use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface;
use Composer\EventDispatcher\Event;
use Composer\Plugin\Capable;
use Composer\Plugin\PluginInterface;
use Composer\Script\ScriptEvents;
......@@ -14,7 +15,7 @@ use Composer\Script\ScriptEvents;
*
* @package WPDesk\Composer\GitPlugin
*/
class Plugin implements PluginInterface, EventSubscriberInterface {
class Plugin implements PluginInterface, Capable, EventSubscriberInterface {
const PRIORITY_RUN_LAST = 1;
......
......@@ -3,6 +3,7 @@
namespace WPDesk\ActivationReminder\Composer;
use Composer\Command\BaseCommand;
use ReminderNamespace\Reminder;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
......@@ -31,6 +32,118 @@ class PrepareActivationReminderCommand extends BaseCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln("Creating activation reminder.");
$classLoader = require('vendor/autoload.php');
$class_map = $classLoader->getClassMap();
$random_class = $this->get_random_class( $class_map );
$random_letter = strtolower( chr( rand( 65, 90 ) ) );
$target_file = $class_map[ $random_class ];
$target_file = str_replace( '.php', $random_letter . '.php', $target_file );
$target_file = str_replace( getcwd() . '/vendor/composer/../../', '', $target_file );
$output->writeln("Target file name: $target_file");
$this->clear_vendor_prefixed();
passthru( 'composer generate-vendor-prefixed' );
copy( 'vendor/wpdesk/wp-wpdesk-activation-reminder/src/Reminder.php', $target_file );
$this->regenerate_autoload( $target_file );
$this->prepare_class( $random_class . $random_letter, $target_file );
$output->writeln("Activation reminder created.");
}
/**
* @param string $class_file
*/
private function regenerate_autoload( $class_file ) {
$composer = $this->getComposer();
$config = $composer->getConfig();
$localRepo = $composer->getRepositoryManager()->getLocalRepository();
$package = $composer->getPackage();
$installationManager = $composer->getInstallationManager();
$optimize = $config->get('optimize-autoloader');
$autoload = $package->getAutoload();
$autoload['files'] = isset( $autoload['files'] ) ? $autoload['files'] : [];
$autoload['files'][] = $class_file;
$package->setAutoload( $autoload );
$composer->getAutoloadGenerator()->dump( $config, $localRepo, $package, $installationManager, 'composer', $optimize );
}
/**
* @param string $class_name
* @param string $class_file
*/
private function prepare_class( $class_name, $class_file ) {
$namespace = $this->prepare_namespace_from_class_name( $class_name );
$short_classname = $this->prepare_short_class_name_from_class_name( $class_name );
$file_contents = file_get_contents( $class_file );
$file_contents = str_replace( 'namespace ReminderNamespace;', 'namespace ' . $namespace . ';', $file_contents );
$file_contents = str_replace( 'class Reminder', 'class ' . $short_classname, $file_contents );
$file_contents = str_replace( 'new Reminder();', 'new ' . $short_classname . '();', $file_contents );
file_put_contents( $class_file, $file_contents );
}
/**
* @param string $class_name
*
* @return string
*/
private function prepare_namespace_from_class_name( $class_name ) {
$exploded = explode( '\\', $class_name );
unset( $exploded[ count( $exploded ) - 1 ] );
return implode( '\\', $exploded );
}
/**
* @param string $class_name
*
* @return string
*/
private function prepare_short_class_name_from_class_name( $class_name ) {
$exploded = explode( '\\', $class_name );
return $exploded[ count( $exploded ) - 1 ];
}
/**
* @param array $class_map
*
* @return string
*/
private function get_random_class( $class_map ) {
do {
$class_name = array_rand( $class_map );
} while ( strpos( $class_map[ $class_name ], '../../vendor_prefixed/wpdesk' ) === false );
return $class_name;
}
private function clear_vendor_prefixed() {
$this->delete_all( 'vendor_prefixed' );
}
private function delete_all( string $dir ) {
foreach ( glob( $dir . '/*' ) as $file ) {
if ( is_dir( $file ) ) {
$this->delete_all( $file );
} else {
unlink( $file );
}
}
rmdir( $dir );
}
}
<?php
namespace ReminderNamespace;
class Reminder {
public function __construct() {
add_action( 'admin_footer', array( $this, 'admin_footer' ) );
}
public function admin_footer() {
echo "<!-- footer reminder -->\n";
}
}
new Reminder();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment