Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The possibility to specify a path to the settings file #41

Closed
wants to merge 97 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
4c57a85
Refactor DefaultConfigFinder extract the settings file name to a cons…
kudashevs Feb 15, 2022
abb1e36
Refactor DefaultConfigFinder extract a generate file path method.
kudashevs Feb 15, 2022
cdf4ed0
Refactor DefaultConfigFinder upfate filepath generation with the new …
kudashevs Feb 15, 2022
a95370b
Update DefaultConfigFinder add constructor with a path parameter.
kudashevs Feb 15, 2022
895fdf2
Update DefaultConfigFinder add filepath generator to the get config f…
kudashevs Feb 15, 2022
88871bd
Update DefaultConfigFinderTest add check config finder works in Windo…
kudashevs Feb 15, 2022
c13ff86
Update DefaultConfigFinder add create a file functionality to the gen…
kudashevs Feb 15, 2022
c9ccd6f
Update DefaultConfigFinderTest add check config finder works in non-W…
kudashevs Feb 15, 2022
964237e
Refactor DefaultConfigFinder rename the path variable in the generate…
kudashevs Feb 15, 2022
baf4434
Refactor DefaultConfigFinder move the is_readable check to the genera…
kudashevs Feb 15, 2022
056483e
Refactor DefaultConfigFinder extract format home directory method.
kudashevs Feb 15, 2022
ff43fa6
Update DefaultConfigFinderTest add retrieve settings filepath helper.
kudashevs Feb 15, 2022
5e017ff
Update DefaultConfigFinderTest add teardown with the deletion of a se…
kudashevs Feb 15, 2022
cfa485c
Update DefaultConfigFinderTest add stricter testing conditions to som…
kudashevs Feb 16, 2022
c669128
Style DefaultConfigFinderTest remove a redundant new line.
kudashevs Feb 16, 2022
8210d3c
Fix DefaultConfigFinderTest fix wrong skip conditions in some tests.
kudashevs Feb 16, 2022
8e94502
Update DefaultConfigFinderTest refine the Windows OS check.
kudashevs Feb 16, 2022
2608520
Refactor DefaultConfigFinder extract the isWindows method.
kudashevs Feb 16, 2022
b2b94cf
Style DefaultConfigFinderTest fix the arrow functions style.
kudashevs Feb 16, 2022
f3deef3
Update Contracts add a new ConfigManager abstraction.
kudashevs Feb 16, 2022
1dd9863
Update Config add FileConfigManager class with basic realization.
kudashevs Feb 16, 2022
7b7b475
Update FileConfigManager add the source creation functionality.
kudashevs Feb 16, 2022
ea9a412
Update FileConfigManager update Throwable in catch with globals scope.
kudashevs Feb 16, 2022
1498e67
Refactor FileConfigManager extract a preparePath method.
kudashevs Feb 16, 2022
f02a565
Refactor FileConfigManagerTest change the file exists check to the ph…
kudashevs Feb 16, 2022
a500c83
Update FileConfigManager add initialize file path from a home variabl…
kudashevs Feb 16, 2022
afe9928
Update FileConfigManager add initialize file path from a home variabl…
kudashevs Feb 16, 2022
271e6b9
Refactor FileConfigManagerTest remove redundant not empty return chec…
kudashevs Feb 16, 2022
246ade2
Update IgnitionConfig add a manager field with initilization.
kudashevs Feb 16, 2022
b76ba05
Update ConfigManager update signature of the save method.
kudashevs Feb 16, 2022
4e429a0
Refactor FileConfigManager extract a isValidFile method.
kudashevs Feb 16, 2022
7c760c5
Update FileConfigManager add a realization to the save method.
kudashevs Feb 16, 2022
03ed889
Refactor FileConfigManager reorder class methods.
kudashevs Feb 16, 2022
cd35032
Update FileConfigManager add a realization to the load method.
kudashevs Feb 16, 2022
989f226
Refactor FileConfigManager reorder class methods.
kudashevs Feb 16, 2022
265b504
Update FileConfigManager add file exists check before creating to the…
kudashevs Feb 16, 2022
b5cd1f2
Update IgnitionConfig add the manager realization to the dependent me…
kudashevs Feb 16, 2022
7373758
Refactor ConfigManager remove the updateSource signature.
kudashevs Feb 16, 2022
9c31ba2
Refactor FileConfigManager reorder class methods.
kudashevs Feb 16, 2022
a6b4d86
Update FileConfigManager add optional constructor parameter.
kudashevs Feb 16, 2022
92f4a56
Refactor FileConfigManager rename the findHomeDirectory method.
kudashevs Feb 16, 2022
4d56634
Update IgnitionConfigTest add a usage of a constructor argument to th…
kudashevs Feb 16, 2022
d8bc899
Refactor Config remove the DefaultConfigFinder class.
kudashevs Feb 16, 2022
f89f729
Update FileConfigManager move realpath() invocation to the isValidPat…
kudashevs Feb 16, 2022
d93a92b
Style FileConfigManager fix a negation style in the load method.
kudashevs Feb 16, 2022
574b19f
Update FileConfigManagerTest remove a redundant helpers section comment.
kudashevs Feb 17, 2022
6c15b77
Style IgnitionConfig fix redundant new line.
kudashevs Feb 17, 2022
1b71a91
Refactor IgnitionConfig extract a check manager in a container method.
kudashevs Feb 17, 2022
e4bffa1
Refactor IgnitionConfig invert the logic of the check in container me…
kudashevs Feb 17, 2022
56dfef5
Style FileConfigManager add a warning comment to the isValidPath method.
kudashevs Feb 17, 2022
5959c30
Style FileConfigManager add a warning comment to the isValidPath method.
kudashevs Feb 17, 2022
8262647
Update FileConfigManager remove realpaht() invocation in the isValidP…
kudashevs Feb 17, 2022
e073859
Style FileConfigManager add inheritdoc tag to some methods.
kudashevs Feb 17, 2022
5d8d288
Refactor FileConfigManager change the visibility of the initPath method.
kudashevs Feb 17, 2022
ef79cd6
Update FileConfigManager change the import of Throwable to the explic…
kudashevs Feb 17, 2022
a2477c1
Update IgnitionConfigTest remove a redundant helpers section comment.
kudashevs Feb 18, 2022
957714d
Fix IgnitionConfigTest fix a usage of the path variable in the app he…
kudashevs Feb 18, 2022
4ee239d
Update IgnitionConfig remove no longer used createSource() call from …
kudashevs Feb 18, 2022
9fea4cf
Style IgnitionConfig remove a redundant new line in the saveValues do…
kudashevs Feb 18, 2022
a497ccd
Update FileConfigManager add a created state field.
kudashevs Feb 18, 2022
3cff76e
Update FileConfigManager add a new guard clause to the load method.
kudashevs Feb 18, 2022
eae30c3
Update FileConfigManager extract a readFromFile method.
kudashevs Feb 18, 2022
1308e07
Refactor FileConfigManager rename options variable in the readFromFil…
kudashevs Feb 18, 2022
874eb5d
Update FileConfigManager extract a isFileCreated method.
kudashevs Feb 18, 2022
14df3a0
Update FileConfigManager add a new guard clause to the save method.
kudashevs Feb 18, 2022
8b6fb09
Refactor FileConfigManager extract a saveToFile method.
kudashevs Feb 18, 2022
77431fd
Update FileConfigManager make more strict the behavior of the json_en…
kudashevs Feb 18, 2022
abdeb22
Refactor FileConfigManager reorder class methods.
kudashevs Feb 18, 2022
c8d6e2f
Refactor FileConfigManager extract a createFile method.
kudashevs Feb 18, 2022
f35ebd0
Refactor FileConfigManager change the created field to a file field.
kudashevs Feb 18, 2022
21f1b94
Update FileConfigManager update the isFileCreated method.
kudashevs Feb 18, 2022
59628fc
Update FileConfigManagerTest add check it cannot create file when a w…
kudashevs Feb 18, 2022
639f7d6
Refactor FileConfigManager remove exceeding generateFullFilePath call…
kudashevs Feb 18, 2022
118549d
Refactor FileConfigManager remove no longer used parameter in the isV…
kudashevs Feb 18, 2022
2379fa9
Refactor FileConfigManager switch to use the file field in some methods.
kudashevs Feb 18, 2022
008e646
Fix FileConfigManager fix the wrong creatinal logic in the createSour…
kudashevs Feb 18, 2022
6d24482
Update FileConfigManager add a formatting of a path to the generateFu…
kudashevs Feb 18, 2022
4f030e3
Refactor FileConfigManager move the guard clause from load to the rea…
kudashevs Feb 18, 2022
6d42df2
Refactor FileConfigManager move the guard clause from save to the sav…
kudashevs Feb 18, 2022
2299d55
Update FileConfigManager remove formatting of a path from the generat…
kudashevs Feb 18, 2022
6eb88a7
Update FileConfigManager add a formatting to a return of the initPath…
kudashevs Feb 18, 2022
5dfbbcb
Update FileConfigManagerTest add check it cannot create file when an …
kudashevs Feb 18, 2022
9284fb3
Update FileConfigManager add a guard clause for empty path in the cre…
kudashevs Feb 18, 2022
dfec1e7
Refactor FileConfigManagerTest remove no longer used the createSource…
kudashevs Feb 18, 2022
1f71719
Refactor ConfigManager rename the createSource to the createPersisten…
kudashevs Feb 18, 2022
e2e63fa
Refactor ConfigManager rename the getSource to the getPersistentInfo …
kudashevs Feb 18, 2022
92caef5
Refactor ConfigManager reorder interface method signatures.
kudashevs Feb 18, 2022
f4ceb0c
Refactor FileConfigManager reorder class methods.
kudashevs Feb 18, 2022
c566033
Update IgnitionConfig add more checks to the isInContainer method.
kudashevs Feb 18, 2022
ac37f0b
Refactor FileConfigManager rename isValidFile to isFileValid method.
kudashevs Feb 18, 2022
2f710c6
Refactor FileConfigManager change the visibility of the initPathFromE…
kudashevs Feb 18, 2022
d7025fe
Refactor FileConfigManager reorder class methods.
kudashevs Feb 18, 2022
672616e
Refactor FileConfigManager change the visibility of the preparePath m…
kudashevs Feb 18, 2022
0246027
Update IgnitionConfigTest update a title of the check retrieve from f…
kudashevs Feb 18, 2022
3315661
Update IgnitionConfig remove the isInContainer method.
kudashevs Feb 18, 2022
fcd27af
Update IgnitionConfig change the container is available check in the …
kudashevs Feb 19, 2022
b5a73c3
Refactor FileConfigManager rename the generateFullFilePath method.
kudashevs Feb 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions src/Config/DefaultConfigFinder.php

