0 follower

Final Class Yiisoft\YiiDevTool\App\Command\SwitchCommand

InheritanceYiisoft\YiiDevTool\App\Command\SwitchCommand » Yiisoft\YiiDevTool\App\Component\Console\PackageCommand » Symfony\Component\Console\Command\Command

Protected Methods

Hide inherited methods

Method Description Defined By
afterProcessingPackages() Override this method in a subclass if you want to do something after processing the packages. Yiisoft\YiiDevTool\App\Component\Console\PackageCommand
areTargetPackagesSpecifiedExplicitly() Yiisoft\YiiDevTool\App\Component\Console\PackageCommand
beforeProcessingPackages() Override this method in a subclass if you want to do something before processing the packages. Yiisoft\YiiDevTool\App\Component\Console\PackageCommand
checkSSHConnection() Yiisoft\YiiDevTool\App\Component\Console\PackageCommand
configure() Yiisoft\YiiDevTool\App\Command\SwitchCommand
doesPackageContainErrors() Yiisoft\YiiDevTool\App\Component\Console\PackageCommand
execute() Yiisoft\YiiDevTool\App\Command\SwitchCommand
getAppRootDir() Use this method to get a root directory of the tool. Yiisoft\YiiDevTool\App\Component\Console\PackageCommand
getErrorsList() Yiisoft\YiiDevTool\App\Component\Console\PackageCommand
getExampleCommandPrefix() Yiisoft\YiiDevTool\App\Component\Console\PackageCommand
getIO() Yiisoft\YiiDevTool\App\Component\Console\PackageCommand
getMessageWhenNothingHasBeenOutput() Override this method in a subclass if you want to output something to the console in cases where a command did not output anything during its execution. Yiisoft\YiiDevTool\App\Component\Console\PackageCommand
getPackageList() Yiisoft\YiiDevTool\App\Component\Console\PackageCommand
getTargetPackages() Yiisoft\YiiDevTool\App\Component\Console\PackageCommand
initPackageList() Yiisoft\YiiDevTool\App\Component\Console\PackageCommand
initTargetPackages() Yiisoft\YiiDevTool\App\Component\Console\PackageCommand
initialize() Yiisoft\YiiDevTool\App\Component\Console\PackageCommand
processPackage() This method in a subclass should implement the processing logic of each package. Yiisoft\YiiDevTool\App\Component\Console\PackageCommand
registerPackageError() Yiisoft\YiiDevTool\App\Component\Console\PackageCommand

Method Details

Hide inherited methods

afterProcessingPackages() protected method

Defined in: Yiisoft\YiiDevTool\App\Component\Console\PackageCommand::afterProcessingPackages()

Override this method in a subclass if you want to do something after processing the packages.

For example, link the packages with each other.

protected void afterProcessingPackages ( \Symfony\Component\Console\Input\InputInterface $input )
$input \Symfony\Component\Console\Input\InputInterface

                protected function afterProcessingPackages(InputInterface $input): void
{
}

            
areTargetPackagesSpecifiedExplicitly() protected method
protected boolean areTargetPackagesSpecifiedExplicitly ( )

                protected function areTargetPackagesSpecifiedExplicitly(): bool
{
    if ($this->targetPackagesSpecifiedExplicitly === null) {
        throw new RuntimeException('Target packages are not initialized.');
    }
    return $this->targetPackagesSpecifiedExplicitly;
}

            
beforeProcessingPackages() protected method

Defined in: Yiisoft\YiiDevTool\App\Component\Console\PackageCommand::beforeProcessingPackages()

Override this method in a subclass if you want to do something before processing the packages.

For example, check the input arguments.

protected void beforeProcessingPackages ( \Symfony\Component\Console\Input\InputInterface $input )
$input \Symfony\Component\Console\Input\InputInterface

                protected function beforeProcessingPackages(InputInterface $input): void
{
}

            
checkSSHConnection() protected method
protected boolean checkSSHConnection ( )

                protected function checkSSHConnection(): bool
{
    $process = new Process(['ssh', '-T', 'git@github.com']);
    $process
        ->setTimeout(null)
        ->run();
    if ($process->getExitCode() !== 1) {
        $this
            ->getIO()
            ->error([
                'Checking access to github.com ... DENIED',
                'Error: ' . $process->getErrorOutput(),
                'Seems like you have not installed SSH key to you Github account.',
                'Key is required to work with repository via SSH.',
                'See here for instructions: https://docs.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account',
            ]);
        return false;
    }
    return true;
}

            
configure() protected method

