-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
182 additions
and
39 deletions.
There are no files selected for viewing
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,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap; | ||
|
||
use Buggregator\Trap\Service\Config\ConfigLoader; | ||
use Buggregator\Trap\Service\Container; | ||
|
||
/** | ||
* Build the container based on the configuration. | ||
* | ||
* @internal | ||
*/ | ||
final class Bootstrap | ||
{ | ||
private function __construct( | ||
private Container $container, | ||
) { | ||
} | ||
|
||
public static function init(Container $container = new Container()): self | ||
{ | ||
return new self($container); | ||
} | ||
|
||
public function finish(): Container | ||
{ | ||
$c = $this->container; | ||
unset($this->container); | ||
|
||
return $c; | ||
} | ||
|
||
/** | ||
* @param non-empty-string|null $xml File or XML content | ||
*/ | ||
public function withConfig( | ||
?string $xml = null, | ||
array $inputOptions = [], | ||
array $inputArguments = [], | ||
array $environment = [], | ||
): self { | ||
$args = [ | ||
'env' => $environment, | ||
'inputArguments' => $inputArguments, | ||
'inputOptions' => $inputOptions, | ||
]; | ||
|
||
// XML config file | ||
$xml === null or $args['xml'] = $this->readXml($xml); | ||
|
||
// Register bindings | ||
$this->container->bind(ConfigLoader::class, $args); | ||
|
||
return $this; | ||
} | ||
|
||
private function readXml(string $fileOrContent): string | ||
{ | ||
// Load content | ||
if (\str_starts_with($fileOrContent, '<?xml')) { | ||
$xml = $fileOrContent; | ||
} else { | ||
\file_exists($fileOrContent) or throw new \InvalidArgumentException('Config file not found.'); | ||
$xml = \file_get_contents($fileOrContent); | ||
$xml === false and throw new \RuntimeException('Failed to read config file.'); | ||
} | ||
|
||
// Validate Schema | ||
// todo | ||
|
||
return $xml; | ||
} | ||
} |
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
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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap\Service\Config; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY)] | ||
final class InputArgument implements ConfigAttribute | ||
{ | ||
public function __construct( | ||
public string $name, | ||
) { | ||
} | ||
} |
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
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