Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/AppBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static function build(string|null $path = null, string|null $env = null):
{
$app = new App($path ?? dirname(__DIR__));

Environment::load($env);
Environment::load('.env', $env);

putenv('PHENIX_BASE_PATH=' . base_path());
$_ENV['PHENIX_BASE_PATH'] = base_path();
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/ViewCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->compile(Config::get('view.path'));

WorkerPool::batch($this->tasks);
WorkerPool::awaitAll($this->tasks);

$output->writeln('<info>All views were compiled successfully!.</info>');

Expand Down
29 changes: 14 additions & 15 deletions src/Crypto/Crypto.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Phenix\Crypto\Tasks\Decrypt;
use Phenix\Crypto\Tasks\Encrypt;
use Phenix\Tasks\Result;
use Phenix\Tasks\Worker;
use SensitiveParameter;

class Crypto implements CipherContract, StringCipher
Expand All @@ -26,14 +25,14 @@ public function __construct(

public function encrypt(#[SensitiveParameter] object|array|string $value, bool $serialize = true): string
{
$task = new Encrypt(
key: $this->key,
value: $value,
serialize: $serialize
);

/** @var Result $result */
[$result] = Worker::batch([
new Encrypt(
key: $this->key,
value: $value,
serialize: $serialize
),
]);
$result = $task->output();

if ($result->isFailure()) {
throw new EncryptException($result->message());
Expand All @@ -49,14 +48,14 @@ public function encryptString(#[SensitiveParameter] string $value): string

public function decrypt(string $payload, bool $unserialize = true): object|array|string
{
$task = new Decrypt(
key: $this->key,
value: $payload,
unserialize: $unserialize
);

/** @var Result $result */
[$result] = Worker::batch([
new Decrypt(
key: $this->key,
value: $payload,
unserialize: $unserialize
),
]);
$result = $task->output();

if ($result->isFailure()) {
throw new DecryptException($result->message());
Expand Down
19 changes: 9 additions & 10 deletions src/Crypto/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,36 @@
use Phenix\Crypto\Tasks\GeneratePasswordHash;
use Phenix\Crypto\Tasks\VerifyPasswordHash;
use Phenix\Tasks\Result;
use Phenix\Tasks\Worker;
use SensitiveParameter;

class Hash implements HasherContract
{
public function make(#[SensitiveParameter] string $password): string
{
$task = new GeneratePasswordHash($password);

/** @var Result $result */
[$result] = Worker::batch([
new GeneratePasswordHash($password),
]);
$result = $task->output();

return $result->output();
}

public function verify(string $hash, #[SensitiveParameter] string $password): bool
{
$task = new VerifyPasswordHash($hash, $password);

/** @var Result $result */
[$result] = Worker::batch([
new VerifyPasswordHash($hash, $password),
]);
$result = $task->output();

return $result->output();
}

public function needsRehash(string $hash): bool
{
$task = new CheckNeedsRehash($hash);

/** @var Result $result */
[$result] = Worker::batch([
new CheckNeedsRehash($hash),
]);
$result = $task->output();

return $result->output();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Facades/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Phenix\Facades;

use Amp\Future;
use Phenix\Mail\Constants\MailerType;
use Phenix\Mail\Contracts\Mailable as MailableContract;
use Phenix\Mail\Contracts\Mailer;
Expand All @@ -15,7 +16,7 @@
* @method static Mailer mailer(MailerType|null $mailerType = null)
* @method static Mailer using(MailerType $mailerType)
* @method static Mailer to(array|string $to)
* @method static void send(MailableContract $mailable)
* @method static Future send(MailableContract $mailable)
* @method static Mailer fake(MailerType|null $mailerType = null)
* @method static array getSendingLog(MailerType|null $mailerType = null)
* @method static void resetSendingLog(MailerType|null $mailerType = null)
Expand Down
35 changes: 35 additions & 0 deletions src/Facades/Worker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Phenix\Facades;

use Amp\Cancellation;
use Amp\Parallel\Worker\Execution;
use Amp\Parallel\Worker\Task;
use Amp\Parallel\Worker\Worker as ParallelWorker;
use Phenix\Runtime\Facade;

/**
* Worker Pool Facade
*
* @method static bool isRunning()
* @method static bool isIdle()
* @method static int getWorkerLimit()
* @method static int getLimit()
* @method static int getWorkerCount()
* @method static int getIdleWorkerCount()
* @method static Execution submit(Task $task, Cancellation|null $cancellation = null)
* @method static void shutdown()
* @method static void kill()
* @method static ParallelWorker getWorker()
*
* @see \Amp\Parallel\Worker\WorkerPool
*/
class Worker extends Facade
{
public static function getKeyName(): string
{
return \Amp\Parallel\Worker\WorkerPool::class;
}
}
3 changes: 2 additions & 1 deletion src/Mail/Contracts/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Phenix\Mail\Contracts;

use Amp\Future;
use Phenix\Mail\Mailable;

interface Mailer
Expand All @@ -14,7 +15,7 @@ public function cc(array|string $cc): self;

public function bcc(array|string $bcc): self;

public function send(Mailable $mailable): void;
public function send(Mailable $mailable): Future;

public function getSendingLog(): array;

Expand Down
5 changes: 3 additions & 2 deletions src/Mail/MailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Phenix\Mail;

use Amp\Future;
use Phenix\Mail\Constants\MailerType;
use Phenix\Mail\Contracts\Mailer as MailerContract;
use Phenix\Mail\Mailers\Resend;
Expand Down Expand Up @@ -44,9 +45,9 @@ public function to(array|string $to): MailerContract
return $this->mailer()->to($to);
}

public function send(Mailable $mailable): void
public function send(Mailable $mailable): Future
{
$this->mailer()->send($mailable);
return $this->mailer()->send($mailable);
}

public function fake(MailerType|null $mailerType = null): void
Expand Down
16 changes: 8 additions & 8 deletions src/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Phenix\Mail;

use Amp\Future;
use Phenix\Mail\Contracts\Mailable;
use Phenix\Mail\Contracts\Mailer as MailerContract;
use Phenix\Mail\Tasks\SendEmail;
use Phenix\Tasks\Result;
use Phenix\Tasks\Worker;
use Phenix\Tasks\WorkerPool;
use SensitiveParameter;
use Symfony\Component\Mime\Address;

Expand Down Expand Up @@ -57,7 +57,7 @@ public function bcc(array|string $bcc): self
return $this;
}

public function send(Mailable $mailable): void
public function send(Mailable $mailable): Future
{
$mailable->from($this->from)
->to($this->to)
Expand All @@ -67,22 +67,22 @@ public function send(Mailable $mailable): void

$email = $mailable->toMail();

/** @var Result $result */
[$result] = Worker::batch([
$future = WorkerPool::submit(
new SendEmail(
$email,
$this->config,
$this->serviceConfig,
),
]);
)
);

if ($this->config['transport'] === 'log') {
$this->sendingLog[] = [
'mailable' => $mailable::class,
'email' => $email,
'success' => $result->isSuccess(),
];
}

return $future;
}

public function getSendingLog(): array
Expand Down
5 changes: 3 additions & 2 deletions src/Mail/TransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use InvalidArgumentException;
use Phenix\Mail\Constants\MailerType;
use Phenix\Mail\Transports\LogTransport;
use Phenix\Util\Arr;
use SensitiveParameter;
use Symfony\Component\Mailer\Bridge\Amazon\Transport\SesSmtpTransport;
use Symfony\Component\Mailer\Bridge\Resend\Transport\ResendApiTransport;
Expand Down Expand Up @@ -34,15 +35,15 @@ private static function createSmtpTransport(#[SensitiveParameter] array $config)
$scheme = 'smtp';

if (! empty($config['encryption']) && $config['encryption'] === 'tls') {
$scheme = ($config['port'] === 465) ? 'smtps' : 'smtp';
$scheme = (Arr::get($config, 'port') === 465) ? 'smtps' : 'smtp';
}

$dsn = new Dsn(
$scheme,
$config['host'],
$config['username'] ?? null,
$config['password'] ?? null,
$config['port'] ?? null,
Arr::has($config, 'port') ? (int) Arr::get($config, 'port') : null,
$config
);

Expand Down
3 changes: 2 additions & 1 deletion src/Runtime/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
namespace Phenix\Runtime;

use Dotenv\Dotenv;
use Phenix\Util\Str;

class Environment
{
public static function load(string|null $fileName = null, string|null $environment = null): void
{
$fileName ??= '.env';
$fileName .= $environment ? ".{$environment}" : '';
$fileNamePath = base_path() . DIRECTORY_SEPARATOR . $fileName;
$fileNamePath = Str::finish(base_path(), DIRECTORY_SEPARATOR) . $fileName;

if (file_exists($fileNamePath)) {
Dotenv::createImmutable(base_path(), $fileName)->load();
Expand Down
10 changes: 5 additions & 5 deletions src/Tasks/AbstractWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public function __construct()
* @param array<int, Task> $tasks
* @return array<int, Result>
*/
public static function batch(array $tasks): array
public static function awaitAll(array $tasks): array
{
$pool = new static();

foreach ($tasks as $task) {
$pool->submit($task);
$pool->push($task);
}

$results = $pool->run();
Expand All @@ -41,9 +41,9 @@ public static function batch(array $tasks): array
return $results;
}

public function submit(Task $parallelTask): self
public function push(Task $parallelTask): self
{
$this->tasks[] = $this->submitTask($parallelTask);
$this->tasks[] = $this->prepareTask($parallelTask);

return $this;
}
Expand All @@ -56,7 +56,7 @@ public function run(): array
));
}

abstract protected function submitTask(Task $parallelTask): Worker\Execution;
abstract protected function prepareTask(Task $parallelTask): Worker\Execution;

protected function finalize(): void
{
Expand Down
4 changes: 2 additions & 2 deletions src/Tasks/Contracts/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

interface Worker
{
public function submit(Task $parallelTask): self;
public function push(Task $parallelTask): self;

public function run(): array;

/**
* @param Task[] $tasks
* @return array
*/
public static function batch(array $tasks): array;
public static function awaitAll(array $tasks): array;
}
7 changes: 1 addition & 6 deletions src/Tasks/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ public function run(Channel $channel, Cancellation $cancellation): mixed

public function output(): Result
{
/** @var Result $result */
[$result] = Worker::batch([
$this,
]);

return $result;
return WorkerPool::submit($this)->await();
}

public function setTimeout(int $timeout): void
Expand Down
2 changes: 1 addition & 1 deletion src/Tasks/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct()
$this->worker = Workers\createWorker();
}

protected function submitTask(Task $parallelTask): Workers\Execution
protected function prepareTask(Task $parallelTask): Workers\Execution
{
$timeout = new TimeoutCancellation($parallelTask->getTimeout());

Expand Down
Loading