1+ <?php
2+
3+ namespace Sadegh19b \LaravelIranCities \Commands ;
4+
5+ use Illuminate \Console \Command ;
6+ use Illuminate \Support \Facades \File ;
7+
8+ class GenerateCommand extends Command
9+ {
10+ /**
11+ * The name and signature of the console command.
12+ *
13+ * @var string
14+ */
15+ protected $ signature = 'iran-cities:generate
16+ {--models : Generate Province and City models}
17+ {--migrations : Generate migrations for provinces and cities tables}
18+ {--seeder : Generate Iran Provinces and Cities seeder}
19+ {--all : Generate all files (models, migrations, and seeders)} ' ;
20+
21+ /**
22+ * The console command description.
23+ *
24+ * @var string
25+ */
26+ protected $ description = 'Generate Iran Provinces and Cities models, migrations, and seeders ' ;
27+
28+ private $ modelNamespace = 'App \\Models ' ;
29+
30+ /**
31+ * Execute the console command.
32+ */
33+ public function handle ()
34+ {
35+ if ($ this ->option ('all ' )) {
36+ $ this ->makeModels ();
37+ $ this ->makeMigrations ();
38+ $ this ->makeSeeder ();
39+
40+ $ this ->info ('All Province City files have been generated. ' );
41+ return ;
42+ }
43+
44+ if ($ this ->option ('models ' )) {
45+ $ this ->makeModels ();
46+ }
47+
48+ if ($ this ->option ('migrations ' )) {
49+ $ this ->makeMigrations ();
50+ }
51+
52+ if ($ this ->option ('seeder ' )) {
53+ $ this ->makeSeeder ();
54+ }
55+
56+ if (!$ this ->option ('models ' ) && !$ this ->option ('migrations ' ) && !$ this ->option ('seeder ' )) {
57+ $ this ->error ('Please specify what to generate (--model, --migration, --seeder, or --all) ' );
58+ }
59+ }
60+
61+ /**
62+ * Generate models from stubs.
63+ *
64+ * @return void
65+ */
66+ protected function makeModels (): void
67+ {
68+ $ this ->info ('Generating Province and City models... ' );
69+
70+ // Generate Province model
71+ $ this ->generateFromStub (
72+ $ this ->findStub ('models/Province.stub ' ),
73+ app_path ('Models/Province.php ' ),
74+ ['{{ namespace }} ' => $ this ->modelNamespace ]
75+ );
76+
77+ // Generate City model
78+ $ this ->generateFromStub (
79+ $ this ->findStub ('models/City.stub ' ),
80+ app_path ('Models/City.php ' ),
81+ ['{{ namespace }} ' => $ this ->modelNamespace ]
82+ );
83+
84+ $ this ->info ('Models generated successfully. ' );
85+ }
86+
87+ /**
88+ * Generate migrations from stubs.
89+ *
90+ * @return void
91+ */
92+ protected function makeMigrations (): void
93+ {
94+ $ this ->info ('Generating migrations... ' );
95+
96+ // Generate provinces migration
97+ $ provincesTimestamp = date ('Y_m_d_His ' );
98+ $ this ->generateFromStub (
99+ $ this ->findStub ('migrations/create_provinces_table.stub ' ),
100+ database_path ("migrations/ {$ provincesTimestamp }_create_provinces_table.php " ),
101+ ['{{ model_namespace }} ' => $ this ->modelNamespace ]
102+ );
103+
104+ // Generate cities migration with a timestamp 1 second later
105+ sleep (1 );
106+ $ citiesTimestamp = date ('Y_m_d_His ' );
107+ $ this ->generateFromStub (
108+ $ this ->findStub ('migrations/create_cities_table.stub ' ),
109+ database_path ("migrations/ {$ citiesTimestamp }_create_cities_table.php " ),
110+ ['{{ model_namespace }} ' => $ this ->modelNamespace ]
111+ );
112+
113+ $ this ->info ('Migrations generated successfully. ' );
114+ }
115+
116+ /**
117+ * Generate seeder from stub.
118+ *
119+ * @return void
120+ */
121+ protected function makeSeeder (): void
122+ {
123+ $ this ->info ('Generating IranProvincesAndCitiesSeeder... ' );
124+
125+ $ this ->generateFromStub (
126+ $ this ->findStub ('seeders/IranProvincesAndCitiesSeeder.stub ' ),
127+ database_path ('seeders/IranProvincesAndCitiesSeeder.php ' ),
128+ [
129+ '{{ model_namespace }} ' => $ this ->modelNamespace
130+ ]
131+ );
132+
133+ $ this ->info ('Seeder generated successfully. ' );
134+ }
135+
136+ /**
137+ * Find the stub file path.
138+ *
139+ * @param string $stubName
140+ * @return string
141+ */
142+ protected function findStub (string $ stubName ): string
143+ {
144+ $ customPath = base_path ("stubs/vendor/sadegh19b/laravel-iran-cities/src/stubs/ {$ stubName }" );
145+
146+ if (File::exists ($ customPath )) {
147+ return $ customPath ;
148+ }
149+
150+ return __DIR__ . "/../stubs/ {$ stubName }" ;
151+ }
152+
153+ /**
154+ * Generate a file from a stub.
155+ *
156+ * @param string $stubPath
157+ * @param string $targetPath
158+ * @param array $replacements
159+ * @return void
160+ */
161+ protected function generateFromStub (string $ stubPath , string $ targetPath , array $ replacements ): void
162+ {
163+ $ content = File::get ($ stubPath );
164+
165+ foreach ($ replacements as $ search => $ replace ) {
166+ $ content = str_replace ($ search , $ replace , $ content );
167+ }
168+
169+ File::put ($ targetPath , $ content );
170+
171+ $ this ->line ("Created: " . $ targetPath );
172+ }
173+ }
0 commit comments