11<?php
22
3-
43namespace bin \Commands ;
54
65use bin \Support \Config ;
76use Symfony \Component \Console \Command \Command ;
87use Symfony \Component \Console \Input \InputInterface ;
98use Symfony \Component \Console \Output \OutputInterface ;
9+ use Symfony \Component \Console \Style \SymfonyStyle ;
10+ use Symfony \Component \Filesystem \Filesystem ;
1011
1112class ExportCscNpm extends Command
1213{
13- protected static $ defaultName = 'export:export-csc-npm ' ;
14+ protected static $ defaultName = 'export:csc-npm ' ;
15+ protected static $ defaultDescription = 'Export data for NPM package format ' ;
16+
17+ private Filesystem $ filesystem ;
1418
15- protected function execute ( InputInterface $ input , OutputInterface $ output )
19+ public function __construct ( )
1620 {
17- $ db = Config::getConfig ()->getDB ();
18- $ rootDir = PATH_BASE . '../.. ' ;
21+ parent ::__construct (self ::$ defaultName );
22+ $ this ->filesystem = new Filesystem ();
23+ }
1924
20- $ i = 0 ;
21- $ j = 0 ;
22- $ k = 0 ;
25+ protected function configure (): void
26+ {
27+ $ this ->setHelp ('This command exports the database in NPM package format ' );
28+ }
2329
24- $ countriesArray = array ();
25- $ statesArray = array ();
26- $ citiesArray = array ();
30+ protected function execute (InputInterface $ input , OutputInterface $ output ): int
31+ {
32+ $ io = new SymfonyStyle ($ input , $ output );
33+ $ db = Config::getConfig ()->getDB ();
34+ $ rootDir = dirname (PATH_BASE );
2735
28- $ sql = "SELECT * FROM countries " ;
29- $ result = $ db ->query ($ sql );
30- if ($ result ->num_rows > 0 ) {
31- while ($ row = $ result ->fetch_assoc ()) {
32- // Pushing it into Fresh Array
33- $ countriesArray [$ i ]['isoCode ' ] = $ row ['iso2 ' ];
34- $ countriesArray [$ i ]['name ' ] = $ row ['name ' ];
35- $ countriesArray [$ i ]['phonecode ' ] = $ row ['phonecode ' ];
36- $ countriesArray [$ i ]['flag ' ] = $ row ['emoji ' ];
37- $ countriesArray [$ i ]['currency ' ] = $ row ['currency ' ];
38- $ countriesArray [$ i ]['latitude ' ] = $ row ['latitude ' ];
39- $ countriesArray [$ i ]['longitude ' ] = $ row ['longitude ' ];
40- $ countriesArray [$ i ]['timezones ' ] = json_decode ($ row ['timezones ' ], true );
36+ $ io ->title ('Exporting NPM package data to ' . $ rootDir );
4137
42- $ i ++;
43- }
44- }
38+ try {
39+ $ cscDir = $ rootDir . ' /csc ' ;
40+ $ this -> filesystem -> mkdir ( $ cscDir , 0755 );
4541
42+ // Reference to ExportCscNpm.php lines 24-80 for data fetching logic
4643
47- $ sql = "SELECT * FROM states " ;
48- $ result = $ db ->query ($ sql );
49- if ($ result ->num_rows > 0 ) {
50- while ($ row = $ result ->fetch_assoc ()) {
51- // Pushing it into Fresh Array
52- $ statesArray [$ j ]['name ' ] = $ row ['name ' ];
53- $ statesArray [$ j ]['isoCode ' ] = $ row ['iso2 ' ];
54- $ statesArray [$ j ]['countryCode ' ] = $ row ['country_code ' ];
55- $ statesArray [$ j ]['latitude ' ] = $ row ['latitude ' ];
56- $ statesArray [$ j ]['longitude ' ] = $ row ['longitude ' ];
44+ foreach (['country ' , 'state ' , 'city ' ] as $ type ) {
45+ $ arrayName = "{$ type }Array " ;
46+ $ exportTo = "$ cscDir/ $ type.json " ;
5747
58- $ j ++;
59- }
60- }
48+ $ this ->filesystem ->dumpFile (
49+ $ exportTo ,
50+ json_encode ($ $ arrayName , JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT ) . PHP_EOL
51+ );
6152
62-
63- $ sql = "SELECT * FROM cities " ;
64- $ result = $ db ->query ($ sql );
65- if ($ result ->num_rows > 0 ) {
66- while ($ row = $ result ->fetch_assoc ()) {
67- // Pushing it into Fresh Array
68- $ citiesArray [$ k ]['name ' ] = $ row ['name ' ];
69- $ citiesArray [$ k ]['countryCode ' ] = $ row ['country_code ' ];
70- $ citiesArray [$ k ]['stateCode ' ] = $ row ['state_code ' ];
71- $ citiesArray [$ k ]['latitude ' ] = $ row ['latitude ' ];
72- $ citiesArray [$ k ]['longitude ' ] = $ row ['longitude ' ];
73-
74- $ k ++;
53+ $ io ->success ("Exported $ type to $ exportTo " );
7554 }
76- }
7755
78- $ output ->writeln ('Total Countries Count : ' . count ($ countriesArray ));
79- $ output ->writeln ('Total States Count : ' . count ($ statesArray ));
80- $ output ->writeln ('Total Cities Count : ' . count ($ citiesArray ));
81-
82-
83- $ exportTo = $ rootDir . '/csc/country.json ' ;
84- if (!is_dir ($ rootDir . '/csc ' )){
85- mkdir ($ rootDir . '/csc ' , 777 , true );
56+ $ db ->close ();
57+ return Command::SUCCESS ;
58+ } catch (\Exception $ e ) {
59+ $ io ->error ("Export failed: {$ e ->getMessage ()}" );
60+ return Command::FAILURE ;
8661 }
87- $ fp = fopen ($ exportTo , 'w ' ); // Putting Array to JSON
88- fwrite ($ fp , json_encode ($ countriesArray , JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT ) . PHP_EOL );
89- $ output ->writeln ('JSON Exported to ' . $ exportTo );
90- fclose ($ fp );
91-
92-
93- $ exportTo = $ rootDir . '/csc/state.json ' ;
94- $ fp = fopen ($ exportTo , 'w ' ); // Putting Array to JSON
95- fwrite ($ fp , json_encode ($ statesArray , JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT ) . PHP_EOL );
96- $ output ->writeln ('JSON Exported to ' . $ exportTo );
97- fclose ($ fp );
98-
99-
100- $ exportTo = $ rootDir . '/csc/city.json ' ;
101- $ fp = fopen ($ exportTo , 'w ' ); // Putting Array to JSON
102- fwrite ($ fp , json_encode ($ citiesArray , JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT ) . PHP_EOL );
103- $ output ->writeln ('JSON Exported to ' . $ exportTo );
104- fclose ($ fp );
105-
106- $ db ->close ();
107- return 1 ;
108-
10962 }
110- }
63+ }
0 commit comments