Skip to content

Commit afc5f0d

Browse files
authored
Merge pull request #407 from iMattPro/fix-composer-installers
Fix composer installers from overwriting author intent
2 parents bfd304c + 1596214 commit afc5f0d

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

contribution/extension/type.php

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ protected function repack(package $package, \titania_contribution $contrib, \tit
280280

281281
$ext_name = $data['name'];
282282
$data['type'] = 'phpbb-extension';
283-
$data = $this->update_phpbb_requirement($data);
283+
$data = $this->update_phpbb_requirement($data, $revision);
284284
$data = $this->set_version_check($data, $contrib);
285285

286286
$data = json_encode(
@@ -352,9 +352,10 @@ protected function set_version_check(array $data, \titania_contribution $contrib
352352
* Updates phpBB requirements in composer.json
353353
*
354354
* @param array $data composer.json data
355+
* @param \titania_revision $revision
355356
* @return array Returns $data array with phpBB requirement updated
356357
*/
357-
protected function update_phpbb_requirement(array $data)
358+
protected function update_phpbb_requirement(array $data, \titania_revision $revision)
358359
{
359360
if (!isset($data['require']['phpbb/phpbb']))
360361
{
@@ -371,7 +372,15 @@ protected function update_phpbb_requirement(array $data)
371372
}
372373

373374
// Composer installers must be required by all extensions in order to be installed correctly
374-
$data['require']['composer/installers'] = '~1.0.0';
375+
if (!isset($data['require']['composer/installers']))
376+
{
377+
$installers_version = '~1.0';
378+
if ($this->is_phpbb_4_branch($revision) || (isset($data['require']['phpbb/phpbb']) && $this->requires_phpbb_4($data['require']['phpbb/phpbb'])))
379+
{
380+
$installers_version = '^1.0 || ^2.0';
381+
}
382+
$data['require']['composer/installers'] = $installers_version;
383+
}
375384

376385
return $data;
377386
}
@@ -387,6 +396,40 @@ protected function is_stable_version($version)
387396
return preg_match('#^\d+\.\d+\.\d+(-pl\d+)?$#i', $version) === 1 && phpbb_version_compare($version, '1.0.0', '>=');
388397
}
389398

399+
/**
400+
* Check if phpBB requires 4.0.0 or higher
401+
*
402+
* @param string $constraint Version constraint
403+
* @return bool True if constraint requires phpBB 4.0.0+
404+
*/
405+
protected function requires_phpbb_4($constraint)
406+
{
407+
try
408+
{
409+
$parser = new \Composer\Semver\VersionParser();
410+
$constraint_obj = $parser->parseConstraints($constraint);
411+
$phpbb4_constraint = $parser->parseConstraints('>=4.0.0');
412+
$phpbb3_constraint = $parser->parseConstraints('<4.0.0');
413+
// Check if constraint allows 4.0.0+ and excludes all versions below 4.0.0
414+
return $constraint_obj->matches($phpbb4_constraint) && !$constraint_obj->matches($phpbb3_constraint);
415+
}
416+
catch (\Exception $e)
417+
{
418+
return false;
419+
}
420+
}
421+
422+
/**
423+
* Check if revision is submitted to phpBB 4.0+ branch
424+
*
425+
* @param \titania_revision $revision
426+
* @return bool True if submitted to phpBB 4.0+ branch
427+
*/
428+
protected function is_phpbb_4_branch(\titania_revision $revision)
429+
{
430+
return max(array_column($revision->phpbb_versions, 'phpbb_version_branch')) >= 40;
431+
}
432+
390433
/**
391434
* Get prevalidator
392435
*

0 commit comments

Comments
 (0)