diff --git a/composer.json b/composer.json index b40874e96b24b5b552c4052f3c02c896b5fed42d..05e04b5324cb805bf03c4d690773eb672e6e86ac 100644 --- a/composer.json +++ b/composer.json @@ -30,6 +30,14 @@ "lakedrops/dotenv": "^0.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": { "psr-4": { "LakeDrops\\Docker4Drupal\\": "src/" @@ -37,9 +45,5 @@ }, "extra": { "class": "LakeDrops\\Docker4Drupal\\Plugin" - }, - "require-dev": { - "composer/composer": "dev-master", - "phpunit/phpunit": "^4.4.0" } } diff --git a/src/Handler.php b/src/Handler.php index 0137228025b33972f09d975875b065b2b963bd58..d0bf2a7935f87f0b6d2d5e1b7a7b118d1578a24f 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -2,37 +2,49 @@ namespace LakeDrops\Docker4Drupal; -use Composer\Package\PackageInterface; use Composer\Composer; use Composer\IO\IOInterface; -use Composer\Script\Event as ScriptEvent; +use Composer\Script\Event; use LakeDrops\Component\Dotenv\Dotenv; use LakeDrops\DockerTraefik\Traefik; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Yaml\Yaml; +/** + * Class Handler. + * + * @package LakeDrops\Docker4Drupal + */ class Handler { /** + * The composer object of this session. + * * @var \Composer\Composer */ protected $composer; /** + * The input-output object of the composer session. + * * @var \Composer\IO\IOInterface */ protected $io; /** - * @var PackageInterface + * The Drupal core package. + * + * @var \Composer\Package\PackageInterface */ protected $drupalCorePackage; /** * Handler constructor. * - * @param Composer $composer - * @param IOInterface $io + * @param \Composer\Composer $composer + * 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) { $this->composer = $composer; @@ -40,10 +52,10 @@ class Handler { } /** - * Look up the Drupal core package object, or return it from where we cached - * it in the $drupalCorePackage field. + * Look up the Drupal core package object. * - * @return PackageInterface + * @return \Composer\Package\PackageInterface + * The Drupal core package. */ protected function getDrupalCorePackage() { if (!isset($this->drupalCorePackage)) { @@ -58,7 +70,8 @@ class Handler { * @param string $name * Name of the package to get from the current composer installation. * - * @return PackageInterface + * @return \Composer\Package\PackageInterface + * The package. */ protected function getPackage($name) { return $this->composer->getRepositoryManager()->getLocalRepository()->findPackage($name, '*'); @@ -67,12 +80,14 @@ class Handler { /** * 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 + * 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()) { return; } @@ -94,13 +109,13 @@ class Handler { return; } $corePath = $installationManager->getInstallPath($drupalCorePackage); - // Directory where Drupal's index.php is located + // Directory where Drupal's index.php is located. $webRoot = dirname($corePath); } - // Directory where the root project is being created + // Directory where the root project is being created. $projectRoot = getcwd(); - // Directory where this plugin is being installed + // Directory where this plugin is being installed. $pluginRoot = $installationManager->getInstallPath($this->getPackage('lakedrops/docker4drupal')); // If the d8-project-scaffold plugin is present we only execute this one @@ -114,7 +129,7 @@ class Handler { $settingsPath = 'settings/default'; } - // Provide all the required files + // Provide all the required files. $twig_loader = new \Twig_Loader_Array([]); $twig = new \Twig_Environment($twig_loader); $options['webRoot'] = $webRoot . '/'; @@ -128,7 +143,7 @@ class Handler { $rendered = $twig->render($filename, $options); if (!empty($def['add2yaml']) && isset($options[$filename])) { $yaml = Yaml::parse($rendered); - $yaml = array_merge_recursive($yaml, $options[$filename]); + $yaml = array_merge_recursive($yaml, $options[$filename]); $rendered = Yaml::dump($yaml, 9, 2); } if ($fs->exists($file)) { @@ -160,7 +175,7 @@ class Handler { $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'; if ($fs->exists(($settingsPhpFile))) { $settingsPhp = file_get_contents($settingsPhpFile); @@ -174,6 +189,19 @@ class Handler { $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) { return [ 'settings.docker.php' => [ @@ -202,9 +230,10 @@ class Handler { } /** - * Retrieve excludes from optional "extra" configuration. + * Retrieve options from composer.json "extra" configuration. * * @return array + * The options. */ protected function getOptions() { $projectname = str_replace([' ', '-', '_', '.'], '', basename(getcwd())); @@ -225,8 +254,15 @@ class Handler { 'drush' => [ 'sql' => [ 'tables' => [ - 'structure' => ['cache', 'cache_*', 'history', 'search_*', 'sessions', 'watchdog',], - 'skip' => ['none',], + 'structure' => [ + 'cache', + 'cache_*', + 'history', + 'search_*', + 'sessions', + 'watchdog', + ], + 'skip' => ['none'], ], ], ], @@ -284,13 +320,22 @@ class Handler { /** * Wrapper for git command in the root directory. * - * @param $command + * @param string $command * Git command name, arguments and/or options. */ protected function git($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) { $out = explode(PHP_EOL, shell_exec('LC_ALL=C /sbin/ifconfig')); $local_addrs = array(); @@ -299,10 +344,11 @@ class Handler { $matches = array(); if (preg_match('/^([a-z0-9]+)(:\d{1,2})?(\s)+Link/', $str, $matches)) { $ifname = $matches[1]; - if(strlen($matches[2])>0) { + if (strlen($matches[2]) > 0) { $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]; } } diff --git a/src/NestedArray.php b/src/NestedArray.php index e36c0727ba2f918434df358274f227fe2a14cbe7..2323ec2773f503de613b4131c165570d51c23759 100644 --- a/src/NestedArray.php +++ b/src/NestedArray.php @@ -2,12 +2,34 @@ namespace LakeDrops\Docker4Drupal; +/** + * Class NestedArray. + * + * @package LakeDrops\Docker4Drupal + */ class NestedArray { + /** + * Deeply merges arrays. Borrowed from drupal.org/project/core. + * + * @return array + * The merged array. + */ public static function mergeDeep() { 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) { $result = []; foreach ($arrays as $array) { diff --git a/src/Plugin.php b/src/Plugin.php index 139d55acb6187d1377f5a6bae729d729289099a8..af56d8fc9d499f7383e4e5d88964e31627724ea8 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -6,6 +6,7 @@ use Composer\Composer; use Composer\EventDispatcher\EventSubscriberInterface; use Composer\IO\IOInterface; use Composer\Plugin\PluginInterface; +use Composer\Script\Event; use Composer\Script\ScriptEvents; /** @@ -14,6 +15,8 @@ use Composer\Script\ScriptEvents; class Plugin implements PluginInterface, EventSubscriberInterface { /** + * The handler object to configure the project for docker. + * * @var \LakeDrops\Docker4Drupal\Handler */ protected $handler; @@ -40,8 +43,9 @@ class Plugin implements PluginInterface, EventSubscriberInterface { * Configure project event callback. * * @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); } @@ -49,8 +53,9 @@ class Plugin implements PluginInterface, EventSubscriberInterface { * Script callback for putting in composer scripts to configure the project. * * @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->configureProject($event, TRUE); }