From 7f9a838663f5b9e0372a7eb94c1c94683a204c9e Mon Sep 17 00:00:00 2001 From: jurgenhaas <juergen@paragon-es.de> Date: Sat, 22 Dec 2018 21:59:48 +0100 Subject: [PATCH] composer/plugin/docker4drupal#33 Better handling of git ignore and code style --- BaseHandler.php | 29 ++++------------------- Utils.php | 61 +++++++++++++++++++++++++++++++++++++------------ 2 files changed, 51 insertions(+), 39 deletions(-) diff --git a/BaseHandler.php b/BaseHandler.php index dfcee3d..b9bfe9b 100644 --- a/BaseHandler.php +++ b/BaseHandler.php @@ -47,11 +47,6 @@ abstract class BaseHandler implements BaseHandlerInterface { */ protected $drupalCorePackage; - /** - * @var string[] - */ - protected $ignoredGitPatterns; - /** * Handler constructor. * @@ -101,7 +96,7 @@ abstract class BaseHandler implements BaseHandlerInterface { /** * {@inheritdoc} */ - public function isDevMode() { + public function isDevMode(): bool { if ($this->event !== NULL) { return $this->event->isDevMode(); } @@ -114,7 +109,7 @@ abstract class BaseHandler implements BaseHandlerInterface { /** * {@inheritdoc} */ - public function isLocalDevMode() { + public function isLocalDevMode(): bool { $local_dev_mode = getenv('LAKEDROPS_DEV_ENV'); return !empty($local_dev_mode); } @@ -122,7 +117,7 @@ abstract class BaseHandler implements BaseHandlerInterface { /** * {@inheritdoc} */ - public function isCiContext() { + public function isCiContext(): bool { $ci_project_dir = getenv('CI_PROJECT_DIR'); return !empty($ci_project_dir); } @@ -131,28 +126,14 @@ abstract class BaseHandler implements BaseHandlerInterface { * {@inheritdoc} */ public function git($command) { - passthru(escapeshellcmd('git -c "user.email=composer@lakedrops.com" ' . $command)); + Utils::git($command); } /** * {@inheritdoc} */ public function gitIgnore($pattern) { - if ($this->ignoredGitPatterns === NULL) { - if (file_exists('.gitignore')) { - $this->ignoredGitPatterns = explode(PHP_EOL, file_get_contents('.gitignore')); - } - else { - $this->ignoredGitPatterns = []; - } - } - - if (\in_array($pattern, $this->ignoredGitPatterns, TRUE)) { - return; - } - - $this->ignoredGitPatterns[] = $pattern; - $this->git('ignore ' . $pattern); + Utils::gitIgnore($pattern); } } diff --git a/Utils.php b/Utils.php index c6f8417..277e6b6 100644 --- a/Utils.php +++ b/Utils.php @@ -1,4 +1,4 @@ -<?php +<?php /** @noinspection UnusedConstructorDependenciesInspection */ namespace LakeDrops\Component\Composer; @@ -15,28 +15,33 @@ final class Utils { * * @var \Composer\Composer */ - protected $composer; + private $composer; /** * Content of the composer.json file. * * @var array */ - protected $content; + private $content; /** * The path of the directory of the composer.json file. * * @var string */ - protected $sourcePath; + private $sourcePath; /** * The full filename of the composer.json file. * * @var string */ - protected $composerJsonPath; + private $composerJsonPath; + + /** + * @var string[] + */ + private static $ignoredGitPatterns; /** * Utils constructor. @@ -48,9 +53,7 @@ final class Utils { */ public function __construct(Composer $composer, $sourcePath = NULL) { $this->composer = $composer; - $this->sourcePath = isset($sourcePath) ? - $sourcePath : - getcwd() . $composer->getPackage()->getTargetDir(); + $this->sourcePath = $sourcePath ?? getcwd() . $composer->getPackage()->getTargetDir(); $this->composerJsonPath = $this->sourcePath . '/composer.json'; $this->read(); } @@ -60,7 +63,7 @@ final class Utils { * * @return $this */ - public function read() { + public function read(): self { $this->content = []; if (file_exists($this->composerJsonPath)) { $jsonFile = new JsonFile($this->composerJsonPath); @@ -74,7 +77,7 @@ final class Utils { * * @return $this */ - public function write() { + public function write(): self { $jsonFile = new JsonFile($this->composerJsonPath); if (file_exists($this->composerJsonPath)) { $content = $jsonFile->read(); @@ -102,7 +105,7 @@ final class Utils { * The settings of the key. */ public function getSection($key) { - return isset($this->content[$key]) ? $this->content[$key] : []; + return $this->content[$key] ?? []; } /** @@ -118,7 +121,7 @@ final class Utils { */ public function getSubSection($key1, $key2) { $section = $this->getSection($key1); - return isset($section[$key2]) ? $section[$key2] : []; + return $section[$key2] ?? []; } /** @@ -136,7 +139,7 @@ final class Utils { */ public function getSubSubSection($key1, $key2, $key3) { $subSection = $this->getSubSection($key1, $key2); - return isset($subSection[$key3]) ? $subSection[$key3] : []; + return $subSection[$key3] ?? []; } /** @@ -149,7 +152,7 @@ final class Utils { * * @return $this */ - public function setSection($key, $value) { + public function setSection($key, $value): self { $this->content[$key] = $value; return $this; } @@ -166,9 +169,37 @@ final class Utils { * * @return $this */ - public function setSubSection($key1, $key2, $value) { + public function setSubSection($key1, $key2, $value): self { $this->content[$key1][$key2] = $value; return $this; } + /** + * @param string $command + */ + public static function git($command) { + passthru(escapeshellcmd('git -c "user.email=composer@lakedrops.com" ' . $command)); + } + + /** + * @param string $pattern + */ + public static function gitIgnore($pattern) { + if (self::$ignoredGitPatterns === NULL) { + if (file_exists('.gitignore')) { + self::$ignoredGitPatterns = explode(PHP_EOL, file_get_contents('.gitignore')); + } + else { + self::$ignoredGitPatterns = []; + } + } + + if (\in_array($pattern, self::$ignoredGitPatterns, TRUE)) { + return; + } + + self::$ignoredGitPatterns[] = $pattern; + self::git('ignore ' . $pattern); + } + } -- GitLab