This file was deleted.

175 changes: 175 additions & 0 deletions src/Config/FileConfigManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php

namespace Spatie\Ignition\Config;

use Spatie\Ignition\Contracts\ConfigManager;
use Throwable;

class FileConfigManager implements ConfigManager
{
private const SETTINGS_FILE_NAME = '.ignition.json';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can rename this to CONFIG_FILE_NAME to keep it in line with the ConfigManager, etc...

Suggested change
private const SETTINGS_FILE_NAME = '.ignition.json';
private const CONFIG_FILE_NAME = '.ignition.json';

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,

I kind of agree, but to me, the config/ignition.php is a config file and .ignition.json is a settings file. One of the reasons for this separation is the Laravel Ignition button naming (we have a Save settings button there). So, not to confuse users I tried to differentiate these two concepts by introducing this term settings file. That's why I would like to stick to this name settings file and use it for this file if you don't mind.

However, if you don't like this differentiation at all let's change it. @AlexVanderbist please let me know about your decision.


private string $path;

private string $file;

public function __construct(string $path = '')
{
$this->path = $this->initPath($path);
$this->file = $this->createFile();
}

protected function initPath(string $path): string
{
if ($path !== '') {
return $this->initPathFromArgument($path);
}

return $this->initPathFromEnvironment();
}

private function initPathFromArgument(string $path): string
{
if ($this->isValidPath($path)) {
return $this->preparePath($path);
}

return '';
}

private function isValidPath(string $path): bool
{
return file_exists($path) && is_writable($path);
}

private function initPathFromEnvironment(): string
{
if ($this->isWindows()) {
if (empty($_SERVER['HOMEDRIVE']) || empty($_SERVER['HOMEPATH'])) {
return '';
}

$homeDirectory = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'];

return $this->preparePath($homeDirectory);
}

if ($homeDirectory = getenv('HOME')) {
return $this->preparePath($homeDirectory);
}

return '';
}

private function isWindows(): bool
{
return str_starts_with(strtoupper(PHP_OS), 'WIN');
}

private function preparePath(string $path): string
{
return rtrim($path, DIRECTORY_SEPARATOR);
}

protected function createFile(): string
{
if ($this->createPersistent()) {
return $this->generateFullFileName();
}

return '';
}

private function generateFullFileName(): string
{
return $this->path . DIRECTORY_SEPARATOR . self::SETTINGS_FILE_NAME;
}

/** {@inheritDoc} */
public function load(): array
{
return $this->readFromFile();
}

private function readFromFile()
{
if (! $this->isFileCreated()) {
return [];
}

if (! $this->isFileValid()) {
return [];
}

$content = (string)file_get_contents($this->file);
$settings = json_decode($content, true) ?? [];

return $settings;
}

/** {@inheritDoc} */
public function save(array $options): bool
{
return $this->saveToFile($options);
}

private function saveToFile(array $options): bool
{
if (! $this->isFileCreated()) {
return false;
}

if (! $this->isFileValid()) {
return false;
}

try {
file_put_contents($this->file, json_encode($options, JSON_THROW_ON_ERROR));
} catch (Throwable) {
return false;
}

return true;
}

protected function isFileCreated(): bool
{
return $this->file !== '';
}

protected function isFileValid(): bool
{
return file_exists($this->file) && @is_writable($this->file);
}

/** {@inheritDoc} */
public function createPersistent(): bool
{
if ($this->isEmptyPath()) {
return false;
}

$file = $this->generateFullFileName();

if (file_exists($file)) {
return true;
}

return (file_put_contents($file, '') !== false);
}

private function isEmptyPath(): bool
{
return trim($this->path) === '';
}

/** {@inheritDoc} */
public function getPersistentInfo(): array
{
return [
'name' => self::SETTINGS_FILE_NAME,
'path' => $this->path,
'file' => $this->file,
];
}
}
41 changes: 15 additions & 26 deletions src/Config/IgnitionConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace Spatie\Ignition\Config;

