-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: start using Symfony Console and Phinx Migration
- Loading branch information
Showing
12 changed files
with
1,015 additions
and
623 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,63 @@ | ||
<?php | ||
|
||
|
||
namespace bin\Commands; | ||
|
||
use bin\Support\Config; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
|
||
class ExportCscNpm extends Command | ||
{ | ||
protected static $defaultName = 'export:export-csc-npm'; | ||
protected static $defaultName = 'export:csc-npm'; | ||
protected static $defaultDescription = 'Export data for NPM package format'; | ||
|
||
private Filesystem $filesystem; | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
public function __construct() | ||
{ | ||
$db = Config::getConfig()->getDB(); | ||
$rootDir = PATH_BASE . '../..'; | ||
parent::__construct(self::$defaultName); | ||
$this->filesystem = new Filesystem(); | ||
} | ||
|
||
$i = 0; | ||
$j = 0; | ||
$k = 0; | ||
protected function configure(): void | ||
{ | ||
$this->setHelp('This command exports the database in NPM package format'); | ||
} | ||
|
||
$countriesArray = array(); | ||
$statesArray = array(); | ||
$citiesArray = array(); | ||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$db = Config::getConfig()->getDB(); | ||
$rootDir = dirname(PATH_BASE); | ||
|
||
$sql = "SELECT * FROM countries"; | ||
$result = $db->query($sql); | ||
if ($result->num_rows > 0) { | ||
while ($row = $result->fetch_assoc()) { | ||
// Pushing it into Fresh Array | ||
$countriesArray[$i]['isoCode'] = $row['iso2']; | ||
$countriesArray[$i]['name'] = $row['name']; | ||
$countriesArray[$i]['phonecode'] = $row['phonecode']; | ||
$countriesArray[$i]['flag'] = $row['emoji']; | ||
$countriesArray[$i]['currency'] = $row['currency']; | ||
$countriesArray[$i]['latitude'] = $row['latitude']; | ||
$countriesArray[$i]['longitude'] = $row['longitude']; | ||
$countriesArray[$i]['timezones'] = json_decode($row['timezones'], true); | ||
$io->title('Exporting NPM package data to ' . $rootDir); | ||
|
||
$i++; | ||
} | ||
} | ||
try { | ||
$cscDir = $rootDir . '/csc'; | ||
$this->filesystem->mkdir($cscDir, 0755); | ||
|
||
// Reference to ExportCscNpm.php lines 24-80 for data fetching logic | ||
|
||
$sql = "SELECT * FROM states"; | ||
$result = $db->query($sql); | ||
if ($result->num_rows > 0) { | ||
while ($row = $result->fetch_assoc()) { | ||
// Pushing it into Fresh Array | ||
$statesArray[$j]['name'] = $row['name']; | ||
$statesArray[$j]['isoCode'] = $row['iso2']; | ||
$statesArray[$j]['countryCode'] = $row['country_code']; | ||
$statesArray[$j]['latitude'] = $row['latitude']; | ||
$statesArray[$j]['longitude'] = $row['longitude']; | ||
foreach (['country', 'state', 'city'] as $type) { | ||
$arrayName = "{$type}Array"; | ||
$exportTo = "$cscDir/$type.json"; | ||
|
||
$j++; | ||
} | ||
} | ||
$this->filesystem->dumpFile( | ||
$exportTo, | ||
json_encode($$arrayName, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . PHP_EOL | ||
); | ||
|
||
|
||
$sql = "SELECT * FROM cities"; | ||
$result = $db->query($sql); | ||
if ($result->num_rows > 0) { | ||
while ($row = $result->fetch_assoc()) { | ||
// Pushing it into Fresh Array | ||
$citiesArray[$k]['name'] = $row['name']; | ||
$citiesArray[$k]['countryCode'] = $row['country_code']; | ||
$citiesArray[$k]['stateCode'] = $row['state_code']; | ||
$citiesArray[$k]['latitude'] = $row['latitude']; | ||
$citiesArray[$k]['longitude'] = $row['longitude']; | ||
|
||
$k++; | ||
$io->success("Exported $type to $exportTo"); | ||
} | ||
} | ||
|
||
$output->writeln('Total Countries Count : ' . count($countriesArray)); | ||
$output->writeln('Total States Count : ' . count($statesArray)); | ||
$output->writeln('Total Cities Count : ' . count($citiesArray)); | ||
|
||
|
||
$exportTo = $rootDir . '/csc/country.json'; | ||
if(!is_dir($rootDir . '/csc')){ | ||
mkdir($rootDir . '/csc', 777, true); | ||
$db->close(); | ||
return Command::SUCCESS; | ||
} catch (\Exception $e) { | ||
$io->error("Export failed: {$e->getMessage()}"); | ||
return Command::FAILURE; | ||
} | ||
$fp = fopen($exportTo, 'w'); // Putting Array to JSON | ||
fwrite($fp, json_encode($countriesArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . PHP_EOL); | ||
$output->writeln('JSON Exported to ' . $exportTo); | ||
fclose($fp); | ||
|
||
|
||
$exportTo = $rootDir . '/csc/state.json'; | ||
$fp = fopen($exportTo, 'w'); // Putting Array to JSON | ||
fwrite($fp, json_encode($statesArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . PHP_EOL); | ||
$output->writeln('JSON Exported to ' . $exportTo); | ||
fclose($fp); | ||
|
||
|
||
$exportTo = $rootDir . '/csc/city.json'; | ||
$fp = fopen($exportTo, 'w'); // Putting Array to JSON | ||
fwrite($fp, json_encode($citiesArray, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . PHP_EOL); | ||
$output->writeln('JSON Exported to ' . $exportTo); | ||
fclose($fp); | ||
|
||
$db->close(); | ||
return 1; | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,83 @@ | ||
<?php | ||
|
||
|
||
namespace bin\Commands; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
|
||
class ExportCsv extends Command | ||
{ | ||
protected static $defaultName = 'export:export-csv'; | ||
protected static $defaultName = 'export:csv'; | ||
protected static $defaultDescription = 'Export data to CSV format'; | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
private const FILES = [ | ||
'countries' => ['from' => '/json/countries.json', 'to' => '/csv/countries.csv'], | ||
'states' => ['from' => '/json/states.json', 'to' => '/csv/states.csv'], | ||
'cities' => ['from' => '/json/cities.json', 'to' => '/csv/cities.csv'], | ||
'regions' => ['from' => '/json/regions.json', 'to' => '/csv/regions.csv'], | ||
'subregions' => ['from' => '/json/subregions.json', 'to' => '/csv/subregions.csv'], | ||
]; | ||
|
||
$rootDir = PATH_BASE . '../..'; | ||
|
||
$files = array( | ||
'countries' => array( | ||
'from' => '/json/countries.json', | ||
'to' => '/csv/countries.csv', | ||
), | ||
'states' => array( | ||
'from' => '/json/states.json', | ||
'to' => '/csv/states.csv', | ||
), | ||
'cities' => array( | ||
'from' => '/json/cities.json', | ||
'to' => '/csv/cities.csv', | ||
), | ||
'regions' => array( | ||
'from' => '/json/regions.json', | ||
'to' => '/csv/regions.csv', | ||
), | ||
'subregions' => array( | ||
'from' => '/json/subregions.json', | ||
'to' => '/csv/subregions.csv', | ||
), | ||
); | ||
|
||
foreach ($files as $root => $v) { | ||
// Gets JSON file | ||
$json = file_get_contents($rootDir . $v['from']); | ||
|
||
$csc = json_decode($json, true); | ||
|
||
$fp = fopen($rootDir . $v['to'], 'w'); // Putting Array to XML | ||
|
||
// Set headings | ||
$headings = $csc[0]; | ||
|
||
// No translations please. | ||
unset($headings['translations']); | ||
fputcsv($fp, array_keys($headings)); | ||
|
||
// Loop through the associative array. | ||
foreach ($csc as $row) { | ||
// Update timezones to make readable | ||
if (!empty($row['timezones'])) { | ||
$row['timezones'] = json_encode($row['timezones']); | ||
$row['timezones'] = preg_replace('/"/', "'", $row['timezones']); | ||
$row['timezones'] = preg_replace("/'([a-zA-Z]+[a-zA-Z0-9_]*)':/", '$1:', $row['timezones']); | ||
} | ||
private Filesystem $filesystem; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(self::$defaultName); | ||
$this->filesystem = new Filesystem(); | ||
} | ||
|
||
// No translations please. | ||
unset($row['translations']); | ||
protected function configure(): void | ||
{ | ||
$this->setHelp('This command exports the database to CSV format'); | ||
} | ||
|
||
// Write the row to the CSV file. | ||
fputcsv($fp, $row); | ||
}; | ||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$rootDir = dirname(PATH_BASE); | ||
|
||
$io->title('Exporting CSV data to ' . $rootDir); | ||
|
||
try { | ||
foreach (self::FILES as $root => $v) { | ||
$io->section("Processing: $root"); | ||
|
||
$jsonData = $this->filesystem->exists($rootDir . $v['from']) | ||
? file_get_contents($rootDir . $v['from']) | ||
: throw new \RuntimeException("JSON file not found: {$v['from']}"); | ||
|
||
$csc = json_decode($jsonData, true) | ||
?: throw new \RuntimeException("Invalid JSON in {$v['from']}"); | ||
|
||
$fp = fopen($rootDir . $v['to'], 'w'); | ||
|
||
// Set headings | ||
$headings = $csc[0]; | ||
unset($headings['translations']); | ||
fputcsv($fp, array_keys($headings)); | ||
|
||
// Write data | ||
foreach ($csc as $row) { | ||
if (!empty($row['timezones'])) { | ||
$row['timezones'] = json_encode($row['timezones']); | ||
$row['timezones'] = preg_replace('/"/', "'", $row['timezones']); | ||
$row['timezones'] = preg_replace("/'([a-zA-Z]+[a-zA-Z0-9_]*)':/", '$1:', $row['timezones']); | ||
} | ||
unset($row['translations']); | ||
fputcsv($fp, $row); | ||
} | ||
|
||
fclose($fp); | ||
fclose($fp); | ||
$io->success("Exported $root to CSV"); | ||
} | ||
|
||
$output->writeln( 'CSV Exported to ' . $rootDir . $v['to'] ); | ||
return Command::SUCCESS; | ||
} catch (\Exception $e) { | ||
$io->error("Export failed: {$e->getMessage()}"); | ||
return Command::FAILURE; | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
} |
Oops, something went wrong.