@@ -68,6 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
68
68
->extract ()
69
69
->cleanUp ()
70
70
->updateParameters ()
71
+ ->updateComposerJson ()
71
72
->checkSymfonyRequirements ()
72
73
->displayInstallationResult ()
73
74
;
@@ -372,26 +373,6 @@ private function displayInstallationResult()
372
373
return $ this ;
373
374
}
374
375
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
-
395
376
/**
396
377
* Checks if environment meets symfony requirements
397
378
*
@@ -411,11 +392,25 @@ private function checkSymfonyRequirements()
411
392
return $ this ;
412
393
}
413
394
395
+ /**
396
+ * Updates the Symfony parameters.yml file to replace default configuration
397
+ * values with better generated values.
398
+ *
399
+ * @return NewCommand
400
+ */
414
401
private function updateParameters ()
415
402
{
416
403
$ filename = $ this ->projectDir .'/app/config/parameters.yml ' ;
417
404
418
405
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
+
419
414
return $ this ;
420
415
}
421
416
@@ -425,15 +420,64 @@ private function updateParameters()
425
420
return $ this ;
426
421
}
427
422
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 ()
429
430
{
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 ;
432
443
}
433
444
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 ;
435
453
}
436
454
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
+ */
437
481
private function getErrorMessage (\Requirement $ requirement , $ lineSize = 70 )
438
482
{
439
483
if ($ requirement ->isFulfilled ()) {
@@ -467,6 +511,45 @@ private function getInstalledSymfonyVersion()
467
511
}
468
512
}
469
513
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
+
470
553
/**
471
554
* Returns the executed command.
472
555
*
0 commit comments