use Illuminate\Contracts\Support\Arrayable;
use Spatie\Ignition\Contracts\ConfigManager;
use Throwable;

class IgnitionConfig implements Arrayable
{
private ConfigManager $manager;

public static function loadFromConfigFile(): self
{
return (new self())->loadConfigFile();
Expand All @@ -20,6 +23,7 @@ public function __construct(protected array $options = [])
$defaultOptions = $this->getDefaultOptions();

$this->options = array_merge($defaultOptions, $options);
$this->manager = $this->initConfigManager();
}

public function setOption(string $name, string $value): self
Expand All @@ -29,6 +33,15 @@ public function setOption(string $name, string $value): self
return $this;
}

private function initConfigManager(): ConfigManager
{
try {
return app(ConfigManager::class);
} catch (Throwable) {
return new FileConfigManager();
}
}

/** @param array<string, string> $options */
public function merge(array $options): self
{
Expand All @@ -47,39 +60,16 @@ public function loadConfigFile(): self
/** @return array<string, mixed> */
public function getConfigOptions(): array
{
$configFilePath = (new DefaultConfigFinder())->getConfigFilePath();

$options = [];

if (file_exists($configFilePath)) {
$content = (string)file_get_contents($configFilePath);

$options = json_decode($content, true) ?? [];
}

return $options;
return $this->manager->load();
}

/**
* @param array<string, mixed> $options
*
* @return bool
*/
public function saveValues(array $options): bool
{
$configFilePath = (new DefaultConfigFinder())->getConfigFilePath();

if (! $configFilePath) {
return false;
}

try {
file_put_contents($configFilePath, json_encode($options));
} catch (Throwable) {
return false;
}

return true;
return $this->manager->save($options);
}

public function hideSolutions(): bool
Expand Down Expand Up @@ -148,7 +138,6 @@ public function toArray(): array
];
}


/**
* @return array<string, mixed> $options
*/
Expand Down
17 changes: 17 additions & 0 deletions src/Contracts/ConfigManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Spatie\Ignition\Contracts;

interface ConfigManager
{
/** @return array<string, mixed> */
public function load(): array;

/** @param array<string, mixed> $options */
public function save(array $options): bool;

public function createPersistent(): bool;

/** @return array<string, mixed> */
public function getPersistentInfo(): array;
}
Loading