Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Facade For Calling The Functions #161

Closed
wants to merge 7 commits into from
17 changes: 17 additions & 0 deletions playground/facade/clear.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Laravel\Prompts\Support\Facades\Prompt;

require __DIR__ . '/../../vendor/autoload.php';

Prompt::note('This will disappear.');

Prompt::pause('Press [Enter] to continue.');

Prompt::clear();

Prompt::note('This will also disappear.');

Prompt::pause('Press [Enter] to continue.');

Prompt::clear();
18 changes: 18 additions & 0 deletions playground/facade/confirm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Laravel\Prompts\Support\Facades\Prompt;

require __DIR__ . '/../../vendor/autoload.php';

$confirmed = Prompt::confirm(
label: 'Would you like to install dependencies?',
validate: fn($value) => match ($value) {
false => 'You must install dependencies.',
default => null,
},
hint: 'Dependencies are required to run the application.',
);

var_dump($confirmed);

echo str_repeat(PHP_EOL, 2);
97 changes: 97 additions & 0 deletions playground/facade/form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

use Laravel\Prompts\Support\Facades\Prompt;

require __DIR__ . '/../../vendor/autoload.php';

$responses = Prompt::form()
->intro('Welcome to Laravel')
->suggest(
label: 'What is your name?',
placeholder: 'E.g. Taylor Otwell',
options: [
'Dries Vints',
'Guus Leeuw',
'James Brooks',
'Jess Archer',
'Joe Dixon',
'Mior Muhammad Zaki Mior Khairuddin',
'Nuno Maduro',
'Taylor Otwell',
'Tim MacDonald',
],
validate: fn($value) => match (true) {
! $value => 'Please enter your name.',
default => null,
},
)
->text(
label: 'Where should we create your project?',
placeholder: 'E.g. ./laravel',
validate: fn($value) => match (true) {
! $value => 'Please enter a path',
$value[0] !== '.' => 'Please enter a relative path',
default => null,
},
name: 'path'
)
->textarea('Describe your project')
->pause()
->submit();

$moreResponses = Prompt::form()
->password(
label: 'Provide a password',
validate: fn($value) => match (true) {
! $value => 'Please enter a password.',
strlen($value) < 5 => 'Password should have at least 5 characters.',
default => null,
},
)
->select(
label: 'Pick a project type',
default: 'ts',
options: [
'ts' => 'TypeScript',
'js' => 'JavaScript',
],
)
->multiselect(
label: 'Select additional tools.',
default: ['pint', 'eslint'],
options: [
'pint' => 'Pint',
'eslint' => 'ESLint',
'prettier' => 'Prettier',
],
validate: function ($values) {
if (count($values) === 0) {
return 'Please select at least one tool.';
}
}
)
->add(function () {
$install = Prompt::confirm(
label: 'Install dependencies?',
);

if ($install) {
Prompt::spin(fn() => sleep(3), 'Installing dependencies...');
}

return $install;
}, name: 'install')
->confirm('Finish installation?')
->add(fn($responses) => Prompt::note(
<<<EOT
Installation complete!

To get started, run:

cd {$responses['path']}
php artisan serve
EOT
))
->submit();

var_dump($responses, $moreResponses);
95 changes: 95 additions & 0 deletions playground/facade/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

use Laravel\Prompts\Support\Facades\Prompt;

require __DIR__ . '/../../vendor/autoload.php';

Prompt::intro('Welcome to Laravel');

$name = Prompt::suggest(
label: 'What is your name?',
placeholder: 'E.g. Taylor Otwell',
options: [
'Dries Vints',
'Guus Leeuw',
'James Brooks',
'Jess Archer',
'Joe Dixon',
'Mior Muhammad Zaki Mior Khairuddin',
'Nuno Maduro',
'Taylor Otwell',
'Tim MacDonald',
],
validate: fn($value) => match (true) {
! $value => 'Please enter your name.',
default => null,
},
);

$path = Prompt::text(
label: 'Where should we create your project?',
placeholder: 'E.g. ./laravel',
validate: fn($value) => match (true) {
! $value => 'Please enter a path',
$value[0] !== '.' => 'Please enter a relative path',
default => null,
},
);

