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
No related tags found
No related merge requests found
......@@ -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);
}
}
<?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);
}
}
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