generated from chevere/package-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '0.9' of github.com:chevere/workflow into 0.9
- Loading branch information
Showing
7 changed files
with
225 additions
and
23 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
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,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Chevere. | ||
* | ||
* (c) Rodolfo Berrios <rodolfo@chevere.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Chevere\Demo\Actions; | ||
|
||
use Chevere\Action\Action; | ||
use RuntimeException; | ||
|
||
class FetchUrl extends Action | ||
{ | ||
protected function main(string $url): string | ||
{ | ||
$content = file_get_contents($url); | ||
if ($content === false) { | ||
throw new RuntimeException('Error fetching URL'); | ||
} | ||
|
||
return $content; | ||
} | ||
} |
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,66 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Chevere. | ||
* | ||
* (c) Rodolfo Berrios <rodolfo@chevere.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
use Chevere\Demo\Actions\FetchUrl; | ||
use function Chevere\Workflow\async; | ||
use function Chevere\Workflow\run; | ||
use function Chevere\Workflow\sync; | ||
use function Chevere\Workflow\variable; | ||
use function Chevere\Workflow\workflow; | ||
|
||
require 'loader.php'; | ||
|
||
/* | ||
php demo/sync-vs-async.php | ||
*/ | ||
$sync = workflow( | ||
php: sync( | ||
new FetchUrl(), | ||
url: variable('php'), | ||
), | ||
github: sync( | ||
new FetchUrl(), | ||
url: variable('github'), | ||
), | ||
chevere: sync( | ||
new FetchUrl(), | ||
url: variable('chevere'), | ||
), | ||
); | ||
$async = workflow( | ||
php: async( | ||
new FetchUrl(), | ||
url: variable('php'), | ||
), | ||
github: async( | ||
new FetchUrl(), | ||
url: variable('github'), | ||
), | ||
chevere: async( | ||
new FetchUrl(), | ||
url: variable('chevere'), | ||
), | ||
); | ||
$variables = [ | ||
'php' => 'https://www.php.net', | ||
'github' => 'https://github.com/chevere/workflow', | ||
'chevere' => 'https://chevere.org', | ||
]; | ||
$time = microtime(true); | ||
$run = run($sync, ...$variables); | ||
$time = microtime(true) - $time; | ||
echo "Time sync: {$time}\n"; | ||
$time = microtime(true); | ||
$run = run($async, ...$variables); | ||
$time = microtime(true) - $time; | ||
echo "Time async: {$time}\n"; |
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,45 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Chevere. | ||
* | ||
* (c) Rodolfo Berrios <rodolfo@chevere.org> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Chevere\Workflow; | ||
|
||
use Amp\Cancellation; | ||
use Amp\Parallel\Worker\Task; | ||
use Amp\Sync\Channel; | ||
|
||
/** | ||
* @template-implements Task<mixed, never, never> | ||
*/ | ||
final class CallableTask implements Task | ||
{ | ||
private string $callable; | ||
|
||
/** | ||
* @var array<mixed> | ||
*/ | ||
private array $arguments; | ||
|
||
public function __construct( | ||
string $callable, | ||
mixed ...$arguments | ||
) { | ||
$this->callable = $callable; | ||
$this->arguments = $arguments; | ||
} | ||
|
||
public function run(Channel $channel, Cancellation $cancellation): mixed | ||
{ | ||
// @phpstan-ignore-next-line | ||
return ($this->callable)(...$this->arguments); | ||
} | ||
} |
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