Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
CrochetFeve0251 committed Feb 24, 2024
0 parents commit 4234a4c
Show file tree
Hide file tree
Showing 19 changed files with 615 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor
.idea
composer.lock
33 changes: 33 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "wp-launchpad/renderer-take-off",
"description": "Initialisation library for the renderer library from the Launchpad framework",
"require": {
"wp-launchpad/cli": "^1.0.1",
"wp-launchpad/renderer": "^1.0"
},
"autoload": {
"psr-4": {
"LaunchpadRendererTakeOff\\": "inc/"
}
},
"require-dev": {
"phpunit/phpunit": "^7.5 || ^8 || ^9",
"wp-media/phpunit": "^3.0"
},
"autoload-dev": {
"psr-4": {
"LaunchpadRendererTakeOff\\Tests\\": "tests/"
}
},
"extra": {
"launchpad": {
"provider" : "LaunchpadRendererTakeOff\\ServiceProvider",
"command": "renderer:install",
"install": true,
"clean": true,
"libraries": {
"wp-launchpad/renderer": "^1.0"
}
}
}
}
107 changes: 107 additions & 0 deletions inc/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace LaunchpadRendererTakeOff\Commands;

use LaunchpadCLI\Commands\Command;
use LaunchpadRendererTakeOff\Services\ConfigsManager;
use League\Flysystem\Filesystem;

class InstallCommand extends Command
{

/**
* @var ConfigsManager
*/
protected $config_manager;

/**
* @var Filesystem
*/
protected $project_filesystem;

/**
* Instantiate the class.
*
* @param Filesystem $configs_manager
*/
public function __construct(Filesystem $project_filesystem, ConfigsManager $config_manager)
{
parent::__construct('renderer:install', 'Install a renderer');

$this->project_filesystem = $project_filesystem;
$this->config_manager = $config_manager;

$this
// Usage examples:
->usage(
// append details or explanation of given example with ` ## ` so they will be uniformly aligned when shown
'<bold> $0 renderer:install</end> ## Install a renderer<eol/>'
);
}


public function execute() {

$this->create_template_folder();
$this->config_manager->set_up_provider();
}

public function create_template_folder() {

$params_path = 'configs/parameters.php';

$this->create_template_dir();

if ( ! $this->project_filesystem->has( $params_path ) ) {
return;
}

$content = $this->project_filesystem->read( $params_path );

$parameters = [
'template_path' => '$plugin_launcher_path . \'templates/\'',
'root_directory' => ' WP_CONTENT_DIR . \'/cache/\'',
'renderer_cache_enabled' => 'false',
'renderer_caching_solution' => '[]',
];

foreach ($parameters as $name => $value) {
$content = $this->add_parameter($content, $name, $value);
}

$this->project_filesystem->update($params_path, $content);
}

protected function create_template_dir()
{
$template_dir = 'templates';
if ( $this->project_filesystem->has($template_dir) ) {
return;
}

$this->project_filesystem->createDir($template_dir);
}

protected function add_parameter(string $content, string $name, string $value)
{
if(preg_match('/[\'"]' . $name . '[\'"]\s=>/', $content)) {
return $content;
}

if(! preg_match('/(?<array>return\s\[(?:[^[\]]+|(?R))*\]\s*;\s*$)/', $content, $results)) {
return $content;
}

$array = $results['array'];

if(! preg_match('/(?<indents>\h*)[\'"].*[\'"]\s=>/', $array, $results)) {
return $content;
}

$indents = $results['indents'];
$new_content = "$indents'$name' => $value,\n";
$new_content .= "];\n";

return preg_replace('/]\s*;\s*$/', $new_content, $content);
}
}
47 changes: 47 additions & 0 deletions inc/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
namespace LaunchpadRendererTakeOff;

use LaunchpadCLI\App;
use LaunchpadCLI\Entities\Configurations;
use LaunchpadCLI\ServiceProviders\ServiceProviderInterface;
use LaunchpadRendererTakeOff\Commands\InstallCommand;
use LaunchpadRendererTakeOff\Services\ConfigsManager;
use League\Flysystem\Filesystem;

class ServiceProvider implements ServiceProviderInterface
{
/**
* Configuration from the project.
*
* @var Configurations
*/
protected $configs;

/**
* Interacts with the filesystem.
*
* @var Filesystem
*/
protected $filesystem;

/**
* Instantiate the class.
*
* @param Configurations $configs configuration from the project.
* @param Filesystem $filesystem Interacts with the filesystem.
* @param string $app_dir base directory from the cli.
*/
public function __construct(Configurations $configs, Filesystem $filesystem, string $app_dir)
{
$this->configs = $configs;
$this->filesystem = $filesystem;
}


public function attach_commands(App $app): App
{
$config_manager = new ConfigsManager($this->filesystem, $this->configs);
$app->add(new InstallCommand($this->filesystem, $config_manager));
return $app;
}
}
53 changes: 53 additions & 0 deletions inc/Services/ConfigsManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace LaunchpadRendererTakeOff\Services;

use LaunchpadCLI\Entities\Configurations;
use LaunchpadRenderer\ServiceProvider;
use League\Flysystem\Filesystem;