$password = Prompt::password(
label: 'Provide a password',
validate: fn($value) => match (true) {
! $value => 'Please enter a password.',
strlen($value) < 5 => 'Password should have at least 5 characters.',
default => null,
},
);

$type = Prompt::select(
label: 'Pick a project type',
default: 'ts',
options: [
'ts' => 'TypeScript',
'js' => 'JavaScript',
],
);

$tools = Prompt::multiselect(
label: 'Select additional tools.',
default: ['pint', 'eslint'],
options: [
'pint' => 'Pint',
'eslint' => 'ESLint',
'prettier' => 'Prettier',
],
validate: function ($values) {
if (count($values) === 0) {
return 'Please select at least one tool.';
}
}
);

$install = Prompt::confirm(
label: 'Install dependencies?',
);

if ($install) {
Prompt::spin(fn() => sleep(3), 'Installing dependencies...');
}

Prompt::error('Error');
Prompt::warning('Warning');
Prompt::alert('Alert');

Prompt::note(<<<EOT
Installation complete!

To get started, run:

cd {$path}
php artisan serve
EOT);

Prompt::outro('Happy coding!');

var_dump(compact('name', 'path', 'password', 'type', 'tools', 'install'));
43 changes: 43 additions & 0 deletions playground/facade/multisearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use Laravel\Prompts\Support\Facades\Prompt;

require __DIR__ . '/../../vendor/autoload.php';

$users = collect([
'taylor' => 'Taylor Otwell',
'dries' => 'Dries Vints',
'james' => 'James Brooks',
'nuno' => 'Nuno Maduro',
'mior' => 'Mior Muhammad Zaki',
'jess' => 'Jess Archer',
'guus' => 'Guus Leeuw',
'tim' => 'Tim MacDonald',
'joe' => 'Joe Dixon',
]);

$selected = Prompt::multisearch(
label: 'Which users should receive the email?',
placeholder: 'Search...',
options: function ($value) use ($users) {
// Comment to show all results by default.
if (strlen($value) === 0) {
return [];
}

usleep(100 * 1000); // Simulate a DB query.

return $users->when(
strlen($value),
fn($users) => $users->filter(fn($name) => str_contains(strtolower($name), strtolower($value)))
)->all();
},
required: true,
validate: function ($values) {
if (in_array('jess', $values)) {
return 'Jess cannot receive emails';
}
},
);

var_dump($selected);
26 changes: 26 additions & 0 deletions playground/facade/multiselect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Laravel\Prompts\Support\Facades\Prompt;

require __DIR__ . '/../../vendor/autoload.php';

$permissions = Prompt::multiselect(
label: 'What permissions should the user have?',
options: [
'view' => 'View',
'create' => 'Create',
'update' => 'Update',
'delete' => 'Delete',
'restore' => 'Restore',
'force-delete' => 'Force delete',
],
validate: fn($values) => match (true) {
empty($values) => 'Please select at least one permission.',
default => null,
},
hint: 'The permissions will determine what the user can do.',
);

var_dump($permissions);

echo str_repeat(PHP_EOL, 1);
13 changes: 13 additions & 0 deletions playground/facade/notes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use Laravel\Prompts\Support\Facades\Prompt;

require __DIR__ . '/../../vendor/autoload.php';

Prompt::intro('Intro');
Prompt::note('Note');
Prompt::info('Info');
Prompt::warning('Warning');
Prompt::error('Error');
Prompt::alert('Alert');
Prompt::outro('Outro');
20 changes: 20 additions & 0 deletions playground/facade/password.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Laravel\Prompts\Support\Facades\Prompt;

require __DIR__ . '/../../vendor/autoload.php';

$password = Prompt::password(
label: 'Please provide a password',
placeholder: 'Min 8 characters',
required: true,
validate: fn($value) => match (true) {
strlen($value) < 8 => 'Password should have at least 8 characters.',
default => null,
},
hint: 'Your password will be encrypted and stored securely.',
);

var_dump($password);

echo str_repeat(PHP_EOL, 5);
9 changes: 9 additions & 0 deletions playground/facade/pause.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Laravel\Prompts\Support\Facades\Prompt;

require __DIR__ . '/../../vendor/autoload.php';

$continued = Prompt::pause();

var_dump($continued);
Loading
Loading