protected void configure ( )

                protected function configure(): void
{
    $this->addArgument(
        'packages',
        InputArgument::REQUIRED,
        <<<DESCRIPTION
        Package names separated by commas. For example: <fg=cyan;options=bold>rbac,di,demo,db-mysql</>
        Array keys from <fg=blue;options=bold>package.php</> configuration can be specified.</>
        DESCRIPTION
    );
}

            
doesPackageContainErrors() protected method
protected boolean doesPackageContainErrors ( Yiisoft\YiiDevTool\App\Component\Package\Package $package )
$package Yiisoft\YiiDevTool\App\Component\Package\Package

                protected function doesPackageContainErrors(Package $package): bool
{
    return $this->errorList->has($package);
}

            
execute() protected method

protected integer execute ( \Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output )
$input \Symfony\Component\Console\Input\InputInterface
$output \Symfony\Component\Console\Output\OutputInterface

                protected function execute(InputInterface $input, OutputInterface $output): int
{
    $this->initPackageList();
    $io = $this->getIO();
    $packageList = $this->getPackageList();
    $enablePackageIds = array_unique(explode(',', (string) $input->getArgument('packages')));
    $enablePackageIds = array_filter($enablePackageIds, static fn ($id) => !empty($id));
    if (empty($enablePackageIds)) {
        $io->error('Please, specify packages separated by commas.');
        return Command::FAILURE;
    }
    foreach ($enablePackageIds as $packageId) {
        if (!$packageList->hasPackage($packageId)) {
            $io->error('Package "' . $packageId . '" not found.');
            return Command::FAILURE;
        }
    }
    $enabledPackages = [];
    $disabledPackages = [];
    foreach ($packageList->getAllPackages() as $packageId => $package) {
        if (in_array($packageId, $enablePackageIds, true)) {
            $package->setEnabled(true);
            $enabledPackages[] = $packageId;
        } elseif ($package->enabled()) {
            $package->setEnabled(false);
            $disabledPackages[] = $packageId;
        }
    }
    $tree = $packageList->getTree();
    $dump = VarDumper::create($tree)->export();
    $handle = fopen(dirname(__DIR__, 3) . '/packages.local.php', 'w+');
    fwrite($handle, '<?php' . "\n\n");
    fwrite($handle, 'return ' . $dump . ';');
    fclose($handle);
    $io->success("\n + " . implode("\n + ", $enabledPackages));
    if (!empty($disabledPackages)) {
        $io->error("\n — " . implode("\n — ", $disabledPackages));
    }
    return Command::SUCCESS;
}

            
getAppRootDir() protected method

Defined in: Yiisoft\YiiDevTool\App\Component\Console\PackageCommand::getAppRootDir()

Use this method to get a root directory of the tool.

Commands and components can be moved as a result of refactoring, so you should not rely on their location in the file system.

protected string getAppRootDir ( )
return string

