Skip to content

Commit 6f6d073

Browse files
committed
Various cleanup and code review
1 parent 7c9c63b commit 6f6d073

9 files changed

Lines changed: 364 additions & 266 deletions

File tree

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,31 @@
1-
<?php namespace Winter\Storm\Config;
1+
<?php namespace Winter\Storm\Parse\Contracts;
22

3-
// Returns are commented out as improved type variance is not supported until php 7.4
4-
// Ref: https://stitcher.io/blog/new-in-php-74#improved-type-variance-rfc
5-
// TODO: enable return types once support for <7.4 is dropped
6-
7-
/**
8-
* Interface ConfigFileInterface
9-
* @package Winter\Storm\Config
10-
*/
11-
interface ConfigFileInterface
3+
interface FileInterface
124
{
135
/**
14-
* Return a new instance of `ConfigFileInterface` ready for modification of the file.
6+
* Return a new instance of `FileInterface` ready for modification of the file.
157
*
168
* @param string $file
17-
* @return ConfigFile|null
189
*/
19-
public static function read(string $file); //: ?ConfigFileInterface;
10+
public static function read(string $file): ?FileInterface;
2011

2112
/**
2213
* Set a property within the config. Passing an array as param 1 is also supported.
14+
*
2315
* @param string|array $key
2416
* @param mixed|null $value
25-
* @return $this
2617
*/
27-
public function set($key, $value = null); //: ConfigFileInterface;
18+
public function set($key, $value = null): FileInterface;
2819

2920
/**
3021
* Write the current config to a file
3122
*
3223
* @param string|null $filePath
33-
* @return void
3424
*/
3525
public function write(string $filePath = null): void;
3626

3727
/**
3828
* Get the printed config
39-
*
40-
* @return string
4129
*/
4230
public function render(): string;
4331
}

src/Parse/EnvFile.php

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
<?php namespace Winter\Storm\Config;
1+
<?php namespace Winter\Storm\Parse;
22

3-
use Winter\Storm\Config\ConfigFileInterface;
4-
use Dotenv\Environment\DotenvFactory;
5-
use Dotenv\Loader;
3+
use Winter\Storm\Parse\Contracts\FileInterface;
64