class ConfigsManager
{
/**
* @var Filesystem
*/
protected $filesystem;

/**
* @var Configurations
*/
protected $configurations;

/**
* @param Filesystem $filesystem
* @param Configurations $configurations
*/
public function __construct(Filesystem $filesystem, Configurations $configurations)
{
$this->filesystem = $filesystem;
$this->configurations = $configurations;
}


public function set_up_provider() {
$base_namespace = $this->configurations->getBaseNamespace();
$providers_path = 'configs/providers.php';

if ( ! $this->filesystem->has( $providers_path ) ) {
return;
}

$content = $this->filesystem->read( $providers_path );

if(! preg_match('/(?<array>return\s\[(?<content>(?:[^[\]]+|(?R))*)\]\s*;\s*$)/', $content, $results)) {
return;
}

$result_content = $results['content'];
$result_content = "\n \\$base_namespace" . "Dependencies\\" . ServiceProvider::class . "::class," . $result_content;
$content = str_replace($results['content'], $result_content, $content);

$this->filesystem->update($providers_path, $content);
}

}
10 changes: 10 additions & 0 deletions tests/Fixtures/files/bin/generator
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/php
<?php

use LaunchpadCLI\AppBuilder;

require_once __DIR__ . '/../vendor/autoload.php';

AppBuilder::init(__DIR__ . '/../', [
\LaunchpadRendererTakeOff\ServiceProvider::class,
]);
113 changes: 113 additions & 0 deletions tests/Fixtures/files/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"name": "crochetfeve0251/rocket-launcher",
"description": "Template for a psr4 compatible plugin",
"keywords": [
"wordpress"
],
"license": "GPL-2.0-or-later",
"authors": [
{
"name": "CrochetFeve0251"
}
],
"type": "project",
"config": {
"sort-packages": true,
"preferred-install": {
"wp-media/phpunit": "source"
},
"process-timeout": 0,
"allow-plugins": {
"composer/installers": true,
"mnsami/composer-custom-directory-installer": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"repositories": [
{
"type": "composer",
"url": "https://wpackagist.org"
}
],
"require": {
"php": ">=7.0",
"berlindb/core": "^2.0",
"composer/installers": "^1.0 || ^2.0",
"monolog/monolog": "^1.0"
},
"require-dev": {
"php": "^7 || ^8",
"brain/monkey": "^2.0",
"coenjacobs/mozart": "^0.7",
"crochetfeve0251/rocket-launcher-builder": "^0.0.5",
"crochetfeve0251/rocket-launcher-take-off": "^0.0.2",
"wp-launchpad/front-take-off": "^0.0.1",
"crochetfeve0251/rocket-launcher-core": "^0.0.1",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
"league/container": "^3.3",
"mnsami/composer-custom-directory-installer": "^2.0",
"phpcompatibility/phpcompatibility-wp": "^2.0",
"phpstan/phpstan": "^0.12",
"phpunit/phpunit": "^7.5 || ^8 || ^9",
"psr/container": "1.0.0",
"roave/security-advisories": "dev-master",
"szepeviktor/phpstan-wordpress": "^0.7.0",
"wp-coding-standards/wpcs": "^2",
"wp-media/phpunit": "^3.0"
},
"autoload": {
"classmap": [
"inc/classes"
],
"psr-4": {
"RocketLauncher\\": "inc/"
}
},
"autoload-dev": {
"psr-4": {
"RocketLauncher\\Tests\\": "tests/"
}
},
"extra": {
"installer-paths": {
"vendor/{$vendor}/{$name}/": ["type:wordpress-plugin"]
},
"mozart": {
"dep_namespace": "RocketLauncher\\Dependencies\\",
"dep_directory": "/inc/Dependencies/",
"classmap_directory": "/inc/classes/dependencies/",
"classmap_prefix": "RocketLauncher",
"packages": [
"berlindb/core",
"league/container",
"crochetfeve0251/rocket-launcher-core"
]
}
},
"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",
"test-integration-adminonly": "\"vendor/bin/phpunit\" --testsuite integration --colors=always --configuration tests/Integration/phpunit.xml.dist --group AdminOnly",
"run-tests": [
"@test-unit",
"@test-integration",
"@test-integration-adminonly"
],
"run-stan": "vendor/bin/phpstan analyze --memory-limit=2G --no-progress",
"install-codestandards": "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin::run",
"phpcs": "phpcs --basepath=.",
"phpcs-changed": "./bin/phpcs-changed.sh",
"phpcs:fix": "phpcbf",
"post-install-cmd": [
"\"vendor/bin/mozart\" compose",
"composer dump-autoload"
],
"post-update-cmd": [
"\"vendor/bin/mozart\" compose",
"composer dump-autoload"
],
"code-coverage": "\"vendor/bin/phpunit\" --testsuite unit --colors=always --configuration tests/Unit/phpunit.xml.dist --coverage-clover=tests/report/coverage.clover"
},
"minimum-stability": "stable",
"prefer-stable": true
}
16 changes: 16 additions & 0 deletions tests/Fixtures/files/configs/parameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

<?php

$plugin_name = 'Rocket launcher';

$plugin_launcher_path = dirname(__DIR__) . '/';

return [
'plugin_name' => $plugin_name,
'plugin_slug' => sanitize_key( $plugin_name ),
'plugin_version' => '1.0.0',
'plugin_launcher_file' => $plugin_launcher_path . '/' . basename($plugin_launcher_path) . '.php',
'plugin_launcher_path' => $plugin_launcher_path,
'plugin_inc_path' => realpath( $plugin_launcher_path . 'inc/' ) . '/',
'prefix' => 'rocket_launcher',
];
Loading

0 comments on commit 4234a4c

Please sign in to comment.