setPathType($pathType); } public function getQuestion(): Question { $question = $this->createQuestion(); $question->setAutocompleterCallback(static function (string $userInput): array { // Strip any characters from the last slash to the end of the string // to keep only the last directory and generate suggestions for it $inputPath = preg_replace('%(/|^)[^/]*$%', '$1', $userInput); $inputPath = $inputPath === '' ? '.' : $inputPath; $inputPath = rtrim($inputPath, '/\\') . '/'; $foundFilesAndDirs = is_dir($inputPath) ? scandir($inputPath) : []; return array_map(static fn(string $dirOrFile): string => $inputPath . $dirOrFile, $foundFilesAndDirs); }); $mustExist = $this->mustExist; $type = $this->type; $question->setValidator( /** @param mixed $value */ static function ($value) use ($mustExist, $type): string { Assert::string($value, sprintf('Invalid value: string expected, %s given', get_debug_type($value))); if (! $mustExist) { // No further checks needed return $value; } Assert::fileExists($value, 'Path does not exist'); if ($type === self::TYPE_DIR) { Assert::directory($value, 'Path is not a valid directory'); } return $value; } ); return $question; } public function setPathMustExist(bool $flag): self { $this->mustExist = $flag; return $this; } private function setPathType(string $type): void { if (! in_array($type, [self::TYPE_DIR, self::TYPE_FILE], true)) { throw new InvalidArgumentException(sprintf( 'Invalid type provided: expected "%s" or "%s"', self::TYPE_DIR, self::TYPE_FILE )); } $this->type = $type; } }