generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 56
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
Closed
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 abb1e36
Refactor DefaultConfigFinder extract a generate file path method.
kudashevs cdf4ed0
Refactor DefaultConfigFinder upfate filepath generation with the new …
kudashevs a95370b
Update DefaultConfigFinder add constructor with a path parameter.
kudashevs 895fdf2
Update DefaultConfigFinder add filepath generator to the get config f…
kudashevs 88871bd
Update DefaultConfigFinderTest add check config finder works in Windo…
kudashevs c13ff86
Update DefaultConfigFinder add create a file functionality to the gen…
kudashevs c9ccd6f
Update DefaultConfigFinderTest add check config finder works in non-W…
kudashevs 964237e
Refactor DefaultConfigFinder rename the path variable in the generate…
kudashevs baf4434
Refactor DefaultConfigFinder move the is_readable check to the genera…
kudashevs 056483e
Refactor DefaultConfigFinder extract format home directory method.
kudashevs ff43fa6
Update DefaultConfigFinderTest add retrieve settings filepath helper.
kudashevs 5e017ff
Update DefaultConfigFinderTest add teardown with the deletion of a se…
kudashevs cfa485c
Update DefaultConfigFinderTest add stricter testing conditions to som…
kudashevs c669128
Style DefaultConfigFinderTest remove a redundant new line.
kudashevs 8210d3c
Fix DefaultConfigFinderTest fix wrong skip conditions in some tests.
kudashevs 8e94502
Update DefaultConfigFinderTest refine the Windows OS check.
kudashevs 2608520
Refactor DefaultConfigFinder extract the isWindows method.
kudashevs b2b94cf
Style DefaultConfigFinderTest fix the arrow functions style.
kudashevs f3deef3
Update Contracts add a new ConfigManager abstraction.
kudashevs 1dd9863
Update Config add FileConfigManager class with basic realization.
kudashevs 7b7b475
Update FileConfigManager add the source creation functionality.
kudashevs ea9a412
Update FileConfigManager update Throwable in catch with globals scope.
kudashevs 1498e67
Refactor FileConfigManager extract a preparePath method.
kudashevs f02a565
Refactor FileConfigManagerTest change the file exists check to the ph…
kudashevs a500c83
Update FileConfigManager add initialize file path from a home variabl…
kudashevs afe9928
Update FileConfigManager add initialize file path from a home variabl…
kudashevs 271e6b9
Refactor FileConfigManagerTest remove redundant not empty return chec…
kudashevs 246ade2
Update IgnitionConfig add a manager field with initilization.
kudashevs b76ba05
Update ConfigManager update signature of the save method.
kudashevs 4e429a0
Refactor FileConfigManager extract a isValidFile method.
kudashevs 7c760c5
Update FileConfigManager add a realization to the save method.
kudashevs 03ed889
Refactor FileConfigManager reorder class methods.
kudashevs cd35032
Update FileConfigManager add a realization to the load method.
kudashevs 989f226
Refactor FileConfigManager reorder class methods.
kudashevs 265b504
Update FileConfigManager add file exists check before creating to the…
kudashevs b5cd1f2
Update IgnitionConfig add the manager realization to the dependent me…
kudashevs 7373758
Refactor ConfigManager remove the updateSource signature.
kudashevs 9c31ba2
Refactor FileConfigManager reorder class methods.
kudashevs a6b4d86
Update FileConfigManager add optional constructor parameter.
kudashevs 92f4a56
Refactor FileConfigManager rename the findHomeDirectory method.
kudashevs 4d56634
Update IgnitionConfigTest add a usage of a constructor argument to th…
kudashevs d8bc899
Refactor Config remove the DefaultConfigFinder class.
kudashevs f89f729
Update FileConfigManager move realpath() invocation to the isValidPat…
kudashevs d93a92b
Style FileConfigManager fix a negation style in the load method.
kudashevs 574b19f
Update FileConfigManagerTest remove a redundant helpers section comment.
kudashevs 6c15b77
Style IgnitionConfig fix redundant new line.
kudashevs 1b71a91
Refactor IgnitionConfig extract a check manager in a container method.
kudashevs e4bffa1
Refactor IgnitionConfig invert the logic of the check in container me…
kudashevs 56dfef5
Style FileConfigManager add a warning comment to the isValidPath method.
kudashevs 5959c30
Style FileConfigManager add a warning comment to the isValidPath method.
kudashevs 8262647
Update FileConfigManager remove realpaht() invocation in the isValidP…
kudashevs e073859
Style FileConfigManager add inheritdoc tag to some methods.
kudashevs 5d8d288
Refactor FileConfigManager change the visibility of the initPath method.
kudashevs ef79cd6
Update FileConfigManager change the import of Throwable to the explic…
kudashevs a2477c1
Update IgnitionConfigTest remove a redundant helpers section comment.
kudashevs 957714d
Fix IgnitionConfigTest fix a usage of the path variable in the app he…
kudashevs 4ee239d
Update IgnitionConfig remove no longer used createSource() call from …
kudashevs 9fea4cf
Style IgnitionConfig remove a redundant new line in the saveValues do…
kudashevs a497ccd
Update FileConfigManager add a created state field.
kudashevs 3cff76e
Update FileConfigManager add a new guard clause to the load method.
kudashevs eae30c3
Update FileConfigManager extract a readFromFile method.
kudashevs 1308e07
Refactor FileConfigManager rename options variable in the readFromFil…
kudashevs 874eb5d
Update FileConfigManager extract a isFileCreated method.
kudashevs 14df3a0
Update FileConfigManager add a new guard clause to the save method.
kudashevs 8b6fb09
Refactor FileConfigManager extract a saveToFile method.
kudashevs 77431fd
Update FileConfigManager make more strict the behavior of the json_en…
kudashevs abdeb22
Refactor FileConfigManager reorder class methods.
kudashevs c8d6e2f
Refactor FileConfigManager extract a createFile method.
kudashevs f35ebd0
Refactor FileConfigManager change the created field to a file field.
kudashevs 21f1b94
Update FileConfigManager update the isFileCreated method.
kudashevs 59628fc
Update FileConfigManagerTest add check it cannot create file when a w…
kudashevs 639f7d6
Refactor FileConfigManager remove exceeding generateFullFilePath call…
kudashevs 118549d
Refactor FileConfigManager remove no longer used parameter in the isV…
kudashevs 2379fa9
Refactor FileConfigManager switch to use the file field in some methods.
kudashevs 008e646
Fix FileConfigManager fix the wrong creatinal logic in the createSour…
kudashevs 6d24482
Update FileConfigManager add a formatting of a path to the generateFu…
kudashevs 4f030e3
Refactor FileConfigManager move the guard clause from load to the rea…
kudashevs 6d42df2
Refactor FileConfigManager move the guard clause from save to the sav…
kudashevs 2299d55
Update FileConfigManager remove formatting of a path from the generat…
kudashevs 6eb88a7
Update FileConfigManager add a formatting to a return of the initPath…
kudashevs 5dfbbcb
Update FileConfigManagerTest add check it cannot create file when an …
kudashevs 9284fb3
Update FileConfigManager add a guard clause for empty path in the cre…
kudashevs dfec1e7
Refactor FileConfigManagerTest remove no longer used the createSource…
kudashevs 1f71719
Refactor ConfigManager rename the createSource to the createPersisten…
kudashevs e2e63fa
Refactor ConfigManager rename the getSource to the getPersistentInfo …
kudashevs 92caef5
Refactor ConfigManager reorder interface method signatures.
kudashevs f4ceb0c
Refactor FileConfigManager reorder class methods.
kudashevs c566033
Update IgnitionConfig add more checks to the isInContainer method.
kudashevs ac37f0b
Refactor FileConfigManager rename isValidFile to isFileValid method.
kudashevs 2f710c6
Refactor FileConfigManager change the visibility of the initPathFromE…
kudashevs d7025fe
Refactor FileConfigManager reorder class methods.
kudashevs 672616e
Refactor FileConfigManager change the visibility of the preparePath m…
kudashevs 0246027
Update IgnitionConfigTest update a title of the check retrieve from f…
kudashevs 3315661
Update IgnitionConfig remove the isInContainer method.
kudashevs fcd27af
Update IgnitionConfig change the container is available check in the …
kudashevs b5a73c3
Refactor FileConfigManager rename the generateFullFilePath method.
kudashevs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
||
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 theConfigManager
, etc...There was a problem hiding this comment.
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 termsettings file
. That's why I would like to stick to this namesettings 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.