1+ <?php
2+
3+ namespace Modulus \Console \Commands ;
4+
5+ use Modulus \Console \ModulusCLI ;
6+ use AtlantisPHP \Console \Command ;
7+ use Modulus \Framework \Migration ;
8+ use Illuminate \Database \Schema \Blueprint ;
9+ use Symfony \Component \Console \Helper \ProgressBar ;
10+ use Illuminate \Database \Capsule \Manager as Capsule ;
11+ use Symfony \Component \Console \Input \InputInterface ;
12+ use Symfony \Component \Console \Output \OutputInterface ;
13+
14+ class Seed extends Command
15+ {
16+ /**
17+ * The name and signature of the console command.
18+ *
19+ * @var string
20+ */
21+ protected $ signature = 'seed {name} {count=10} ' ;
22+
23+ /**
24+ * The full command description.
25+ *
26+ * @var string
27+ */
28+ protected $ help = 'This command allows you to create Database tables ' ;
29+
30+ /**
31+ * The descriptions of the console commands.
32+ *
33+ * @var array
34+ */
35+ protected $ descriptions = [
36+ 'seed ' => 'Run a seed ' ,
37+ 'name ' => 'The name of the seed ' ,
38+ 'count ' => 'The number of rows, the seed will create '
39+ ];
40+
41+ /**
42+ * @param InputInterface $input
43+ * @param OutputInterface $output
44+ *
45+ * @return void
46+ */
47+ protected function execute (InputInterface $ input , OutputInterface $ output )
48+ {
49+ $ name = strtolower ($ input ->getArgument ('name ' ));
50+ $ count = (int )$ input ->getOption ('count ' );
51+
52+ $ seeds = ModulusCLI::$ appdir . DIRECTORY_SEPARATOR . 'database ' . DIRECTORY_SEPARATOR . 'seeds ' . DIRECTORY_SEPARATOR ;
53+
54+ $ seed = $ seeds . $ name . '.php ' ;
55+
56+ if (file_exists ($ seed )) {
57+
58+ if (!is_int ($ count )) return $ output ->writeln ('<error>" ' .$ count .'" is not a real number</error> ' );
59+
60+ require $ seed ;
61+
62+ $ className = $ this ->className (substr ($ name , strrpos ($ name , '/ ' )));
63+
64+ if (class_exists ($ className )) {
65+ ProgressBar::setFormatDefinition ('count ' , 'Processing: %current%/%max%. ' );
66+
67+ $ progressBar = new ProgressBar ($ output , $ count );
68+ $ progressBar ->setFormat ('count ' );
69+ $ progressBar ->start ();
70+
71+ $ seed = new $ className ;
72+ $ seed ->count = $ count ;
73+ $ results = $ seed ->run ($ progressBar );
74+
75+ if (!$ results ) {
76+ return $ output ->writeln ('<error>Could not seed " ' . $ name . '"</error> ' );
77+ }
78+ }
79+
80+ return $ output ->writeln ('<info>" ' . $ name .'" was successful.</info> ' );
81+ }
82+ else {
83+ $ output ->writeln ('<error>" ' .$ seed .'" seed file does not exist</error> ' );
84+ }
85+ }
86+
87+ /**
88+ * Get class name
89+ *
90+ * @param string $fullname
91+ * @return string $class
92+ */
93+ private function className (string $ fullname ) : string
94+ {
95+ $ class = implode ('' , array_map ('ucfirst ' , explode ('_ ' , $ fullname )));
96+ return $ class ;
97+ }
98+ }
0 commit comments