You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Actually if a Composer package is not installed the action is skipped by my plugin (auto-detection).
I wanted to have the same feature with PHIVE.
I've tried many solutions, and found a possible good one, but I'm not sure (if I used the good API calls): Could you check the sequence please ?
Source Code is provide at end of report.
Expected behaviour
Package is auto-detected (see my solution at end of this report with source code provided), respect version constraint, and command ./tools/phpcs is run !
Note
PHP_CodeSniffer is well detected and run as expected (I've put a var_dump not well indented that raise the error)
Current behaviour
Without my "patch", the Package is not detected as installed (even if it's TRUE : see phive status), and action is skipped (my plugin goal).
Phive 0.16.0 - Copyright (C) 2015-2026 by Arne Blankerts, Sebastian Heuer and Contributors
PHARs configured in project:
Alias/URL Version Constraint Installed Location Key Ids
phpcs ^4.0 4.0.1 /shared/backups/bartlett/captainhook-bin-plugin/tools/phpcs 97B02DD8E5071466
Source Code
Now, after a long introduction (sorry, but I want to be clear as much as possible), the code (PHIVE API calls , I used to auto-detect that the tool is correctly installed).
As for Composer Condition the key to check is the package identifier. For example : squizlabs/php_codesniffer for PHPCS
Tip
My Solution to by-pass the not found alias name candidate :
if (in_array('phpcbf', $aliases, true)) {
// "squizlabs/php_codesniffer" is the only Composer package that provide two aliases (for current Phive v0.16)// but only the first one is retrieved by \PharIo\Phive\SourcesList::getAliasForComposerAlias// @see https://github.com/phar-io/phive/blob/0.16.0/src/shared/sources/SourcesList.php#L66$aliases[] = 'phpcs';
}
Source Code
<?phpdeclare(strict_types=1);
namespaceBartlett\CaptainHookBinPlugin\Condition;
useCaptainHook\App\Console\IO;
useCaptainHook\App\Hook\Condition;
useComposer\InstalledVersions;
useComposer\Semver\Semver;
usePharIo\Phive\Cli\Request;
usePharIo\Phive\ComposerAlias;
usePharIo\Phive\EnvironmentLocator;
usePharIo\Phive\Factory;
usePharIo\Phive\LocalPhiveXmlConfig;
usePharIo\Phive\PhiveXmlConfigFileLocator;
usePharIo\Phive\StatusCommandConfig;
usePharIo\Phive\XmlFile;
usePharIo\Version\VersionConstraintParser;
useSebastianFeldmann\Git\Repository;
useThrowable;
usefunctionin_array;
useconstPHP_OS;
class PharInstalled implements Condition
{
publicfunction__construct(privatestring$packageName, privatestring$constraint = '*')
{
}
publicfunctionisTrue(IO$io, Repository$repository): bool
{
$isPhiveAvailable = InstalledVersions::isInstalled('phar-io/phive');
if (!$isPhiveAvailable) {
$io->write(
' <fg=cyan>Applied: PHIVE is not installed</>',
true,
IO::VERBOSE,
);
returnfalse;
}
$request = newRequest([]);
$factory = newFactory($request);
// \PharIo\Phive\Factory::getEnvironment$environment = (newEnvironmentLocator())->getEnvironment(PHP_OS);
// \PharIo\Phive\Factory::getConfig$config = $factory->getConfig();
// \PharIo\Phive\Factory::getOutput$output = $factory->getOutput();
// \PharIo\Phive\Factory::getPhiveXmlConfigFileLocator$xmlConfigFileLocator = newPhiveXmlConfigFileLocator(
$environment,
$config,
$output
);
$xmlConfig = newLocalPhiveXmlConfig(
newXmlFile(
$xmlConfigFileLocator->getFile(),
'https://phar.io/phive',
'phive'
),
newVersionConstraintParser(),
$environment
);
$pharRegistry = $factory->getPharRegistry();
$statusCommandConfig = newStatusCommandConfig(
$request->getOptions(),
$xmlConfig,
$pharRegistry
);
// \PharIo\Phive\Factory::getSourcesList$sourcesList = $factory->getRemoteSourcesListFileLoader()->load();
$isApplied = false;
$aliases = [];
try {
$aliases[] = $sourcesList->getAliasForComposerAlias(newComposerAlias($this->packageName));
if (in_array('phpcbf', $aliases, true)) {
// "squizlabs/php_codesniffer" is the only Composer package that provide two aliases (for current Phive v0.16)// but only the first one is retrieved by \PharIo\Phive\SourcesList::getAliasForComposerAlias// @see https://github.com/phar-io/phive/blob/0.16.0/src/shared/sources/SourcesList.php#L66//$aliases[] = 'phpcs';
}
foreach ($statusCommandConfig->getPhars() as$phar) {
if (!in_array($phar->getName(), $aliases, true)) {
continue;
}
if (!$phar->isInstalled()) {
continue;
}
$versionInstalled = $phar->getInstalledVersion();
\var_dump($versionInstalled->getOriginalString(), $phar->getVersionConstraint()->asString());
$isApplied = Semver::satisfies($versionInstalled->getOriginalString(), $phar->getVersionConstraint()->asString());
if ($isApplied) {
break;
}
}
} catch (Throwable$exception) {
}
$io->write(
sprintf(
' <fg=cyan>Applied: Package %s</> %s (with constraint "%s")',
$isApplied ? 'installed' : 'not installed',
$this->packageName,
$this->constraint
),
true,
IO::VERBOSE,
);
return$isApplied;
}
}
Hello @theseer
Perharps you can help me to confirm or not that the Phive API calls are on right sequence !
Hope it will be enough ? Tell me if you need more info !
Current Context
As author of package https://github.com/llaville/captainhook-bin-plugin (a CaptainHook Plugin), before to release officially the first version on Composer, I wanted to add support for PHIVE too !
Actually if a Composer package is not installed the action is skipped by my plugin (auto-detection).
I wanted to have the same feature with PHIVE.
I've tried many solutions, and found a possible good one, but I'm not sure (if I used the good API calls): Could you check the sequence please ?
Source Code is provide at end of report.
Expected behaviour
Package is auto-detected (see my solution at end of this report with source code provided), respect version constraint, and command
./tools/phpcsis run !Note
PHP_CodeSniffer is well detected and run as expected (I've put a
var_dumpnot well indented that raise the error)Current behaviour
Without my "patch", the Package is not detected as installed (even if it's TRUE : see
phive status), and action is skipped (my plugin goal).Caution
PHP_CodeSniffer is not detected because the first alias retrieved by
\PharIo\Phive\SourcesList::getAliasForComposerAliasisphpcbf(and notphpcs)See https://github.com/phar-io/phive/blob/0.16.0/src/shared/sources/SourcesList.php#L66 only the first alias as provided
PHIVE installation
I've locally installed the PHP_CodeSniffer as follow :
The local
.phive/phars.xmlfile contents is :Important
Here the alias (
phpcs) is the one that expected !Confirmed by
phive statuscommand :Source Code
Now, after a long introduction (sorry, but I want to be clear as much as possible), the code (PHIVE API calls , I used to auto-detect that the tool is correctly installed).
As for Composer Condition the key to check is the package identifier. For example :
squizlabs/php_codesnifferfor PHPCSTip
My Solution to by-pass the not found alias name candidate :
Source Code