Skip to content
This repository was archived by the owner on Nov 14, 2019. It is now read-only.

Commit 39516ce

Browse files
committed
feature #83 Installer now generates a good project name for composer.json (javiereguiluz)
This PR was squashed before being merged into the 1.0-dev branch (closes #83). Discussion ---------- Installer now generates a good project name for composer.json This fixes #78 and it's completely based on the [code used by Composer](https://github.com/composer/composer/blob/4a3bc58adfd501fa6e82c82fdd9e8d4036898fa1/src/Composer/Command/InitCommand.php#L172-186) and reported by @stof. In addition to the generation of the project name, this PR reorders and adds missing docs for some methods. Commits ------- d862e51 Installer now generates a good project name for composer.json
2 parents 865ec6a + d862e51 commit 39516ce

File tree

1 file changed

+107
-24
lines changed

1 file changed

+107
-24
lines changed

src/Symfony/Installer/NewCommand.php

Lines changed: 107 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6868
->extract()
6969
->cleanUp()
7070
->updateParameters()
71+
->updateComposerJson()
7172
->checkSymfonyRequirements()
7273
->displayInstallationResult()
7374
;
@@ -372,26 +373,6 @@ private function displayInstallationResult()
372373
return $this;
373374
}
374375

375-
/**
376-
* Utility method to show the number of bytes in a readable format.
377-
*
378-
* @param int $bytes The number of bytes to format
379-
*
380-
* @return string The human readable string of bytes (e.g. 4.32MB)
381-
*/
382-
private function formatSize($bytes)
383-
{
384-
$units = array('B', 'KB', 'MB', 'GB', 'TB');
385-
386-
$bytes = max($bytes, 0);
387-
$pow = $bytes ? floor(log($bytes, 1024)) : 0;
388-
$pow = min($pow, count($units) - 1);
389-
390-
$bytes /= pow(1024, $pow);
391-
392-
return number_format($bytes, 2).' '.$units[$pow];
393-
}
394-
395376
/**
396377
* Checks if environment meets symfony requirements
397378
*
@@ -411,11 +392,25 @@ private function checkSymfonyRequirements()
411392
return $this;
412393
}
413394

395+
/**
396+
* Updates the Symfony parameters.yml file to replace default configuration
397+
* values with better generated values.
398+
*
399+
* @return NewCommand
400+
*/
414401
private function updateParameters()
415402
{
416403
$filename = $this->projectDir.'/app/config/parameters.yml';
417404

418405
if (!is_writable($filename)) {
406+
if ($this->output->isVerbose()) {
407+
$this->output->writeln(sprintf(
408+
" <comment>[WARNING]</comment> The value of the <info>secret</info> configuration option cannot be updated because\n".
409+
" the <comment>%s</comment> file is not writable.\n",
410+
$filename
411+
));
412+
}
413+
419414
return $this;
420415
}
421416

@@ -425,15 +420,64 @@ private function updateParameters()
425420
return $this;
426421
}
427422

428-
private function generateRandomSecret()
423+
/**
424+
* Updates the composer.json file to provide better values for some of the
425+
* default configuration values.
426+
*
427+
* @return NewCommand
428+
*/
429+
private function updateComposerJson()
429430
{
430-
if (function_exists('openssl_random_pseudo_bytes')) {
431-
return hash('sha1', openssl_random_pseudo_bytes(23));
431+
$filename = $this->projectDir.'/composer.json';
432+
433+
if (!is_writable($filename)) {
434+
if ($this->output->isVerbose()) {
435+
$this->output->writeln(sprintf(
436+
" <comment>[WARNING]</comment> Project name cannot be configured because\n".
437+
" the <comment>%s</comment> file is not writable.\n",
438+
$filename
439+
));
440+
}
441+
442+
return $this;
432443
}
433444

434-
return hash('sha1', uniqid(mt_rand(), true));
445+
$ret = str_replace(
446+
'"name": "symfony/framework-standard-edition",',
447+
sprintf('"name": "%s",', $this->generateComposerProjectName()),
448+
file_get_contents($filename)
449+
);
450+
file_put_contents($filename, $ret);
451+
452+
return $this;
435453
}
436454

455+
/**
456+
* Utility method to show the number of bytes in a readable format.
457+
*
458+
* @param int $bytes The number of bytes to format
459+
*
460+
* @return string The human readable string of bytes (e.g. 4.32MB)
461+
*/
462+
private function formatSize($bytes)
463+
{
464+
$units = array('B', 'KB', 'MB', 'GB', 'TB');
465+
466+
$bytes = max($bytes, 0);
467+
$pow = $bytes ? floor(log($bytes, 1024)) : 0;
468+
$pow = min($pow, count($units) - 1);
469+
470+
$bytes /= pow(1024, $pow);
471+
472+
return number_format($bytes, 2).' '.$units[$pow];
473+
}
474+
475+
/**
476+
* Formats the error message contained in the given Requirement item
477+
* using the optional line length provided.
478+
*
479+
* @return string
480+
*/
437481
private function getErrorMessage(\Requirement $requirement, $lineSize = 70)
438482
{
439483
if ($requirement->isFulfilled()) {
@@ -467,6 +511,45 @@ private function getInstalledSymfonyVersion()
467511
}
468512
}
469513

514+
/**
515+
* Generates a good random value for Symfony's 'secret' option
516+
*
517+
* @return string
518+
*/
519+
private function generateRandomSecret()
520+
{
521+
if (function_exists('openssl_random_pseudo_bytes')) {
522+
return hash('sha1', openssl_random_pseudo_bytes(23));
523+
}
524+
525+
return hash('sha1', uniqid(mt_rand(), true));
526+
}
527+
528+
/**
529+
* Generates a good Composer project name based on the application name
530+
* and on the user name.
531+
*
532+
* @return string
533+
*/
534+
private function generateComposerProjectName()
535+
{
536+
$name = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $this->projectName);
537+
$name = strtolower($name);
538+
539+
if (!empty($_SERVER['USERNAME'])) {
540+
$name = $_SERVER['USERNAME'].'/'.$name;
541+
} elseif ($user = posix_getpwuid(posix_getuid())) {
542+
$name = $user['name'].'/'.$name;
543+
} elseif (get_current_user()) {
544+
$name = get_current_user().'/'.$name;
545+
} else {
546+
// package names must be in the format foo/bar
547+
$name = $name.'/'.$name;
548+
}
549+
550+
return $name;
551+
}
552+
470553
/**
471554
* Returns the executed command.
472555
*

0 commit comments

Comments
 (0)