Skip to content

Commit

Permalink
Move from static method to global function
Browse files Browse the repository at this point in the history
  • Loading branch information
Korbeil committed Jun 23, 2023
1 parent 32c1b76 commit 1b60b8b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion doc/05-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ use Castor\Context;
#[AsContext]
function my_context(): Context
{
return new Context::fromDotEnv();
return new Context(load_dot_env());
}
```

Expand Down
5 changes: 4 additions & 1 deletion examples/context.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use function Castor\get_context;
use function Castor\io;
use function Castor\load_dot_env;
use function Castor\run;
use function Castor\variable;

Expand Down Expand Up @@ -65,7 +66,9 @@ function interactiveContext(): Context
#[AsContext(name: 'path')]
function contextFromPath(): Context
{
return Context::fromDotEnv(__DIR__ . '/dotenv-context/.env');
$data = load_dot_env(__DIR__ . '/dotenv-context/.env');

return new Context($data);

Check failure on line 71 in examples/context.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $data of class Castor\Context constructor expects array{name: string, production: bool, foo?: string}, array given.
}

#[AsTask(description: 'Displays information about the context')]
Expand Down
13 changes: 0 additions & 13 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Castor;

use Symfony\Component\Dotenv\Dotenv;

class Context implements \ArrayAccess
{
public readonly string $currentDirectory;
Expand Down Expand Up @@ -213,15 +211,4 @@ public function offsetUnset(mixed $offset): void
{
throw new \LogicException('Context is immutable');
}

public static function fromDotEnv(string $path = null): self
{
$path ??= PathHelper::getRoot() . '/.env';

$dotenv = new Dotenv();
$dotenv->loadEnv($path);
unset($_ENV['SYMFONY_DOTENV_VARS']);

return new self(environment: $_ENV);
}
}
12 changes: 12 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Exception\ProcessFailedException;
Expand Down Expand Up @@ -474,3 +475,14 @@ function fix_exception(\Exception $exception): \Exception

return $exception;
}

function load_dot_env(string $path = null): array

Check failure on line 479 in src/functions.php

View workflow job for this annotation

GitHub Actions / PHPStan

Function Castor\load_dot_env() return type has no value type specified in iterable type array.
{
$path ??= PathHelper::getRoot() . '/.env';

$dotenv = new Dotenv();
$dotenv->loadEnv($path);
unset($_ENV['SYMFONY_DOTENV_VARS']);

return $_ENV;
}

0 comments on commit 1b60b8b

Please sign in to comment.