Path to the root directory of the tool WITH a TRAILING SLASH.

                protected function getAppRootDir(): string
{
    return rtrim($this
            ->getApplication()
            ->getRootDir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}

            
getErrorsList() protected method
protected Yiisoft\YiiDevTool\App\Component\Package\PackageErrorList getErrorsList ( )

                protected function getErrorsList(): PackageErrorList
{
    return $this->errorList;
}

            
getExampleCommandPrefix() protected method
protected string getExampleCommandPrefix ( )
return string

Console command prefix that works in current environment.

                protected function getExampleCommandPrefix(): string
{
    $shell = getenv('SHELL');
    $isBash = ($shell && stripos($shell, 'bash')) !== false;
    return $isBash ? './' : '';
}

            
getIO() protected method
protected Yiisoft\YiiDevTool\App\Component\Console\OutputManager getIO ( )

                protected function getIO(): OutputManager
{
    if ($this->io === null) {
        throw new RuntimeException('IO is not initialized.');
    }
    return $this->io;
}

            
getMessageWhenNothingHasBeenOutput() protected method

Defined in: Yiisoft\YiiDevTool\App\Component\Console\PackageCommand::getMessageWhenNothingHasBeenOutput()

Override this method in a subclass if you want to output something to the console in cases where a command did not output anything during its execution.

protected string|null getMessageWhenNothingHasBeenOutput ( )
return string|null

The message to be displayed. If null, then nothing will be output.

                protected function getMessageWhenNothingHasBeenOutput(): ?string
{
    return null;
}

            
getPackageList() protected method
protected Yiisoft\YiiDevTool\App\Component\Package\PackageList getPackageList ( )

                protected function getPackageList(): PackageList
{
    return $this->packageList;
}

            
getTargetPackages() protected method
protected Yiisoft\YiiDevTool\App\Component\Package\Package[] getTargetPackages ( )

                protected function getTargetPackages(): array
{
    if ($this->targetPackages === null) {
        throw new RuntimeException('Target packages are not initialized.');
    }
    return $this->targetPackages;
}

            
initPackageList() protected method
protected void initPackageList ( )

                protected function initPackageList(): void
{
    $io = $this->getIO();
    try {
        $ownerPackages = require $this->getAppRootDir() . 'owner-packages.php';
        if (!preg_match('/^[a-z0-9][a-z0-9-]*[a-z0-9]$/i', $ownerPackages)) {
            $io->error([
                'The packages owner can only contain the characters [a-z0-9-], and the character \'-\' cannot appear at the beginning or at the end.',
                'See <file>owner-packages.php</file> to set the packages owner.',
            ]);
            exit(1);
        }
        $packagesRootDir = $this->getApplication()->getConfig('packagesRootDir') ??  $this->getAppRootDir() . 'dev';
        $this->packageList = new PackageList(
            $ownerPackages,
            $this->getAppRootDir() . 'packages.php',
            packagesRootDir: $packagesRootDir,
        );
        $this->errorList = new PackageErrorList();
    } catch (InvalidArgumentException $e) {
        $io->error([
            'Invalid local package configuration <file>packages.local.php</file>',
            $e->getMessage(),
            'See <file>packages.local.php.example</file> for configuration examples.',
        ]);
        exit(1);
    }
}

            
initTargetPackages() protected method
protected void initTargetPackages ( \Symfony\Component\Console\Input\InputInterface $input )
$input \Symfony\Component\Console\Input\InputInterface

                protected function initTargetPackages(InputInterface $input): void
{
    if ($this->packageList === null) {
        throw new RuntimeException('Package list is not initialized.');
    }
    $io = $this->getIO();
    $commaSeparatedPackageIds = $input->getArgument('packages');
    if ($commaSeparatedPackageIds === null) {
        $this->targetPackagesSpecifiedExplicitly = false;
        $this->targetPackages = $this->packageList->getEnabledPackages();
        return;
    }
    $targetPackageIds = array_unique(explode(',', $commaSeparatedPackageIds));
    $problemsFound = false;
    $targetPackages = [];
    foreach ($targetPackageIds as $targetPackageId) {
        $package = $this->packageList->getPackage($targetPackageId);
        if ($package === null) {
            $io->error("Package <package>$targetPackageId</package> not found in <file>packages.php</file>");
            $problemsFound = true;
            continue;
        }
        if ($package->disabled()) {
            $io->error("Package <package>$targetPackageId</package> disabled in <file>packages.local.php</file>");
            $problemsFound = true;
            continue;
        }
        $targetPackages[] = $package;
    }
    if ($problemsFound) {
        exit(1);
    }
    $this->targetPackagesSpecifiedExplicitly = true;
    $this->targetPackages = $targetPackages;
}

            
initialize() protected method
protected mixed initialize ( \Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output )
$input \Symfony\Component\Console\Input\InputInterface
$output \Symfony\Component\Console\Output\OutputInterface

                protected function initialize(InputInterface $input, OutputInterface $output)
{
    $this->io = new OutputManager(new YiiDevToolStyle($input, $output));
}

            
processPackage() protected method

Defined in: Yiisoft\YiiDevTool\App\Component\Console\PackageCommand::processPackage()

This method in a subclass should implement the processing logic of each package.

protected void processPackage ( Yiisoft\YiiDevTool\App\Component\Package\Package $package )
$package Yiisoft\YiiDevTool\App\Component\Package\Package

                protected function processPackage(Package $package): void
{
    throw new RuntimeException('Package processing logic is not implemented.');
}

            
registerPackageError() protected method
protected void registerPackageError ( Yiisoft\YiiDevTool\App\Component\Package\Package $package, string $message, string $during )
$package Yiisoft\YiiDevTool\App\Component\Package\Package
$message string
$during string

                protected function registerPackageError(Package $package, string $message, string $during): void
{
    $this->errorList->set($package, $message, $during);
}