Skip to content
Snippets Groups Projects
Commit 7f9a8386 authored by jurgenhaas's avatar jurgenhaas
Browse files

composer/plugin/docker4drupal#33 Better handling of git ignore and code style

parent e805a3e4
No related branches found
Tags v1.4.1
No related merge requests found
...@@ -47,11 +47,6 @@ abstract class BaseHandler implements BaseHandlerInterface { ...@@ -47,11 +47,6 @@ abstract class BaseHandler implements BaseHandlerInterface {
*/ */
protected $drupalCorePackage; protected $drupalCorePackage;
/**
* @var string[]
*/
protected $ignoredGitPatterns;
/** /**
* Handler constructor. * Handler constructor.
* *
...@@ -101,7 +96,7 @@ abstract class BaseHandler implements BaseHandlerInterface { ...@@ -101,7 +96,7 @@ abstract class BaseHandler implements BaseHandlerInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function isDevMode() { public function isDevMode(): bool {
if ($this->event !== NULL) { if ($this->event !== NULL) {
return $this->event->isDevMode(); return $this->event->isDevMode();
} }
...@@ -114,7 +109,7 @@ abstract class BaseHandler implements BaseHandlerInterface { ...@@ -114,7 +109,7 @@ abstract class BaseHandler implements BaseHandlerInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function isLocalDevMode() { public function isLocalDevMode(): bool {
$local_dev_mode = getenv('LAKEDROPS_DEV_ENV'); $local_dev_mode = getenv('LAKEDROPS_DEV_ENV');
return !empty($local_dev_mode); return !empty($local_dev_mode);
} }
...@@ -122,7 +117,7 @@ abstract class BaseHandler implements BaseHandlerInterface { ...@@ -122,7 +117,7 @@ abstract class BaseHandler implements BaseHandlerInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function isCiContext() { public function isCiContext(): bool {
$ci_project_dir = getenv('CI_PROJECT_DIR'); $ci_project_dir = getenv('CI_PROJECT_DIR');
return !empty($ci_project_dir); return !empty($ci_project_dir);
} }
...@@ -131,28 +126,14 @@ abstract class BaseHandler implements BaseHandlerInterface { ...@@ -131,28 +126,14 @@ abstract class BaseHandler implements BaseHandlerInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function git($command) { public function git($command) {
passthru(escapeshellcmd('git -c "user.email=composer@lakedrops.com" ' . $command)); Utils::git($command);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function gitIgnore($pattern) { public function gitIgnore($pattern) {
if ($this->ignoredGitPatterns === NULL) { Utils::gitIgnore($pattern);
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);
} }
} }
<?php <?php /** @noinspection UnusedConstructorDependenciesInspection */
namespace LakeDrops\Component\Composer; namespace LakeDrops\Component\Composer;
...@@ -15,28 +15,33 @@ final class Utils { ...@@ -15,28 +15,33 @@ final class Utils {
* *
* @var \Composer\Composer * @var \Composer\Composer
*/ */
protected $composer; private $composer;
/** /**
* Content of the composer.json file. * Content of the composer.json file.
* *
* @var array * @var array
*/ */
protected $content; private $content;
/** /**
* The path of the directory of the composer.json file. * The path of the directory of the composer.json file.
* *
* @var string * @var string
*/ */
protected $sourcePath; private $sourcePath;
/** /**
* The full filename of the composer.json file. * The full filename of the composer.json file.
* *
* @var string * @var string
*/ */
protected $composerJsonPath; private $composerJsonPath;
/**
* @var string[]
*/
private static $ignoredGitPatterns;
/** /**
* Utils constructor. * Utils constructor.
...@@ -48,9 +53,7 @@ final class Utils { ...@@ -48,9 +53,7 @@ final class Utils {
*/ */
public function __construct(Composer $composer, $sourcePath = NULL) { public function __construct(Composer $composer, $sourcePath = NULL) {
$this->composer = $composer; $this->composer = $composer;
$this->sourcePath = isset($sourcePath) ? $this->sourcePath = $sourcePath ?? getcwd() . $composer->getPackage()->getTargetDir();
$sourcePath :
getcwd() . $composer->getPackage()->getTargetDir();
$this->composerJsonPath = $this->sourcePath . '/composer.json'; $this->composerJsonPath = $this->sourcePath . '/composer.json';
$this->read(); $this->read();
} }
...@@ -60,7 +63,7 @@ final class Utils { ...@@ -60,7 +63,7 @@ final class Utils {
* *
* @return $this * @return $this
*/ */
public function read() { public function read(): self {
$this->content = []; $this->content = [];
if (file_exists($this->composerJsonPath)) { if (file_exists($this->composerJsonPath)) {
$jsonFile = new JsonFile($this->composerJsonPath); $jsonFile = new JsonFile($this->composerJsonPath);
...@@ -74,7 +77,7 @@ final class Utils { ...@@ -74,7 +77,7 @@ final class Utils {
* *
* @return $this * @return $this
*/ */
public function write() { public function write(): self {
$jsonFile = new JsonFile($this->composerJsonPath); $jsonFile = new JsonFile($this->composerJsonPath);
if (file_exists($this->composerJsonPath)) { if (file_exists($this->composerJsonPath)) {
$content = $jsonFile->read(); $content = $jsonFile->read();
...@@ -102,7 +105,7 @@ final class Utils { ...@@ -102,7 +105,7 @@ final class Utils {
* The settings of the key. * The settings of the key.
*/ */
public function getSection($key) { public function getSection($key) {
return isset($this->content[$key]) ? $this->content[$key] : []; return $this->content[$key] ?? [];
} }
/** /**
...@@ -118,7 +121,7 @@ final class Utils { ...@@ -118,7 +121,7 @@ final class Utils {
*/ */
public function getSubSection($key1, $key2) { public function getSubSection($key1, $key2) {
$section = $this->getSection($key1); $section = $this->getSection($key1);
return isset($section[$key2]) ? $section[$key2] : []; return $section[$key2] ?? [];
} }
/** /**
...@@ -136,7 +139,7 @@ final class Utils { ...@@ -136,7 +139,7 @@ final class Utils {
*/ */
public function getSubSubSection($key1, $key2, $key3) { public function getSubSubSection($key1, $key2, $key3) {
$subSection = $this->getSubSection($key1, $key2); $subSection = $this->getSubSection($key1, $key2);
return isset($subSection[$key3]) ? $subSection[$key3] : []; return $subSection[$key3] ?? [];
} }
/** /**
...@@ -149,7 +152,7 @@ final class Utils { ...@@ -149,7 +152,7 @@ final class Utils {
* *
* @return $this * @return $this
*/ */
public function setSection($key, $value) { public function setSection($key, $value): self {
$this->content[$key] = $value; $this->content[$key] = $value;
return $this; return $this;
} }
...@@ -166,9 +169,37 @@ final class Utils { ...@@ -166,9 +169,37 @@ final class Utils {
* *
* @return $this * @return $this
*/ */
public function setSubSection($key1, $key2, $value) { public function setSubSection($key1, $key2, $value): self {
$this->content[$key1][$key2] = $value; $this->content[$key1][$key2] = $value;
return $this; 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);
}
} }
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