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

Updated docs. #27

Merged
merged 1 commit into from
May 25, 2024
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
58 changes: 30 additions & 28 deletions .customizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CustomizerConfig {
/**
* Messages used by the command.
*
* @param CustomizeCommand $command
* @param CustomizeCommand $customizer
* The command instance.
*
* @return array<string,string|array<string>>
Expand All @@ -23,7 +23,7 @@ class CustomizerConfig {
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public static function messages(CustomizeCommand $command): array {
public static function messages(CustomizeCommand $customizer): array {
return [
// This is an example of a custom message that overrides the default
// message with name `welcome`.
Expand All @@ -38,13 +38,12 @@ public static function messages(CustomizeCommand $command): array {
* in the order they are defined. Questions can use answers from previous
* questions received so far.
*
* Questions defined here will **fully override** any questions defined in
* the Customizer (if any).
*
* Answers will be processed in the order they are defined. Process callbacks
* have access to all answers and Customizer classes properties and methods.
* have access to all answers and Customizer's class public properties and
* methods.
*
* If a question does not have a process callback, a static method prefixed
* with 'process' and a camel cased question title will be called. If the
* with `process` and a camel-cased question title will be called. If the
* method does not exist, there will be no processing.
*
* @code
Expand All @@ -71,7 +70,7 @@ public static function messages(CustomizeCommand $command): array {
* ];
* @endcode
*
* @param CustomizeCommand $command
* @param CustomizeCommand $customizer
* The CustomizeCommand object. Can be used to access the command properties
* and methods to prepare questions. Note that the questions callbacks
* already receive the command object as an argument, so this argument is
Expand All @@ -96,7 +95,7 @@ public static function messages(CustomizeCommand $command): array {
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public static function questions(CustomizeCommand $command): array {
public static function questions(CustomizeCommand $customizer): array {
// This an example of questions that can be asked to customize the project.
// You can adjust this method to ask questions that are relevant to your
// project.
Expand All @@ -108,9 +107,10 @@ public static function questions(CustomizeCommand $command): array {
'Name' => [
// The question callback function defines how the question is asked.
// In this case, we ask the user to provide a package name as a string.
'question' => static fn(array $answers, CustomizeCommand $command): mixed => $command->io->ask('Package name', NULL, static function (string $value): string {
'question' => static fn(array $answers, CustomizeCommand $customizer): mixed => $customizer->io->ask('Package name', NULL, static function (string $value): string {
// This is a validation callback that checks if the package name is
// valid. If not, an exception is thrown.
// valid. If not, an exception is thrown with a message shown to the
// user.
if (!preg_match('/^[a-z0-9_.-]+\/[a-z0-9_.-]+$/', $value)) {
throw new \InvalidArgumentException(sprintf('The package name "%s" is invalid, it should be lowercase and have a vendor name, a forward slash, and a package name.', $value));
}
Expand All @@ -119,32 +119,34 @@ public static function questions(CustomizeCommand $command): array {
}),
// The process callback function defines how the answer is processed.
// The processing takes place only after all answers are received and
// the user confirms the changes.
'process' => static function (string $title, string $answer, array $answers, CustomizeCommand $command): void {
// the user confirms the intended changes.
'process' => static function (string $title, string $answer, array $answers, CustomizeCommand $customizer): void {
$name = is_string($customizer->packageData['name'] ?? NULL) ? $customizer->packageData['name'] : '';
// Update the package data.
$command->packageData['name'] = $answer;
$customizer->packageData['name'] = $answer;
// Write the updated composer.json file.
$command->writeComposerJson($command->packageData);
// Also, replace the package name in the project files.
$command->replaceInPath($command->cwd, $answer, $answer);
$customizer->writeComposerJson($customizer->packageData);
// Replace the package name in the project files.
$customizer->replaceInPath($customizer->cwd, $name, $answer);
},
],
'Description' => [
// For this question, we are using an answer from the previous question
// in the title of the question.
'question' => static fn(array $answers, CustomizeCommand $command): mixed => $command->io->ask(sprintf('Description for %s', $answers['Name'])),
'process' => static function (string $title, string $answer, array $answers, CustomizeCommand $command): void {
$command->packageData['description'] = $answer;
$command->writeComposerJson($command->packageData);
$command->replaceInPath($command->cwd, $answer, $answer);
'question' => static fn(array $answers, CustomizeCommand $customizer): mixed => $customizer->io->ask(sprintf('Description for %s', $answers['Name'])),
'process' => static function (string $title, string $answer, array $answers, CustomizeCommand $customizer): void {
$description = is_string($customizer->packageData['description'] ?? NULL) ? $customizer->packageData['description'] : '';
$customizer->packageData['description'] = $answer;
$customizer->writeComposerJson($customizer->packageData);
$customizer->replaceInPath($customizer->cwd, $description, $answer);
},
],
'License' => [
// For this question, we are using a predefined list of options.
// For this question, we are using a pre-defined list of options.
// For processing, we are using a separate method named 'processLicense'
// (only for the demonstration purposes; it could have been an
// anonymous function).
'question' => static fn(array $answers, CustomizeCommand $command): mixed => $command->io->choice('License type', [
'question' => static fn(array $answers, CustomizeCommand $customizer): mixed => $customizer->io->choice('License type', [
'MIT',
'GPL-3.0-or-later',
'Apache-2.0',
Expand All @@ -165,14 +167,14 @@ public static function questions(CustomizeCommand $command): array {
* The answer to the question.
* @param array<string,string> $answers
* All answers received so far.
* @param CustomizeCommand $command
* @param CustomizeCommand $customizer
* The command instance.
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public static function processLicense(string $title, string $answer, array $answers, CustomizeCommand $command): void {
$command->packageData['license'] = $answer;
$command->writeComposerJson($command->packageData);
public static function processLicense(string $title, string $answer, array $answers, CustomizeCommand $customizer): void {
$customizer->packageData['license'] = $answer;
$customizer->writeComposerJson($customizer->packageData);
}

}
5 changes: 3 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
/.gitattributes export-ignore
/.github export-ignore
/.gitignore export-ignore
/tests export-ignore
/logo.png export-ignore
/phpcs.xml export-ignore
/phpmd.xml export-ignore
/phpstan.neon export-ignore
/phpunit.xml export-ignore
/rector.php export-ignore
/renovate.json export-ignore
12 changes: 6 additions & 6 deletions CustomizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,14 +422,14 @@ protected function message(string $name, array $tokens = []): string {
/**
* Run a command.
*
* @param string $command
* @param string $cmd
* Command to run.
*
* @throws \Exception
* If the command fails.
*/
protected static function passthru(string $command): void {
passthru($command, $status);
protected static function passthru(string $cmd): void {
passthru($cmd, $status);
if ($status != 0) {
throw new \Exception('Command failed with exit code ' . $status);
}
Expand Down Expand Up @@ -601,7 +601,7 @@ public static function arrayUnsetDeep(array &$array, array $path, ?string $value
* class will **recursively replace** the messages defined here. This means
* that only specific messages may be overridden by the configuration class.
*
* @param CustomizeCommand $command
* @param CustomizeCommand $customizer
* The command instance.
*
* @return array<string,string|array<string>>
Expand All @@ -610,7 +610,7 @@ public static function arrayUnsetDeep(array &$array, array $path, ?string $value
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public static function messages(CustomizeCommand $command): array {
public static function messages(CustomizeCommand $customizer): array {
return [
'welcome' => 'Welcome to {{ package.name }} project customizer',
'banner' => [
Expand Down Expand Up @@ -656,7 +656,7 @@ public static function messages(CustomizeCommand $command): array {
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public static function questions(CustomizeCommand $command): array {
public static function questions(CustomizeCommand $customizer): array {
return [];
}

Expand Down
Loading