75
/**
86
* Class EnvFile
9-
* @package Winter\Storm\Config
107
*/
11-
class EnvFile implements ConfigFileInterface
8+
class EnvFile implements FileInterface
129
{
1310
/**
1411
* @var array contains the env during modification
@@ -23,33 +20,30 @@ class EnvFile implements ConfigFileInterface
2320
/**
2421
* @var string|null contains the filepath used to read / write
2522
*/
26-
protected $file = null;
23+
protected $filePath = null;
2724

2825
/**
2926
* EnvFile constructor.
3027
* @param array $env
31-
* @param string $file
28+
* @param string $filePath
3229
*/
33-
public function __construct(string $file)
30+
public function __construct(string $filePath)
3431
{
35-
$this->file = $file;
32+
$this->filePath = $filePath;
3633

37-
list($this->env, $this->map) = $this->parse($file);
34+
list($this->env, $this->map) = $this->parse($filePath);
3835
}
3936

4037
/**
4138
* Return a new instance of `EnvFile` ready for modification of the file.
42-
*
43-
* @param string|null $file
44-
* @return EnvFile|null
4539
*/
46-
public static function read(?string $file = null): ?EnvFile
40+
public static function read(?string $filePath = null): ?EnvFile
4741
{
48-
if (!$file) {
49-
$file = static::getEnvFilePath();
42+
if (!$filePath) {
43+
$filePath = base_path('.env');
5044
}
5145

52-
return new static($file);
46+
return new static($filePath);
5347
}
5448

5549
/**
@@ -115,7 +109,7 @@ public function addNewLine(): EnvFile
115109
public function write(string $filePath = null): void
116110
{
117111
if (!$filePath) {
118-
$filePath = $this->file;
112+
$filePath = $this->filePath;
119113
}
120114

121115
file_put_contents($filePath, $this->render());
@@ -183,12 +177,12 @@ protected function escapeValue($value): string
183177
/**
184178
* Parse a .env file, returns an array of the env file data and a key => pos map
185179
*
186-
* @param string $file
180+
* @param string $filePath
187181
* @return array
188182
*/
189-
protected function parse(string $file): array
183+
protected function parse(string $filePath): array
190184
{
191-
if (!file_exists($file) || !($contents = file($file)) || !count($contents)) {
185+
if (!file_exists($filePath) || !($contents = file($filePath)) || !count($contents)) {
192186
return [[], []];
193187
}
194188

@@ -255,14 +249,4 @@ public function getEnv(): array
255249

256250
return $env;
257251
}
258-
259-
/**
260-
* Get the default env file path
261-
*
262-
* @return string
263-
*/
264-
public static function getEnvFilePath(): string
265-
{
266-
return base_path('.env');
267-
}
268252
}

src/Parse/PHP/ArrayFile.php

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php namespace Winter\Storm\Config;
1+
<?php namespace Winter\Storm\Parse\PHP;
22

33
use PhpParser\Error;
44
use PhpParser\Node\Expr\Array_;
@@ -24,43 +24,41 @@ class ArrayFile implements FileInterface
2424
* @var Stmt[]|null Abstract syntax tree produced by `PhpParser`
2525
*/
2626
protected $ast = null;
27+
2728
/**
28-
* @var string|null Source config file
29+
* @var string|null Path to the file
2930
*/
30-
protected $file = null;
31+
protected $filePath = null;
32+
3133
/**
32-
* @var PrettyPrinterAbstract|WinterPrinter|null Printer used to define output syntax
34+
* @var PrettyPrinterAbstract|ArrayPrinter|null Printer used to define output syntax
3335
*/
3436
protected $printer = null;
3537

3638
/**
37-
* ConfigFile constructor.
38-
*
39-
* @param Stmt[]|null $ast
40-
* @param string $file
41-
* @param PrettyPrinterAbstract|null $printer
39+
* ArrayFile constructor.
4240
*/
43-
public function __construct(array $ast, string $file = null, PrettyPrinterAbstract $printer = null)
41+
public function __construct(array $ast, string $filePath = null, PrettyPrinterAbstract $printer = null)
4442
{
4543
if (!($ast[0] instanceof Stmt\Return_)) {
46-
throw new \InvalidArgumentException('configs must start with a return statement');
44+
throw new \InvalidArgumentException('ArrayFiles must start with a return statement');
4745
}
4846

4947
$this->ast = $ast;
50-
$this->file = $file;
51-
$this->printer = $printer ?? new WinterPrinter();
48+
$this->filePath = $filePath;
49+
$this->printer = $printer ?? new ArrayPrinter();
5250
}
5351

5452
/**
55-
* Return a new instance of `ConfigFile` ready for modification of the file.
53+
* Return a new instance of `ArrayFile` ready for modification of the file.
5654
*
57-
* @param string $file
55+
* @param string $filePath
5856
* @param bool $createMissing
59-
* @return ConfigFile|null
57+
* @return ArrayFile|null
6058
*/
61-
public static function read(string $file, bool $createMissing = false): ?ConfigFile
59+
public static function read(string $filePath, bool $createMissing = false): ?ArrayFile
6260
{
63-
$exists = file_exists($file);
61+
$exists = file_exists($filePath);
6462

6563
if (!$exists && !$createMissing) {
6664
throw new \InvalidArgumentException('file not found');
@@ -71,14 +69,14 @@ public static function read(string $file, bool $createMissing = false): ?ConfigF
7169
try {
7270
$ast = $parser->parse(
7371
$exists
74-
? file_get_contents($file)
72+
? file_get_contents($filePath)
7573
: sprintf('<?php%1$s%1$sreturn [];%1$s', "\n")
7674
);
7775
} catch (Error $e) {
7876
throw new SystemException($e);
7977
}
8078

81-
return new static($ast, $file);
79+
return new static($ast, $filePath);
8280
}
8381

8482
/**
@@ -97,7 +95,7 @@ public static function read(string $file, bool $createMissing = false): ?ConfigF
9795
* @param mixed|null $value
9896
* @return $this
9997
*/
100-
public function set($key, $value = null): ConfigFile
98+
public function set($key, $value = null): ArrayFile
10199
{
102100
if (is_array($key)) {
103101
foreach ($key as $name => $value) {
@@ -237,11 +235,11 @@ protected function castArray(array $array): Array_
237235
*/
238236
protected function getType($var): string
239237
{
240-
if ($var instanceof ConfigFunction) {
238+
if ($var instanceof PHPFunction) {
241239
return 'function';
242240
}
243241

244-
if ($var instanceof ConfigConst) {
242+
if ($var instanceof PHPConst) {
245243
return 'const';
246244
}
247245

@@ -318,12 +316,11 @@ protected function seek(array $path, &$pointer, int $depth = 0): array
318316
}
319317

320318
/**
321-
* Sort the config, supports: ConfigFile::SORT_ASC, ConfigFile::SORT_DESC, callable
319+
* Sort the config, supports: ArrayFile::SORT_ASC, ArrayFile::SORT_DESC, callable
322320
*
323321
* @param string|callable $mode
324-
* @return ConfigFile
325322
*/
326-
public function sort($mode = self::SORT_ASC): ConfigFile
323+
public function sort($mode = self::SORT_ASC): ArrayFile
327324
{
328325
if (is_callable($mode)) {
329326
usort($this->ast[0]->expr->items, $mode);
@@ -372,34 +369,34 @@ protected function sortRecursive(array &$array, string $mode): void
372369
*/
373370
public function write(string $filePath = null): void
374371
{
375-
if (!$filePath && $this->file) {
376-
$filePath = $this->file;
372+
if (!$filePath && $this->filePath) {
373+
$filePath = $this->filePath;
377374
}
378375

379376
file_put_contents($filePath, $this->render());
380377
}
381378

382379
/**
383-
* Returns a new instance of ConfigFunction
380+
* Returns a new instance of PHPFunction
384381
*
385382
* @param string $name
386383
* @param array $args
387-
* @return ConfigFunction
384+
* @return PHPFunction
388385
*/
389-
public function function(string $name, array $args): ConfigFunction
386+
public function function(string $name, array $args): PHPFunction
390387
{
391-
return new ConfigFunction($name, $args);
388+
return new PHPFunction($name, $args);
392389
}
393390

394391
/**
395-
* Returns a new instance of ConfigConst
392+
* Returns a new instance of PHPConst
396393
*
397394
* @param string $name
398-
* @return ConfigConst
395+
* @return PHPConst
399396
*/
400-
public function const(string $name): ConfigConst
397+
public function const(string $name): PHPConst
401398
{
402-
return new ConfigConst($name);
399+
return new PHPConst($name);
403400
}
404401

405402
/**

src/Parse/PHP/ArrayPrinter.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
<?php namespace Winter\Storm\Config;
1+
<?php namespace Winter\Storm\Parse\PHP;
22

33
use PhpParser\Node\Expr\ArrayItem;
44
use PhpParser\PrettyPrinter\Standard;
55

6-
/**
7-
* Class WinterPrinter
8-
* @package Winter\Storm\Config
9-
*/
10-
class WinterPrinter extends Standard
6+
class ArrayPrinter extends Standard
117
{
128
public function __construct(array $options = [])
139
{
@@ -32,10 +28,6 @@ protected function pMaybeMultiline(array $nodes, bool $trailingComma = false)
3228
}
3329
}
3430

35-
/**
36-
* @param array $comments
37-
* @return string
38-
*/
3931
protected function pComments(array $comments): string
4032
{
4133
$formattedComments = [];
Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
1-
<?php namespace Winter\Storm\Config;
1+
<?php namespace Winter\Storm\Parse\PHP;
22

33
/**
4-
* Class ConfigConst
5-
* @package Winter\Storm\Config
6-
*
7-
* This class is for use with ConfigFile as a method to inject a constant into a config file
4+
* Used with ArrayFile to inject a constant into a PHP array file
85
*/
9-
class ConfigConst
6+
class PHPConst
107
{
118
/**
129
* @var string function name
1310
*/
1411
protected $name;
1512

16-
/**
17-
* @param string $name
18-
*/
1913
public function __construct(string $name)
2014
{
2115
$this->name = $name;
2216
}
2317

2418
/**
2519
* Get the const name
26-
*
27-
* @return string
2820
*/
2921
public function getName(): string
3022
{

src/Parse/PHP/PHPFunction.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
<?php namespace Winter\Storm\Config;
1+
<?php namespace Winter\Storm\Parse\PHP;
22

33
/**
4-
* Class ConfigFunction
5-
* @package Winter\Storm\Config
6-
*
7-
* This class is for use with ConfigFile as a method to inject a function call into a config file
4+
* Used with ArrayFile to inject a function call into a PHP array file
85
*/
9-
class ConfigFunction
6+
class PHPFunction
107
{
118
/**
129
* @var string function name

0 commit comments

Comments
 (0)