Skip to content
Snippets Groups Projects
Commit 1e4a010f authored by jurgenhaas's avatar jurgenhaas
Browse files

Coding standards

parent b2e00a77
No related branches found
No related tags found
No related merge requests found
Pipeline #
...@@ -30,6 +30,14 @@ ...@@ -30,6 +30,14 @@
"lakedrops/dotenv": "^0.1", "lakedrops/dotenv": "^0.1",
"twig/twig": "^1.23.1" "twig/twig": "^1.23.1"
}, },
"require-dev": {
"composer/composer": "^1.5.0",
"dealerdirect/phpcodesniffer-composer-installer": "^0.4.3",
"drupal/coder": "^8.2",
"phpunit/phpunit": "^4.8.0"
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"LakeDrops\\Docker4Drupal\\": "src/" "LakeDrops\\Docker4Drupal\\": "src/"
...@@ -37,9 +45,5 @@ ...@@ -37,9 +45,5 @@
}, },
"extra": { "extra": {
"class": "LakeDrops\\Docker4Drupal\\Plugin" "class": "LakeDrops\\Docker4Drupal\\Plugin"
},
"require-dev": {
"composer/composer": "dev-master",
"phpunit/phpunit": "^4.4.0"
} }
} }
...@@ -2,37 +2,49 @@ ...@@ -2,37 +2,49 @@
namespace LakeDrops\Docker4Drupal; namespace LakeDrops\Docker4Drupal;
use Composer\Package\PackageInterface;
use Composer\Composer; use Composer\Composer;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\Script\Event as ScriptEvent; use Composer\Script\Event;
use LakeDrops\Component\Dotenv\Dotenv; use LakeDrops\Component\Dotenv\Dotenv;
use LakeDrops\DockerTraefik\Traefik; use LakeDrops\DockerTraefik\Traefik;
use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
/**
* Class Handler.
*
* @package LakeDrops\Docker4Drupal
*/
class Handler { class Handler {
/** /**
* The composer object of this session.
*
* @var \Composer\Composer * @var \Composer\Composer
*/ */
protected $composer; protected $composer;
/** /**
* The input-output object of the composer session.
*
* @var \Composer\IO\IOInterface * @var \Composer\IO\IOInterface
*/ */
protected $io; protected $io;
/** /**
* @var PackageInterface * The Drupal core package.
*
* @var \Composer\Package\PackageInterface
*/ */
protected $drupalCorePackage; protected $drupalCorePackage;
/** /**
* Handler constructor. * Handler constructor.
* *
* @param Composer $composer * @param \Composer\Composer $composer
* @param IOInterface $io * The composer object of this session.
* @param \Composer\IO\IOInterface $io
* The input-output object of the composer session.
*/ */
public function __construct(Composer $composer, IOInterface $io) { public function __construct(Composer $composer, IOInterface $io) {
$this->composer = $composer; $this->composer = $composer;
...@@ -40,10 +52,10 @@ class Handler { ...@@ -40,10 +52,10 @@ class Handler {
} }
/** /**
* Look up the Drupal core package object, or return it from where we cached * Look up the Drupal core package object.
* it in the $drupalCorePackage field.
* *
* @return PackageInterface * @return \Composer\Package\PackageInterface
* The Drupal core package.
*/ */
protected function getDrupalCorePackage() { protected function getDrupalCorePackage() {
if (!isset($this->drupalCorePackage)) { if (!isset($this->drupalCorePackage)) {
...@@ -58,7 +70,8 @@ class Handler { ...@@ -58,7 +70,8 @@ class Handler {
* @param string $name * @param string $name
* Name of the package to get from the current composer installation. * Name of the package to get from the current composer installation.
* *
* @return PackageInterface * @return \Composer\Package\PackageInterface
* The package.
*/ */
protected function getPackage($name) { protected function getPackage($name) {
return $this->composer->getRepositoryManager()->getLocalRepository()->findPackage($name, '*'); return $this->composer->getRepositoryManager()->getLocalRepository()->findPackage($name, '*');
...@@ -67,12 +80,14 @@ class Handler { ...@@ -67,12 +80,14 @@ class Handler {
/** /**
* Configure Drupal Project for Docker. * Configure Drupal Project for Docker.
* *
* @param ScriptEvent $event * @param \Composer\Script\Event $event
* The event that triggered the call of this function.
* @param bool $overwrite * @param bool $overwrite
* Whether to overwrite existing config files.
*/ */
public function configureProject($event, $overwrite = FALSE) { public function configureProject(Event $event, $overwrite = FALSE) {
// We only do the fancy stuff for developers // We only do the fancy stuff for developers.
if (!$event->isDevMode()) { if (!$event->isDevMode()) {
return; return;
} }
...@@ -94,13 +109,13 @@ class Handler { ...@@ -94,13 +109,13 @@ class Handler {
return; return;
} }
$corePath = $installationManager->getInstallPath($drupalCorePackage); $corePath = $installationManager->getInstallPath($drupalCorePackage);
// Directory where Drupal's index.php is located // Directory where Drupal's index.php is located.
$webRoot = dirname($corePath); $webRoot = dirname($corePath);
} }
// Directory where the root project is being created // Directory where the root project is being created.
$projectRoot = getcwd(); $projectRoot = getcwd();
// Directory where this plugin is being installed // Directory where this plugin is being installed.
$pluginRoot = $installationManager->getInstallPath($this->getPackage('lakedrops/docker4drupal')); $pluginRoot = $installationManager->getInstallPath($this->getPackage('lakedrops/docker4drupal'));
// If the d8-project-scaffold plugin is present we only execute this one // If the d8-project-scaffold plugin is present we only execute this one
...@@ -114,7 +129,7 @@ class Handler { ...@@ -114,7 +129,7 @@ class Handler {
$settingsPath = 'settings/default'; $settingsPath = 'settings/default';
} }
// Provide all the required files // Provide all the required files.
$twig_loader = new \Twig_Loader_Array([]); $twig_loader = new \Twig_Loader_Array([]);
$twig = new \Twig_Environment($twig_loader); $twig = new \Twig_Environment($twig_loader);
$options['webRoot'] = $webRoot . '/'; $options['webRoot'] = $webRoot . '/';
...@@ -128,7 +143,7 @@ class Handler { ...@@ -128,7 +143,7 @@ class Handler {
$rendered = $twig->render($filename, $options); $rendered = $twig->render($filename, $options);
if (!empty($def['add2yaml']) && isset($options[$filename])) { if (!empty($def['add2yaml']) && isset($options[$filename])) {
$yaml = Yaml::parse($rendered); $yaml = Yaml::parse($rendered);
$yaml = array_merge_recursive($yaml, $options[$filename]); $yaml = array_merge_recursive($yaml, $options[$filename]);
$rendered = Yaml::dump($yaml, 9, 2); $rendered = Yaml::dump($yaml, 9, 2);
} }
if ($fs->exists($file)) { if ($fs->exists($file)) {
...@@ -160,7 +175,7 @@ class Handler { ...@@ -160,7 +175,7 @@ class Handler {
$fs->chmod($file, 0664); $fs->chmod($file, 0664);
} }
// Make sure that settings.docker.php gets called from settings.php // Make sure that settings.docker.php gets called from settings.php.
$settingsPhpFile = $settingsPath . '/settings.php'; $settingsPhpFile = $settingsPath . '/settings.php';
if ($fs->exists(($settingsPhpFile))) { if ($fs->exists(($settingsPhpFile))) {
$settingsPhp = file_get_contents($settingsPhpFile); $settingsPhp = file_get_contents($settingsPhpFile);
...@@ -174,6 +189,19 @@ class Handler { ...@@ -174,6 +189,19 @@ class Handler {
$traefik->update(); $traefik->update();
} }
/**
* List of files and settings on how to handle them.
*
* @param string $projectRoot
* Name of the project's root directory.
* @param string $webRoot
* Name of the web's root directory.
* @param string $settingsPath
* Name of the settings directory.
*
* @return array
* List of files.
*/
protected function getFiles($projectRoot, $webRoot, $settingsPath) { protected function getFiles($projectRoot, $webRoot, $settingsPath) {
return [ return [
'settings.docker.php' => [ 'settings.docker.php' => [
...@@ -202,9 +230,10 @@ class Handler { ...@@ -202,9 +230,10 @@ class Handler {
} }
/** /**
* Retrieve excludes from optional "extra" configuration. * Retrieve options from composer.json "extra" configuration.
* *
* @return array * @return array
* The options.
*/ */
protected function getOptions() { protected function getOptions() {
$projectname = str_replace([' ', '-', '_', '.'], '', basename(getcwd())); $projectname = str_replace([' ', '-', '_', '.'], '', basename(getcwd()));
...@@ -225,8 +254,15 @@ class Handler { ...@@ -225,8 +254,15 @@ class Handler {
'drush' => [ 'drush' => [
'sql' => [ 'sql' => [
'tables' => [ 'tables' => [
'structure' => ['cache', 'cache_*', 'history', 'search_*', 'sessions', 'watchdog',], 'structure' => [
'skip' => ['none',], 'cache',
'cache_*',
'history',
'search_*',
'sessions',
'watchdog',
],
'skip' => ['none'],
], ],
], ],
], ],
...@@ -284,13 +320,22 @@ class Handler { ...@@ -284,13 +320,22 @@ class Handler {
/** /**
* Wrapper for git command in the root directory. * Wrapper for git command in the root directory.
* *
* @param $command * @param string $command
* Git command name, arguments and/or options. * Git command name, arguments and/or options.
*/ */
protected function git($command) { protected function git($command) {
passthru(escapeshellcmd('git -c "user.email=d8-project@lakedrops.com" ' . $command)); passthru(escapeshellcmd('git -c "user.email=d8-project@lakedrops.com" ' . $command));
} }
/**
* Determine local ipv4 address.
*
* @param string|null $interface
* The name of the interface for which to determine the ipv4 address.
*
* @return string|array
* The ipv4 address(es).
*/
private function getLocalIpv4($interface = NULL) { private function getLocalIpv4($interface = NULL) {
$out = explode(PHP_EOL, shell_exec('LC_ALL=C /sbin/ifconfig')); $out = explode(PHP_EOL, shell_exec('LC_ALL=C /sbin/ifconfig'));
$local_addrs = array(); $local_addrs = array();
...@@ -299,10 +344,11 @@ class Handler { ...@@ -299,10 +344,11 @@ class Handler {
$matches = array(); $matches = array();
if (preg_match('/^([a-z0-9]+)(:\d{1,2})?(\s)+Link/', $str, $matches)) { if (preg_match('/^([a-z0-9]+)(:\d{1,2})?(\s)+Link/', $str, $matches)) {
$ifname = $matches[1]; $ifname = $matches[1];
if(strlen($matches[2])>0) { if (strlen($matches[2]) > 0) {
$ifname .= $matches[2]; $ifname .= $matches[2];
} }
} elseif (preg_match('/inet addr:((?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3})\s/', $str, $matches)) { }
elseif (preg_match('/inet addr:((?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3})\s/', $str, $matches)) {
$local_addrs[$ifname] = $matches[1]; $local_addrs[$ifname] = $matches[1];
} }
} }
......
...@@ -2,12 +2,34 @@ ...@@ -2,12 +2,34 @@
namespace LakeDrops\Docker4Drupal; namespace LakeDrops\Docker4Drupal;
/**
* Class NestedArray.
*
* @package LakeDrops\Docker4Drupal
*/
class NestedArray { class NestedArray {
/**
* Deeply merges arrays. Borrowed from drupal.org/project/core.
*
* @return array
* The merged array.
*/
public static function mergeDeep() { public static function mergeDeep() {
return self::mergeDeepArray(func_get_args()); return self::mergeDeepArray(func_get_args());
} }
/**
* Deeply merges arrays. Borrowed from drupal.org/project/core.
*
* @param array $arrays
* An array of array that will be merged.
* @param bool $preserve_integer_keys
* Whether to preserve integer keys.
*
* @return array
* The merged array.
*/
public static function mergeDeepArray(array $arrays, $preserve_integer_keys = FALSE) { public static function mergeDeepArray(array $arrays, $preserve_integer_keys = FALSE) {
$result = []; $result = [];
foreach ($arrays as $array) { foreach ($arrays as $array) {
......
...@@ -6,6 +6,7 @@ use Composer\Composer; ...@@ -6,6 +6,7 @@ use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface; use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface; use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents; use Composer\Script\ScriptEvents;
/** /**
...@@ -14,6 +15,8 @@ use Composer\Script\ScriptEvents; ...@@ -14,6 +15,8 @@ use Composer\Script\ScriptEvents;
class Plugin implements PluginInterface, EventSubscriberInterface { class Plugin implements PluginInterface, EventSubscriberInterface {
/** /**
* The handler object to configure the project for docker.
*
* @var \LakeDrops\Docker4Drupal\Handler * @var \LakeDrops\Docker4Drupal\Handler
*/ */
protected $handler; protected $handler;
...@@ -40,8 +43,9 @@ class Plugin implements PluginInterface, EventSubscriberInterface { ...@@ -40,8 +43,9 @@ class Plugin implements PluginInterface, EventSubscriberInterface {
* Configure project event callback. * Configure project event callback.
* *
* @param \Composer\Script\Event $event * @param \Composer\Script\Event $event
* The event that triggered the call of this function.
*/ */
public function configureProject($event) { public function configureProject(Event $event) {
$this->handler->configureProject($event); $this->handler->configureProject($event);
} }
...@@ -49,8 +53,9 @@ class Plugin implements PluginInterface, EventSubscriberInterface { ...@@ -49,8 +53,9 @@ class Plugin implements PluginInterface, EventSubscriberInterface {
* Script callback for putting in composer scripts to configure the project. * Script callback for putting in composer scripts to configure the project.
* *
* @param \Composer\Script\Event $event * @param \Composer\Script\Event $event
* The event that triggered the call of this function.
*/ */
public static function config($event) { public static function config(Event $event) {
$handler = new Handler($event->getComposer(), $event->getIO()); $handler = new Handler($event->getComposer(), $event->getIO());
$handler->configureProject($event, TRUE); $handler->configureProject($event, TRUE);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment