Piwik\Plugins\CoreConsole\Commands\GeneratePluginBase::checkAndUpdateRequiredPiwikVersion PHP Method

checkAndUpdateRequiredPiwikVersion() protected method

protected checkAndUpdateRequiredPiwikVersion ( $pluginName, Symfony\Component\Console\Output\OutputInterface $output )
$output Symfony\Component\Console\Output\OutputInterface
    protected function checkAndUpdateRequiredPiwikVersion($pluginName, OutputInterface $output)
    {
        $pluginJsonPath = $this->getPluginPath($pluginName) . '/plugin.json';
        $relativePluginJson = $this->getRelativePluginPath($pluginName) . '/plugin.json';
        if (!file_exists($pluginJsonPath) || !is_writable($pluginJsonPath)) {
            return;
        }
        $pluginJson = file_get_contents($pluginJsonPath);
        $pluginJson = json_decode($pluginJson, true);
        if (empty($pluginJson)) {
            return;
        }
        if (empty($pluginJson['require'])) {
            $pluginJson['require'] = array();
        }
        $piwikVersion = Version::VERSION;
        $nextMajorVersion = (int) substr($piwikVersion, 0, strpos($piwikVersion, '.')) + 1;
        $secondPartPiwikVersionRequire = ',<' . $nextMajorVersion . '.0.0-b1';
        if (false === strpos($piwikVersion, '-')) {
            // see https://github.com/composer/composer/issues/4080 we need to specify -stable otherwise it would match
            // $piwikVersion-dev meaning it would also match all pre-released. However, we only want to match a stable
            // release
            $piwikVersion .= '-stable';
        }
        $newRequiredVersion = sprintf('>=%s,<%d.0.0', $piwikVersion, $nextMajorVersion);
        if (!empty($pluginJson['require']['piwik'])) {
            $requiredVersion = trim($pluginJson['require']['piwik']);
            if ($requiredVersion === $newRequiredVersion) {
                // there is nothing to updated
                return;
            }
            // our generated versions look like ">=2.25.4,<3.0.0-b1".
            // We only updated the Piwik version in the first part if the piwik version looks like that or if it has only
            // one piwik version defined. In all other cases, eg user uses || etc we do not update it as user has customized
            // the piwik version.
            foreach (['<>', '!=', '<=', '==', '^'] as $comparison) {
                if (strpos($requiredVersion, $comparison) === 0) {
                    // user is using custom piwik version require, we do not overwrite anything.
                    return;
                }
            }
            if (strpos($requiredVersion, '||') !== false || strpos($requiredVersion, ' ') !== false) {
                // user is using custom piwik version require, we do not overwrite anything.
                return;
            }
            $requiredPiwikVersions = explode(',', (string) $requiredVersion);
            $numRequiredPiwikVersions = count($requiredPiwikVersions);
            if ($numRequiredPiwikVersions > 2) {
                // user is using custom piwik version require, we do not overwrite anything.
                return;
            }
            if ($numRequiredPiwikVersions === 2 && !Common::stringEndsWith($requiredVersion, $secondPartPiwikVersionRequire)) {
                // user is using custom piwik version require, we do not overwrite anything
                return;
            }
            // if only one piwik version is defined we update it to make sure it does now specify an upper version limit
            $dependency = new Dependency();
            $missingVersion = $dependency->getMissingVersions($piwikVersion, $requiredVersion);
            if (!empty($missingVersion)) {
                $msg = sprintf('We cannot generate this component as the plugin "%s" requires the Piwik version "%s" in the file "%s". Generating this component requires "%s". If you know your plugin is compatible with your Piwik version remove the required Piwik version in "%s" and try to execute this command again.', $pluginName, $requiredVersion, $relativePluginJson, $newRequiredVersion, $relativePluginJson);
                throw new \Exception($msg);
            }
            $output->writeln('');
            $output->writeln(sprintf('<comment>We have updated the required Piwik version from "%s" to "%s" in "%s".</comment>', $requiredVersion, $newRequiredVersion, $relativePluginJson));
        } else {
            $output->writeln('');
            $output->writeln(sprintf('<comment>We have updated your "%s" to require the Piwik version "%s".</comment>', $relativePluginJson, $newRequiredVersion));
        }
        $pluginJson['require']['piwik'] = $newRequiredVersion;
        file_put_contents($pluginJsonPath, $this->toJson($pluginJson));
    }