From 67dc4b470880711108f8bc45dd777a2fc78110f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFck=20Piera?= Date: Tue, 12 Mar 2024 23:18:08 +0100 Subject: [PATCH] Avoid suggesting echo in documentation and examples --- README.md | 4 ++-- doc/getting-started/arguments.md | 20 ++++++++--------- doc/getting-started/basic-usage.md | 14 ++++++++---- doc/getting-started/context.md | 6 +++-- doc/getting-started/run.md | 13 ++++++----- doc/going-further/helpers/cache.md | 10 ++++++--- doc/going-further/helpers/filesystem.md | 12 +++++----- doc/going-further/helpers/fingerprint.md | 8 +++---- doc/going-further/helpers/http-request.md | 3 ++- doc/going-further/helpers/parallel.md | 5 +++-- doc/going-further/helpers/watch.md | 15 ++++++++----- .../advanced-context.md | 9 +++++--- .../interacting-with-castor/dot-env.md | 2 +- .../interacting-with-castor/signals.md | 6 +++-- doc/index.md | 4 ++-- examples/args.php | 4 ++-- examples/bar.php | 3 ++- examples/cache.php | 9 ++++---- examples/context.php | 14 ++++++------ examples/enabled.php | 4 +++- examples/filesystem.php | 11 +++++----- examples/fingerprint.php | 22 +++++++++---------- examples/foo.php | 6 +++-- examples/http.php | 3 ++- examples/parallel.php | 13 ++++++----- examples/rename.php | 6 +++-- examples/run.php | 17 +++++++------- examples/signal.php | 4 +++- examples/watch.php | 17 +++++++------- examples/yaml.php | 5 +++-- .../ContextContextWithTest.php.output.txt | 2 +- .../Generated/FooBarTest.php.output.txt | 2 +- .../Generated/HttpRequestTest.php.output.txt | 2 +- .../ParallelExceptionTest.php.err.txt | 4 ++-- .../Generated/RunVariablesTest.php.output.txt | 4 +--- 35 files changed, 161 insertions(+), 122 deletions(-) diff --git a/README.md b/README.md index 61d9c652..86313a02 100644 --- a/README.md +++ b/README.md @@ -41,12 +41,12 @@ For example: namespace greetings; use Castor\Attribute\AsTask; -use function Castor\run; +use function Castor\io; #[AsTask()] function hello(): void { - run('echo "Hello from castor"'); + io()->writeln('Hello from castor'); } ``` diff --git a/doc/getting-started/arguments.md b/doc/getting-started/arguments.md index 6605dc0a..26c61e85 100644 --- a/doc/getting-started/arguments.md +++ b/doc/getting-started/arguments.md @@ -6,14 +6,14 @@ the function will be used as arguments or options: ```php use Castor\Attribute\AsTask; -use function Castor\run; +use function Castor\io; #[AsTask()] function task( string $firstArg, string $secondArg ) { - run(['echo', $firstArg, $secondArg]); + io()->writeln($firstArg . ' ' . $secondArg); } ``` @@ -31,14 +31,14 @@ You can make an argument optional by giving it a default value: ```php use Castor\Attribute\AsTask; -use function Castor\run; +use function Castor\io; #[AsTask()] function task( string $firstArg, - string $default = 'default' + string $secondArg = 'default' ) { - run(['echo', $firstArg, $secondArg]); + io()->writeln($firstArg . ' ' . $secondArg); } ``` @@ -80,14 +80,14 @@ the `Castor\Attribute\AsArgument` attribute: use Castor\Attribute\AsArgument; use Castor\Attribute\AsTask; -use function Castor\run; +use function Castor\io; #[AsTask()] function command( #[AsArgument(name: 'foo', description: 'This is the foo argument')] string $arg = 'bar', ) { - run(['echo', $arg]); + io()->writeln($arg); } ``` @@ -108,14 +108,14 @@ If you prefer, you can force an argument to be an option by using the use Castor\Attribute\AsOption; use Castor\Attribute\AsTask; -use function Castor\run; +use function Castor\io; #[AsTask()] function command( #[AsOption(name: 'foo', description: 'This is the foo option')] string $arg = 'bar', ) { - run(['echo', $arg]); + io()->writeln($arg); } ``` @@ -139,7 +139,7 @@ function command( bool $force, ) { if ($force) { - echo "command has been forced\n"; + io()->writeln('command has been forced'); } } ``` diff --git a/doc/getting-started/basic-usage.md b/doc/getting-started/basic-usage.md index 6e227e38..c9bdd065 100644 --- a/doc/getting-started/basic-usage.md +++ b/doc/getting-started/basic-usage.md @@ -16,20 +16,24 @@ namespace hello; use Castor\Attribute\AsTask; +use function Castor\io; + #[AsTask()] function castor(): void { - echo 'Hello castor'; + io()->writeln('Hello castor'); } namespace foo; +use function Castor\io; + use Castor\Attribute\AsTask; #[AsTask()] function bar(): void { - echo 'Foo bar'; + io()->writeln('Foo bar'); } ``` @@ -77,10 +81,12 @@ arguments: `name`, `namespace` and `description` to override the default values: ```php use Castor\Attribute\AsTask; -#[AsTask(name: 'bar', namespace: 'foo', description: 'Echo foo bar')] +use function Castor\io; + +#[AsTask(name: 'bar', namespace: 'foo', description: 'Output foo bar')] function a_very_long_function_name_that_is_very_painful_to_write(): void { - echo 'Foo bar'; + io()->writeln('Foo bar'); } ``` diff --git a/doc/getting-started/context.md b/doc/getting-started/context.md index 27febdee..95eb501f 100644 --- a/doc/getting-started/context.md +++ b/doc/getting-started/context.md @@ -20,6 +20,7 @@ You can get the initial context thanks to the `context()` function: use Castor\Attribute\AsTask; use function Castor\context; +use function Castor\io; use function Castor\run; #[AsTask()] @@ -27,7 +28,7 @@ function foo(): void { $context = context(); - echo $context->currentDirectory; // will print the directory of the castor.php file + io()->writeln($context->currentDirectory); // will print the directory of the castor.php file $context = $context->withCurrentDirectory('/tmp'); // will create a new context where the current directory is /tmp run('pwd', context: $context); // will print "/tmp" @@ -117,6 +118,7 @@ setting the `default` argument to `true` in the `AsContext` attribute: use Castor\Attribute\AsContext; use Castor\Context; +use function Castor\io; use function Castor\run; #[AsContext(default: true, name: 'my_context')] @@ -128,7 +130,7 @@ function create_default_context(): Context #[AsTask()] function foo(Context $context): void { - run(['echo', $context['foo']]); // will print bar even if you do not use the --context option + io()->writeln($context['foo']); // will print bar even if you do not use the --context option run('pwd'); // will print /tmp } ``` diff --git a/doc/getting-started/run.md b/doc/getting-started/run.md index 5bc37cc6..8e818983 100644 --- a/doc/getting-started/run.md +++ b/doc/getting-started/run.md @@ -12,8 +12,8 @@ use function Castor\run; #[AsTask()] function foo(): void { - run('echo "bar"'); - run(['echo', 'bar']); + run('my-script.sh'); + run(['php', 'vendor/bin/phpunit', '--filter', 'MyTest']); } ``` @@ -35,8 +35,8 @@ use function Castor\run; #[AsTask()] function foo(): void { - $process = run('echo "bar"'); - $process->isSuccessful(); // will return true + $process = run('my-script.sh'); + $process->isSuccessful(); // will return true if the process exited with code 0. } ``` @@ -152,13 +152,14 @@ trims the output, then returns it: use Castor\Attribute\AsTask; use function Castor\capture; +use function Castor\io; #[AsTask()] function whoami() { $whoami = capture('whoami'); - echo "Hello: $whoami\n"; + io()->writeln("Hello: $whoami"); } ``` @@ -200,7 +201,7 @@ use function Castor\run; #[AsTask()] function foo(): void { - run('echo "bar"', timeout: 120); + run('my-script.sh', timeout: 120); } ``` diff --git a/doc/going-further/helpers/cache.md b/doc/going-further/helpers/cache.md index 085f3476..c9dd316c 100644 --- a/doc/going-further/helpers/cache.md +++ b/doc/going-further/helpers/cache.md @@ -9,19 +9,22 @@ use Castor\Attribute\AsTask; use Psr\Cache\CacheItemInterface; use function Castor\cache; +use function Castor\io; #[AsTask()] function foo() { - echo cache('a-key', expansive_call(...)); + $result = cache('a-key', expansive_call(...)); // Or if you want to set a TTL - echo cache('another-key', function (CacheItemInterface $item) => { + $result = cache('another-key', function (CacheItemInterface $item) => { $item->expiresAfter(3600); return expansive_call(); }); + + io()->writeln($result); } ``` @@ -43,6 +46,7 @@ If you need to have a full control on the cache, you can access the use Castor\Attribute\AsTask; use function Castor\get_cache; +use function Castor\io; #[AsTask()] function foo() @@ -56,6 +60,6 @@ function foo() $cache->save($item); } - echo $item->get(); + io()->writeln($item->get()); } ``` diff --git a/doc/going-further/helpers/filesystem.md b/doc/going-further/helpers/filesystem.md index a381bc4a..aabc9c71 100644 --- a/doc/going-further/helpers/filesystem.md +++ b/doc/going-further/helpers/filesystem.md @@ -14,23 +14,24 @@ use Castor\Attribute\AsTask; use Symfony\Component\Filesystem\Path; use function Castor\fs; +use function Castor\io; #[AsTask()] function foo() { $dir = '/tmp/foo'; - echo $dir, ' directory exist: ', fs()->exists($dir) ? 'yes' : 'no', \PHP_EOL; + io()->writeln($dir . ' directory exist: ' . (fs()->exists($dir) ? 'yes' : 'no')); fs()->mkdir($dir); fs()->touch($dir . '/bar.md'); - echo $dir, ' is an absolute path: ', Path::isAbsolute($dir) ? 'yes' : 'no', \PHP_EOL; - echo '../ is an absolute path: ', Path::isAbsolute('../') ? 'yes' : 'no', \PHP_EOL; + io()->writeln($dir, ' is an absolute path: ' . (Path::isAbsolute($dir) ? 'yes' : 'no')); + io()->writeln('../ is an absolute path: ' . (Path::isAbsolute('../') ? 'yes' : 'no')); fs()->remove($dir); - echo 'Absolute path: ', Path::makeAbsolute('../', $dir), \PHP_EOL; + io()->writeln('Absolute path: ' . Path::makeAbsolute('../', $dir)); } ``` @@ -50,11 +51,12 @@ intuitive fluent interface. It returns an instance of use Castor\Attribute\AsTask; use function Castor\finder; +use function Castor\io; #[AsTask()] function foo() { - echo 'Number of PHP files: ', finder()->name('*.php')->in(__DIR__)->count(), \PHP_EOL; + io()->writeln('Number of PHP files: ' . finder()->name('*.php')->in(__DIR__)->count()); } ``` diff --git a/doc/going-further/helpers/fingerprint.md b/doc/going-further/helpers/fingerprint.md index 29cc366a..f7056a49 100644 --- a/doc/going-further/helpers/fingerprint.md +++ b/doc/going-further/helpers/fingerprint.md @@ -13,14 +13,14 @@ the given fingerprint has changed. ```php use Castor\Attribute\AsTask; use function Castor\fingerprint; -use function Castor\run; +use function Castor\io; #[AsTask(description: 'Execute a callback only if the fingerprint has changed')] function task_with_a_fingerprint(): void { fingerprint( callback: function () { - run('echo "Cool, no fingerprint! Executing..."'); + io()->writeln('Cool, no fingerprint! Executing...'); }, fingerprint: "my fingerprint", ); @@ -69,7 +69,7 @@ function task_with_a_fingerprint(): void { fingerprint( callback: function () { - run('echo "Executing the callback because my-file.json has changed."'); + io()->writeln('Executing the callback because my-file.json has changed.'); }, fingerprint: hasher()->writeFile('my-file.json', FileHashStrategy::Content)->finish(), ); @@ -94,7 +94,7 @@ use function Castor\hasher; function task_with_some_fingerprint(): void { if (!fingerprint_exists(my_fingerprint_check())) { - run('echo "Executing some code because fingerprint has changed."'); + io()->writeln('Executing some code because fingerprint has changed.'); fingerprint_save(my_fingerprint_check()); } } diff --git a/doc/going-further/helpers/http-request.md b/doc/going-further/helpers/http-request.md index 8a2134a6..5fe548c6 100644 --- a/doc/going-further/helpers/http-request.md +++ b/doc/going-further/helpers/http-request.md @@ -9,12 +9,13 @@ request and returns an instance of ```php use Castor\Attribute\AsTask; +use function Castor\io; use function Castor\request; #[AsTask()] function foo() { - echo request('GET', 'https://example.org')->getContent(), \PHP_EOL; + io()->writeln(request('GET', 'https://example.org')->getContent()); } ``` diff --git a/doc/going-further/helpers/parallel.md b/doc/going-further/helpers/parallel.md index 8d252eff..67177a06 100644 --- a/doc/going-further/helpers/parallel.md +++ b/doc/going-further/helpers/parallel.md @@ -8,6 +8,7 @@ so you do not have to wait for a function to finish before starting another one: ```php use Castor\Attribute\AsTask; +use function Castor\io; use function Castor\parallel; #[AsTask()] @@ -22,8 +23,8 @@ function foo(): void } ); - echo $foo->getOutput(); // will print foo - echo $bar->getOutput(); // will print bar + io()->writeln($foo->getOutput()); // will print foo + io()->writeln($bar->getOutput()); // will print bar } ``` diff --git a/doc/going-further/helpers/watch.md b/doc/going-further/helpers/watch.md index eb494c2f..6edef9a9 100644 --- a/doc/going-further/helpers/watch.md +++ b/doc/going-further/helpers/watch.md @@ -6,13 +6,14 @@ call a callback function when the file or directory changes: ```php use Castor\Attribute\AsTask; +use function Castor\io; use function Castor\watch; #[AsTask()] function watcher(): void { watch('src/', function (string $file, string $action) { - echo "File {$file} has been {$action}\n"; + io()->writeln("File {$file} has been {$action}"); }); } ``` @@ -28,6 +29,7 @@ that by passing a path suffixed by `/...`: ```php use Castor\Attribute\AsTask; +use function Castor\io; use function Castor\watch; #[AsTask()] @@ -35,7 +37,7 @@ function watcher(): void { // watch recursively inside the src folder watch('src/...', function (string $file, string $action) { - echo "File {$file} has been {$action}\n"; + io()->writeln("File {$file} has been {$action}"); }); } ``` @@ -48,6 +50,7 @@ the callback function returns `false` the watch will stop: ```php use Castor\Attribute\AsTask; +use function Castor\io; use function Castor\watch; #[AsTask()] @@ -55,10 +58,11 @@ function watcher(): void { // watch recursively inside the src folder watch('src/...', function (string $file, string $action) { - echo "File {$file} has been {$action}\n"; + io()->writeln("File {$file} has been {$action}"); + return false; }); - echo 'stopped watching'; // will print "stopped watching" once a file has been modified in the src folder + io()->writeln('stopped watching'); // will print "stopped watching" once a file has been modified in the src folder } ``` @@ -69,6 +73,7 @@ The `watch()` function can watch multiple paths at the same time: ```php use Castor\Attribute\AsTask; +use function Castor\io; use function Castor\watch; #[AsTask()] @@ -76,7 +81,7 @@ function watcher(): void { // watch recursively inside the src and tests folders watch(['src/...', 'tests/...'], function (string $file, string $action) { - echo "File {$file} has been {$action}\n"; + io()->writeln("File {$file} has been {$action}"); }); } ``` diff --git a/doc/going-further/interacting-with-castor/advanced-context.md b/doc/going-further/interacting-with-castor/advanced-context.md index cdb72bfd..f66318fb 100644 --- a/doc/going-further/interacting-with-castor/advanced-context.md +++ b/doc/going-further/interacting-with-castor/advanced-context.md @@ -8,10 +8,12 @@ You can disable a task according to the context by using the ```php use Castor\Attribute\AsTask; +use function Castor\io; + #[AsTask(description: 'Say hello, but only in production', enabled: "var('production') == true")] function hello(): void { - echo "Hello world!\n"; + io()->writeln('Hello world!'); } ``` @@ -34,6 +36,7 @@ You can get a specific context by its name using the `context()` function: use Castor\Attribute\AsContext; use Castor\Context; +use function Castor\io; use function Castor\run; #[AsContext(name: 'my_context')] @@ -47,7 +50,7 @@ function foo(): void { $context = context('my_context'); - run(['echo', $context['foo']]); // will print bar even if you do not use the --context option + io()->writeln($context['foo']); // will print bar even if you do not use the --context option run('pwd', context: $context); // will print /tmp } ``` @@ -75,7 +78,7 @@ function create_my_context(): Context function foo(): void { with(function (Context $context) { - run(['echo', $context['foo']]); // will print bar even if you do not use the --context option + io()->writeln($context['foo']); // will print bar even if you do not use the --context option run('pwd'); // will print /tmp }, context: 'my_context'); } diff --git a/doc/going-further/interacting-with-castor/dot-env.md b/doc/going-further/interacting-with-castor/dot-env.md index 9225a729..dbdcbfe8 100644 --- a/doc/going-further/interacting-with-castor/dot-env.md +++ b/doc/going-further/interacting-with-castor/dot-env.md @@ -24,7 +24,7 @@ function show_database_url(): void { $env = load_dot_env(); - echo $env['DATABASE_URL'] ?? throw new \RuntimeException('DATABASE_URL is not defined'); + io()->writeln($env['DATABASE_URL']) ?? throw new \RuntimeException('DATABASE_URL is not defined')); } ``` diff --git a/doc/going-further/interacting-with-castor/signals.md b/doc/going-further/interacting-with-castor/signals.md index 28a21503..294c1865 100644 --- a/doc/going-further/interacting-with-castor/signals.md +++ b/doc/going-further/interacting-with-castor/signals.md @@ -6,6 +6,8 @@ stop a task when the user presses `CTRL+C` or to handle other signals: ```php use Castor\Attribute\AsTask; +use function Castor\io; + #[AsTask(onSignals: [\SIGUSR2 => 'onSigUsr2'])] function foo(): void { @@ -14,7 +16,7 @@ function foo(): void function onSigUsr2(int $signal): int|false { - echo "SIGUSR2 received\n"; + io()->writeln("SIGUSR2 received\n"); return false; } @@ -38,7 +40,7 @@ function foo(): void function onSigUsr2(int $signal): int|false { - echo "SIGUSR2 received\n"; + io()->writeln('SIGUSR2 received'); return false; } diff --git a/doc/index.md b/doc/index.md index f5b53781..6d7c3f77 100644 --- a/doc/index.md +++ b/doc/index.md @@ -43,12 +43,12 @@ For example: namespace greetings; use Castor\Attribute\AsTask; -use function Castor\run; +use function Castor\io; #[AsTask()] function hello(): void { - run('echo "Hello from castor"'); + io()->write('Hello from castor'); } ``` diff --git a/examples/args.php b/examples/args.php index e9c63d75..088a9f90 100644 --- a/examples/args.php +++ b/examples/args.php @@ -7,7 +7,7 @@ use Castor\Attribute\AsRawTokens; use Castor\Attribute\AsTask; -use function Castor\run; +use function Castor\io; /** * @param string[] $argument2 @@ -31,7 +31,7 @@ function another_args( string $required, int $test2 = 1 ): void { - run(['echo', $required, $test2]); + io()->writeln($required . ' ' . $test2); } /** diff --git a/examples/bar.php b/examples/bar.php index ef062a1f..413de38c 100644 --- a/examples/bar.php +++ b/examples/bar.php @@ -4,6 +4,7 @@ use Castor\Attribute\AsTask; +use function Castor\io; use function foo\foo; #[AsTask(description: 'Prints bar, but also executes foo')] @@ -11,5 +12,5 @@ function bar(): void { foo(); - echo "bar\n"; + io()->writeln('bar'); } diff --git a/examples/cache.php b/examples/cache.php index e82ab641..4acbb573 100644 --- a/examples/cache.php +++ b/examples/cache.php @@ -6,13 +6,14 @@ use Psr\Cache\CacheItemInterface; use function Castor\cache; +use function Castor\io; #[AsTask(description: 'Cache a simple call')] function simple(): void { - echo cache('my-key', fn () => "SALUT\n"); + io()->writeln(cache('my-key', fn () => 'SALUT')); // Should returns the same things - echo cache('my-key', fn () => "HELLO\n"); + io()->writeln(cache('my-key', fn () => 'HELLO')); } #[AsTask(description: 'Cache with usage of CacheItemInterface')] @@ -25,7 +26,7 @@ function complex(): void return true; }); - echo sprintf("First call: %s\n", $hasBeenCalled ? 'yes' : 'no'); + io()->writeln(sprintf('First call: %s', $hasBeenCalled ? 'yes' : 'no')); $hasBeenCalled = false; cache('another-key', function (CacheItemInterface $item) use (&$hasBeenCalled) { @@ -34,5 +35,5 @@ function complex(): void return true; }); - echo sprintf("Second call: %s\n", $hasBeenCalled ? 'yes' : 'no'); + io()->writeln(sprintf('Second call: %s', $hasBeenCalled ? 'yes' : 'no')); } diff --git a/examples/context.php b/examples/context.php index 3a608f93..4debfda8 100644 --- a/examples/context.php +++ b/examples/context.php @@ -100,11 +100,11 @@ function contextFromPath(): Context function contextInfo(): void { $context = context(); - echo 'context name: ' . variable('name', 'N/A') . "\n"; - echo 'Production? ' . (variable('production', false) ? 'yes' : 'no') . "\n"; - echo "verbosity: {$context->verbosityLevel->value}\n"; - echo 'context: ' . variable('foo', 'N/A') . "\n"; - echo 'nested merge recursive: ' . json_encode(variable('nested', []), \JSON_THROW_ON_ERROR) . "\n"; + io()->writeln('context name: ' . variable('name', 'N/A')); + io()->writeln('Production? ' . (variable('production', false) ? 'yes' : 'no')); + io()->writeln("verbosity: {$context->verbosityLevel->value}"); + io()->writeln('context: ' . variable('foo', 'N/A')); + io()->writeln('nested merge recursive: ' . json_encode(variable('nested', []), \JSON_THROW_ON_ERROR)); } /** @@ -124,7 +124,7 @@ function context_generator(): iterable function contextInfoForced(): void { $context = context('dynamic'); - echo 'context name: ' . $context->data['name'] . "\n"; + io()->writeln('context name: ' . $context->data['name']); } #[AsTask(description: 'Displays information about the context, using a specific context')] @@ -136,5 +136,5 @@ function contextWith(): void return $context->data['foo'] ?? 'N/A'; }, data: ['foo' => 'bar'], context: 'dynamic'); - echo $result; + io()->writeln($result); } diff --git a/examples/enabled.php b/examples/enabled.php index 31e49542..a460bb12 100644 --- a/examples/enabled.php +++ b/examples/enabled.php @@ -4,8 +4,10 @@ use Castor\Attribute\AsTask; +use function Castor\io; + #[AsTask(description: 'Say hello, but only in production', enabled: "var('production') == true")] function hello(): void { - echo "Hello world!\n"; + io()->writeln('Hello world!'); } diff --git a/examples/filesystem.php b/examples/filesystem.php index 3f86c685..02ace373 100644 --- a/examples/filesystem.php +++ b/examples/filesystem.php @@ -7,6 +7,7 @@ use function Castor\finder; use function Castor\fs; +use function Castor\io; #[AsTask(description: 'Performs some operations on the filesystem')] function filesystem(): void @@ -15,17 +16,17 @@ function filesystem(): void $dir = '/tmp/foo'; - echo $dir, ' directory exist: ', $fs->exists($dir) ? 'yes' : 'no', \PHP_EOL; + io()->writeln($dir . ' directory exist: ' . ($fs->exists($dir) ? 'yes' : 'no')); $fs->mkdir($dir); $fs->touch($dir . '/bar.md'); - echo $dir, ' is an absolute path: ', Path::isAbsolute($dir) ? 'yes' : 'no', \PHP_EOL; - echo '../ is an absolute path: ', Path::isAbsolute('../') ? 'yes' : 'no', \PHP_EOL; + io()->writeln($dir . ' is an absolute path: ' . (Path::isAbsolute($dir) ? 'yes' : 'no')); + io()->writeln('../ is an absolute path: ' . (Path::isAbsolute('../') ? 'yes' : 'no')); $fs->remove($dir); - echo 'Absolute path: ', Path::makeAbsolute('../', $dir), \PHP_EOL; + io()->writeln('Absolute path: ' . Path::makeAbsolute('../', $dir)); } #[AsTask(description: 'Search files and directories on the filesystem')] @@ -33,5 +34,5 @@ function find(): void { $finder = finder(); - echo 'Number of PHP files: ', $finder->name('*.php')->in(__DIR__)->count(), \PHP_EOL; + io()->writeln('Number of PHP files: ' . $finder->name('*.php')->in(__DIR__)->count()); } diff --git a/examples/fingerprint.php b/examples/fingerprint.php index cacf843c..e288160e 100644 --- a/examples/fingerprint.php +++ b/examples/fingerprint.php @@ -11,55 +11,55 @@ use function Castor\fingerprint_exists; use function Castor\fingerprint_save; use function Castor\hasher; -use function Castor\run; +use function Castor\io; #[AsTask(description: 'Execute a callback only if the fingerprint has changed')] function task_with_a_fingerprint(): void { - run('echo "Hello Task with Fingerprint!"'); + io()->writeln('Hello Task with Fingerprint!'); fingerprint( callback: function () { - run('echo "Cool, no fingerprint! Executing..."'); + io()->writeln('Cool, no fingerprint! Executing...'); }, fingerprint: my_fingerprint_check() ); - run('echo "Cool! I finished!"'); + io()->writeln('Cool! I finished!'); } #[AsTask(description: 'Check if the fingerprint has changed before executing some code')] function task_with_complete_fingerprint_check(): void { - run('echo "Hello Task with Fingerprint!"'); + io()->writeln('Hello Task with Fingerprint!'); if (!fingerprint_exists(my_fingerprint_check())) { - run('echo "Cool, no fingerprint! Executing..."'); + io()->writeln('Cool, no fingerprint! Executing...'); fingerprint_save(my_fingerprint_check()); } - run('echo "Cool! I finished!"'); + io()->writeln('Cool! I finished!'); } #[AsTask(description: 'Check if the fingerprint has changed before executing a callback (with force option)')] function task_with_a_fingerprint_and_force( #[AsOption(description: 'Force the callback to run even if the fingerprint has not changed')] bool $force = false ): void { - run('echo "Hello Task with Fingerprint!"'); + io()->writeln('Hello Task with Fingerprint!'); $hasRun = fingerprint( callback: function () { - run('echo "Cool, no fingerprint! Executing..."'); + io()->writeln('Cool, no fingerprint! Executing...'); }, fingerprint: my_fingerprint_check(), force: $force // This option will force the task to run even if the fingerprint has not changed ); if ($hasRun) { - run('echo "Fingerprint has been executed!"'); + io()->writeln('Fingerprint has been executed!'); } - run('echo "Cool! I finished!"'); + io()->writeln('Cool! I finished!'); } function my_fingerprint_check(): string diff --git a/examples/foo.php b/examples/foo.php index aa04707a..c6e7f6f1 100644 --- a/examples/foo.php +++ b/examples/foo.php @@ -4,14 +4,16 @@ use Castor\Attribute\AsTask; +use function Castor\io; + #[AsTask(description: 'Prints foo')] function foo(): void { - echo "foo\n"; + io()->writeln('foo'); } #[AsTask(name: 'bar', namespace: 'foo', description: 'Echo foo bar')] function a_very_long_function_name_that_is_very_painful_to_write(): void { - echo 'Foo bar'; + io()->writeln('Foo bar'); } diff --git a/examples/http.php b/examples/http.php index 42ac7b63..b8f585b4 100644 --- a/examples/http.php +++ b/examples/http.php @@ -2,6 +2,7 @@ use Castor\Attribute\AsTask; +use function Castor\io; use function Castor\request; #[AsTask(description: 'Make HTTP request')] @@ -11,5 +12,5 @@ function httpRequest(): void $response = request('GET', $url); - echo $response->getContent(); + io()->writeln($response->getContent()); } diff --git a/examples/parallel.php b/examples/parallel.php index fe046550..f7a53ca9 100644 --- a/examples/parallel.php +++ b/examples/parallel.php @@ -4,6 +4,7 @@ use Castor\Attribute\AsTask; +use function Castor\io; use function Castor\parallel; use function Castor\run; @@ -53,15 +54,15 @@ function sleep(int $sleep5 = 5, int $sleep7 = 7, int $sleep10 = 10): void fn () => embed_sleep($sleep5, $sleep7) ); - echo "\n"; + io()->writeln(''); $duration = (int) (microtime(true) - $start); - echo "Duration: {$duration}s\n"; + io()->writeln("Duration: {$duration}s"); - echo "\n"; + io()->writeln(''); - echo "\$foo = '{$foo}';\n"; - echo "\$bar = '{$bar}';\n"; - echo "\$baz = '{$baz}';\n"; + io()->writeln("\$foo = '{$foo}';"); + io()->writeln("\$bar = '{$bar}';"); + io()->writeln("\$baz = '{$baz}';"); } #[AsTask(description: 'Sleep and throw an exception')] diff --git a/examples/rename.php b/examples/rename.php index e9939307..85135b0c 100644 --- a/examples/rename.php +++ b/examples/rename.php @@ -4,14 +4,16 @@ use Castor\Attribute\AsTask; +use function Castor\io; + #[AsTask(description: 'Task that was renamed', name: 'renamed', namespace: 'not-rename')] function a_very_long_function_that_we_dont_want_to_write_on_command_line(): void { - echo "renamed\n"; + io()->writeln('renamed'); } #[AsTask(description: 'Task without a namespace', name: 'no-namespace', namespace: '')] function no_namespace(): void { - echo "renamed\n"; + io()->writeln('renamed'); } diff --git a/examples/run.php b/examples/run.php index 38b16da2..9e6e3cfc 100644 --- a/examples/run.php +++ b/examples/run.php @@ -8,6 +8,7 @@ use function Castor\app; use function Castor\capture; use function Castor\exit_code; +use function Castor\io; use function Castor\output; use function Castor\run; @@ -16,10 +17,9 @@ function ls(): void { $process = run('ls -alh && echo $foo', quiet: true, environment: ['foo' => 'ba\'"`r']); - echo "Output: \n" . $process->getOutput(); - echo "\nError output: \n" . $process->getErrorOutput(); - echo "\nExit code: " . $process->getExitCode(); - echo "\n"; + io()->writeln('Output:' . $process->getOutput()); + io()->writeln('Error output: ' . $process->getErrorOutput()); + io()->writeln('Exit code: ' . $process->getExitCode()); } #[AsTask(description: 'Run a sub-process with environment variables and display information about it')] @@ -27,10 +27,9 @@ function variables(): void { $process = run('echo $foo', quiet: true, environment: ['foo' => 'ba\'"`r']); - echo "Output: \n" . $process->getOutput(); - echo "\nError output: \n" . $process->getErrorOutput(); - echo "\nExit code: " . $process->getExitCode(); - echo "\n"; + io()->writeln('Output: ' . $process->getOutput()); + io()->writeln('Error output: ' . $process->getErrorOutput()); + io()->writeln('Exit code: ' . $process->getExitCode()); } #[AsTask(description: 'Run a sub-process and display information about it, with capture() function')] @@ -40,7 +39,7 @@ function whoami(): void // for each different users $whoami = capture('echo whoami'); - echo "Hello: {$whoami}\n"; + io()->writeln("Hello: {$whoami}"); } #[AsTask(description: 'Run a sub-process and return its exit code, with get_exit_code() function')] diff --git a/examples/signal.php b/examples/signal.php index 4f288f68..7a636c39 100644 --- a/examples/signal.php +++ b/examples/signal.php @@ -4,6 +4,8 @@ use Castor\Attribute\AsTask; +use function Castor\io; + if (!\defined('SIGUSR2')) { \define('SIGUSR2', 12); } @@ -20,7 +22,7 @@ function sigusr2(): void */ function onSigUsr2(int $signal): bool { - echo "SIGUSR2 received\n"; + io()->writeln('SIGUSR2 received'); return false; } diff --git a/examples/watch.php b/examples/watch.php index 951c6705..ac1d3e26 100644 --- a/examples/watch.php +++ b/examples/watch.php @@ -4,15 +4,16 @@ use Castor\Attribute\AsTask; +use function Castor\io; use function Castor\parallel; use function Castor\watch; #[AsTask(description: 'Watches on filesystem changes')] function fs_change(): void { - echo "Try editing a file\n"; + io()->writeln('Try editing a file'); watch(\dirname(__DIR__) . '/...', function (string $name, string $type) { - echo "File {$name} has been {$type}\n"; + io()->writeln("File {$name} has been {$type}"); }); } @@ -20,11 +21,11 @@ function fs_change(): void function stop(): void { watch(\dirname(__DIR__) . '/...', function (string $name, string $type) { - echo "File {$name} has been {$type}\n"; + io()->writeln("File {$name} has been {$type}"); return false; }); - echo "Stop watching\n"; + io()->writeln('Stop watching'); } #[AsTask(description: 'Watches on filesystem changes with 2 watchers in parallel')] @@ -33,9 +34,9 @@ function parallel_change(): void parallel( function () { for ($i = 1; $i <= 10; ++$i) { - echo "[app] Writing hello-{$i}.txt\n"; + io()->writeln("[app] Writing hello-{$i}.txt"); file_put_contents("hello-{$i}.txt", "Hello {$i}\n", \FILE_APPEND); - echo "[app] Deleting hello-{$i}.txt\n"; + io()->writeln("[app] Deleting hello-{$i}.txt"); unlink("hello-{$i}.txt"); if (\Fiber::getCurrent()) { \Fiber::suspend(); @@ -45,12 +46,12 @@ function () { }, function () { watch(\dirname(__DIR__) . '/...', function ($name, $type) { - echo "[watcher:A] File {$name} has been {$type}\n"; + io()->writeln("[watcher:A] File {$name} has been {$type}"); }); }, function () { watch(\dirname(__DIR__) . '/...', function ($name, $type) { - echo "[watcher:B] Second : File {$name} has been {$type}\n"; + io()->writeln("[watcher:B] Second : File {$name} has been {$type}"); }); }, ); diff --git a/examples/yaml.php b/examples/yaml.php index ad246e02..35c776a3 100644 --- a/examples/yaml.php +++ b/examples/yaml.php @@ -4,6 +4,7 @@ use Castor\Attribute\AsTask; +use function Castor\io; use function Castor\yaml_dump; use function Castor\yaml_parse; @@ -13,12 +14,12 @@ function parse(): void $data = yaml_parse(<<<'YAML' foo: bar YAML); - echo $data['foo'] . "\n"; + io()->writeln($data['foo']); } #[AsTask(description: 'Dump a YAML content')] function dump(): void { $data = ['foo' => 'bar']; - echo yaml_dump($data) . "\n"; + io()->writeln(yaml_dump($data)); } diff --git a/tests/Examples/Generated/ContextContextWithTest.php.output.txt b/tests/Examples/Generated/ContextContextWithTest.php.output.txt index 5d932fe3..9d78a4ca 100644 --- a/tests/Examples/Generated/ContextContextWithTest.php.output.txt +++ b/tests/Examples/Generated/ContextContextWithTest.php.output.txt @@ -3,4 +3,4 @@ Production? no verbosity: -1 context: bar nested merge recursive: [] -bar \ No newline at end of file +bar diff --git a/tests/Examples/Generated/FooBarTest.php.output.txt b/tests/Examples/Generated/FooBarTest.php.output.txt index 413b70b9..2bda3b44 100644 --- a/tests/Examples/Generated/FooBarTest.php.output.txt +++ b/tests/Examples/Generated/FooBarTest.php.output.txt @@ -1 +1 @@ -Foo bar \ No newline at end of file +Foo bar diff --git a/tests/Examples/Generated/HttpRequestTest.php.output.txt b/tests/Examples/Generated/HttpRequestTest.php.output.txt index c57eff55..980a0d5f 100644 --- a/tests/Examples/Generated/HttpRequestTest.php.output.txt +++ b/tests/Examples/Generated/HttpRequestTest.php.output.txt @@ -1 +1 @@ -Hello World! \ No newline at end of file +Hello World! diff --git a/tests/Examples/Generated/ParallelExceptionTest.php.err.txt b/tests/Examples/Generated/ParallelExceptionTest.php.err.txt index 01cd32da..c27b4bef 100644 --- a/tests/Examples/Generated/ParallelExceptionTest.php.err.txt +++ b/tests/Examples/Generated/ParallelExceptionTest.php.err.txt @@ -1,5 +1,5 @@ -In parallel.php line 73: +In parallel.php line 74: This is an exception @@ -7,7 +7,7 @@ In parallel.php line 73: parallel:exception -In parallel.php line 71: +In parallel.php line 72: The command "exit 1" failed. diff --git a/tests/Examples/Generated/RunVariablesTest.php.output.txt b/tests/Examples/Generated/RunVariablesTest.php.output.txt index caba4d6e..d1c38e0c 100644 --- a/tests/Examples/Generated/RunVariablesTest.php.output.txt +++ b/tests/Examples/Generated/RunVariablesTest.php.output.txt @@ -1,6 +1,4 @@ -Output: -ba'"`r +Output: ba'"`r Error output: - Exit code: 0