1111
1212namespace BlitzPHP \Database \Commands ;
1313
14- use BlitzPHP \Container \ Services ;
14+ use BlitzPHP \Database \ Connection \ BaseConnection ;
1515use BlitzPHP \Database \Seeder \Seeder ;
1616use InvalidArgumentException ;
1717
@@ -34,55 +34,138 @@ class Seed extends DatabaseCommand
3434 * {@inheritDoc}
3535 */
3636 protected array $ arguments = [
37- 'name ' => 'Nom du seedr a executer ' ,
37+ 'name ' => 'Nom du seeder à exécuter (ex: DatabaseSeeder ou Users \\ UserSeeder) ' ,
3838 ];
3939
40+ /**
41+ * {@inheritDoc}
42+ */
43+ protected array $ options = [
44+ '--group ' => 'Groupe de connexion à utiliser ' ,
45+ '--silent ' => 'Mode silencieux (pas de sortie) ' ,
46+ '--locale ' => 'Langue à utiliser pour Faker (ex: fr_FR, en_US) ' ,
47+ ];
48+
49+ protected ?BaseConnection $ db = null ;
50+
4051 /**
4152 * {@inheritDoc}
4253 */
4354 public function handle ()
4455 {
45- if (empty ($ name = $ this ->argument ('name ' ))) {
46- $ name = $ this ->prompt (lang ('Migrations.migSeeder ' ), null , static function ($ val ) {
56+ $ group = $ this ->option ('group ' );
57+ $ silent = $ this ->option ('silent ' ) !== null ;
58+ $ locale = $ this ->option ('locale ' , config ('app.language ' , 'fr_FR ' ));
59+
60+ $ this ->db = $ this ->resolver ->connect ($ group );
61+
62+ $ name = $ this ->getSeederName ();
63+
64+ $ seeder = $ this ->resolveSeeder ($ name );
65+
66+ $ this ->configureSeeder ($ seeder , $ locale , $ silent );
67+
68+ $ this ->runSeeder ($ seeder );
69+
70+ return EXIT_SUCCESS ;
71+ }
72+
73+ /**
74+ * Récupère le nom du seeder
75+ */
76+ protected function getSeederName (): string
77+ {
78+ if (null !== $ name = $ this ->argument ('name ' )) {
79+ return $ name ;
80+ }
81+
82+ return $ this ->prompt (
83+ 'Quel seeder souhaitez-vous exécuter ? ' ,
84+ 'DatabaseSeeder ' ,
85+ function ($ val ) {
4786 if (empty ($ val )) {
4887 throw new InvalidArgumentException ('Veuillez entrer le nom du seeder. ' );
4988 }
50-
5189 return $ val ;
52- });
53- }
90+ }
91+ );
92+ }
5493
55- $ seedClass = APP_NAMESPACE . '\Database\Seeds \\' ;
56- $ seedClass .= str_replace ($ seedClass , '' , $ name );
94+ /**
95+ * Résout la classe du seeder
96+ */
97+ protected function resolveSeeder (string $ name ): Seeder
98+ {
99+ // Chemins possibles
100+ $ paths = [
101+ APP_NAMESPACE . '\\Database \\Seeds \\' ,
102+ APP_NAMESPACE . '\\Database \\Seeders \\' ,
103+ 'Database \\Seeds \\' ,
104+ 'Database \\Seeders \\' ,
105+ ];
106+
107+ $ className = $ name ;
57108
58- /**
59- * @var Seeder
60- */
61- $ seeder = new $ seedClass ($ this ->db );
109+ // Si le nom ne contient pas de namespace, on essaie les chemins standards
110+ if (!str_contains ($ name , '\\' )) {
111+ foreach ($ paths as $ path ) {
112+ $ fullClass = $ path . $ name ;
113+ if (class_exists ($ fullClass )) {
114+ $ className = $ fullClass ;
115+ break ;
116+ }
117+ }
118+ }
62119
63- if ($ seeder ->getLocale () === '' ) {
64- $ seeder ->setLocale (config ('app.language ' ));
120+ if (!class_exists ($ className )) {
121+ throw new InvalidArgumentException (
122+ "Le seeder ' {$ name }' n'a pas été trouvé. \n" .
123+ "Chemins recherchés : \n" .
124+ implode ("\n" , array_map (fn ($ p ) => "- {$ p }{$ name }" , $ paths ))
125+ );
65126 }
66127
67- $ this ->task ('Demarrage du seed ' )->eol ();
68- sleep (2 );
69- $ this ->info ('Remplissage en cours de traitement ' );
128+ return new $ className ($ this ->db );
129+ }
70130
71- if (method_exists ($ seeder , 'run ' )) {
72- Services::container ()->call ([$ seeder , 'run ' ]);
131+ /**
132+ * Configure le seeder
133+ */
134+ protected function configureSeeder (Seeder $ seeder , ?string $ locale , bool $ silent ): void
135+ {
136+ if ($ locale !== null ) {
137+ $ seeder ->setLocale ($ locale );
138+ } elseif ($ seeder ->getLocale () === '' ) {
139+ $ seeder ->setLocale (config ('app.language ' , 'fr_FR ' ));
73140 }
74141
75- $ usedSeed = [
76- Services::container ()->call ([$ seeder , 'execute ' ]),
77- ...$ seeder ->getSeeded (),
142+ if ($ silent ) {
143+ $ seeder ->setSilent (true );
144+ }
145+ }
146+
147+ /**
148+ * Exécute le seeder
149+ */
150+ protected function runSeeder (Seeder $ seeder ): void
151+ {
152+ $ this ->task ('Démarrage du seed ' )->eol ();
153+
154+ $ this ->info ('Remplissage en cours... ' );
155+
156+ $ seeder ->setCommand ($ this )->execute ();
157+
158+ $ executed = [
159+ $ seeder ::class,
160+ ...$ seeder ->getCalled (),
78161 ];
79162
80- $ this ->eol ()->success ('Opération terminée. ' );
163+ $ this ->eol ()->success ('Opération terminée avec succès ! ' );
81164
82- foreach ($ usedSeed as $ seeded ) {
83- $ this ->eol ()->write ('- ' )->writer ->yellow ($ seeded );
84- }
165+ $ this ->eol ()->write ('Seeders exécutés : ' );
85166
86- return EXIT_SUCCESS ;
167+ foreach (array_unique ($ executed ) as $ seeded ) {
168+ $ this ->eol ()->write (' ✔ ' )->writer ->green ($ seeded );
169+ }
87170 }
88171}
0 commit comments