Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 729aee0

Browse files
committed
feature symfony#363 Switched project id to be opt-in (fabpot)
This PR was squashed before being merged into the 1.0-dev branch (closes symfony#363). Discussion ---------- Switched project id to be opt-in Right now, we generate a unique id for each new project (or if a project does not have one already). But as we are only using it server side for restricting private recipes, let's move to an opt-in way. I didn't do that at first as it means having one more command in the composer command + one thing to do. Commits ------- 255f155 fixed command descriptions a06b229 switched project id to be opt-in
2 parents da7f0d0 + 255f155 commit 729aee0

4 files changed

Lines changed: 62 additions & 25 deletions

File tree

src/Command/FixRecipesCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ public function __construct(/* cannot be type-hinted */ $flex)
3232

3333
protected function configure()
3434
{
35-
$this->setName('fix-recipes')
36-
->setDescription('Install missing recipes.')
35+
$this->setName('symfony:fix-recipes')
36+
->setAliases(['fix-recipes'])
37+
->setDescription('Installs missing recipes.')
3738
;
3839
}
3940

src/Command/GenerateIdCommand.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Flex\Command;
13+
14+
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use Symfony\Component\Console\Output\OutputInterface;
17+
18+
class GenerateIdCommand extends Command
19+
{
20+
private $flex;
21+
22+
public function __construct(/* cannot be type-hinted */ $flex)
23+
{
24+
$this->flex = $flex;
25+
26+
parent::__construct();
27+
}
28+
29+
protected function configure()
30+
{
31+
$this->setName('symfony:generate-id')
32+
->setDescription('Generates a unique ID for this project.')
33+
;
34+
}
35+
36+
protected function execute(InputInterface $input, OutputInterface $output)
37+
{
38+
$this->flex->generateFlexId();
39+
}
40+
}

src/Command/UnpackCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ public function __construct(PackageResolver $resolver)
3737

3838
protected function configure()
3939
{
40-
$this->setName('unpack')
41-
->setDescription('Unpack a Symfony pack.')
40+
$this->setName('symfony:unpack')
41+
->setAliases(['unpack'])
42+
->setDescription('Unpacks a Symfony pack.')
4243
->setDefinition([
4344
new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'Installed packages to unpack.'),
4445
new InputOption('sort-packages', null, InputOption::VALUE_NONE, 'Sorts packages'),

src/Flex.php

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ public function activate(Composer $composer, IOInterface $io)
190190
$app->add(new Command\RemoveCommand($resolver));
191191
$app->add(new Command\UnpackCommand($resolver));
192192
$app->add(new Command\FixRecipesCommand($this));
193+
$app->add(new Command\GenerateIdCommand($this));
193194

194195
break;
195196
}
@@ -500,6 +501,20 @@ public function onFileDownload(PreFileDownloadEvent $event)
500501
}
501502
}
502503

504+
public function generateFlexId()
505+
{
506+
if ($this->getFlexId()) {
507+
return;
508+
}
509+
510+
$json = new JsonFile(Factory::getComposerFile());
511+
$manipulator = new JsonManipulator(file_get_contents($json->getPath()));
512+
$manipulator->addSubNode('extra', 'symfony.id', $this->downloader->get('/ulid')->getBody()['ulid']);
513+
file_put_contents($json->getPath(), $manipulator->getContents());
514+
515+
$this->updateComposerLock();
516+
}
517+
503518
private function fetchRecipes(): array
504519
{
505520
$devPackages = null;
@@ -568,27 +583,7 @@ private function getFlexId()
568583
{
569584
$extra = $this->composer->getPackage()->getExtra();
570585

571-
// don't want to be registered
572-
if (getenv('SYMFONY_SKIP_REGISTRATION') || !isset($extra['symfony']['id'])) {
573-
return null;
574-
}
575-
576-
// already registered
577-
if ($extra['symfony']['id']) {
578-
return $extra['symfony']['id'];
579-
}
580-
581-
// get a new ID
582-
$id = $this->downloader->get('/ulid')->getBody()['ulid'];
583-
584-
// update composer.json
585-
$json = new JsonFile(Factory::getComposerFile());
586-
$manipulator = new JsonManipulator(file_get_contents($json->getPath()));
587-
$manipulator->addSubNode('extra', 'symfony.id', $id);
588-
file_put_contents($json->getPath(), $manipulator->getContents());
589-
$this->shouldUpdateComposerLock = true;
590-
591-
return $id;
586+
return $extra['symfony']['id'] ?? null;
592587
}
593588

594589
private function formatOrigin(string $origin): string

0 commit comments

Comments
 (0)