PHP code example of maplephp / prompts

1. Go to this page and download the library: Download maplephp/prompts library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

maplephp / prompts example snippets




aplePHP\Prompts\Command;

$cli = new Command();

$name = $cli->ask('Project name');
$database = $cli->select('Database', [
    'sqlite' => 'SQLite',
    'mysql' => 'MySQL',
    'pgsql' => 'PostgreSQL',
]);

$cli->success("Settings collected for $name ($database).");



aplePHP\Prompts\Prompt;

$steps = [
    'name' => [
        'message' => 'Project name',
        'default' => 'atlas',
        'validate' => ['length' => [3, 40]],
        'error' => 'Use between 3 and 40 characters.',
    ],
    'database' => [
        'type' => 'select',
        'message' => 'Database',
        'items' => ['sqlite' => 'SQLite', 'mysql' => 'MySQL'],
    ],
    'approved' => [
        'type' => 'confirm',
        'message' => 'Create this project?',
    ],
];

$installer = (new Prompt())->setTitle('New project')->set($steps);
$answers = $installer->prompt();

if ($answers === false) {
    exit(1);
}

$installer->getCommand()->success(
    "Settings approved for {$answers['name']} ({$answers['database']})."
);

['name' => 'atlas', 'database' => 'sqlite', 'approved' => 1]

$installer = new Prompt();
$answers = $installer->runDefinition($steps);

$email = $cli->askValidated(
    'Administrator email',
    ['isEmail' => []],
    'Please enter a valid email address.'
);

$slug = $cli->askValidated(
    'Project slug',
    static fn (string $value): bool => preg_match('/\A[a-z0-9-]+\z/', $value) === 1,
    'Use lowercase letters, numbers, and hyphens.'
);

$timezone = $cli->selectWithSearch('Timezone', [
    'Europe/Stockholm' => 'Stockholm',
    'Europe/London' => 'London',
    'America/New_York' => 'New York',
    'America/Los_Angeles' => 'Los Angeles',
]);

$features = $cli->multiSelect('Include in this project', [
    'api' => 'API endpoints',
    'mail' => 'Email notifications',
    'reports' => 'Reports',
], defaults: ['api']);

$files = glob(__DIR__ . '/vendor/maplephp/prompts/src/*.php') ?: [];
$checksums = [];
$bar = $cli->progressBar(count($files), width: 24);

try {
    foreach ($files as $file) {
        $hash = hash_file('sha256', $file);
        if ($hash === false) {
            throw new RuntimeException("Could not read $file.");
        }
        $checksums[$file] = $hash;
        $bar->advance();
    }
} finally {
    $bar->finish();
}

$ansi = $cli->getAnsi();

$cli->line($ansi->blueBg(' ATLAS '));
$cli->line($ansi->dim('Choose a name for your project.'));
$name = $cli->ask($ansi->cyan('Project ' . $ansi->bold('name') . ' to create'));

use MaplePHP\Prompts\Themes\Blocks;

$blocks = new Blocks($cli);
$blocks->table(['Setting', 'Value'], [
    ['Project', 'atlas'],
    ['Database', 'SQLite'],
]);

use MaplePHP\Http\Stream;

$cli = new Command(stream: new Stream(STDERR));