Skip to content

Commit

Permalink
feat: start using Symfony Console and Phinx Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
dr5hn committed Jan 4, 2025
1 parent b048971 commit a309a71
Show file tree
Hide file tree
Showing 12 changed files with 1,015 additions and 623 deletions.
19 changes: 10 additions & 9 deletions .github/workflows/export.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- master
paths-ignore:
- "**"
- "!scripts/export**"
- "!bin/Commands/Export**"
workflow_dispatch:
inputs:
pass:
Expand All @@ -16,11 +16,11 @@ on:
jobs:
export:
name: JSON/XML/YAML/CSV/MYSQL/PSQL/SQLITE/SQLSERVER
runs-on: ubuntu-latest
runs-on: ubuntu-24.04

strategy:
matrix:
php-version: [8.1]
php-version: [8.2]
node-version: [20.x]
fail-fast: false

Expand Down Expand Up @@ -99,24 +99,25 @@ jobs:
- name: Composer Dependencies
run: |
cd scripts/vendor
cd bin
composer install
php bin/console list
- name: Export JSON
run: |
php scripts/export.php
php bin/console export:json
- name: Export XML
run: |
php scripts/export_xml.php
php bin/console export:xml
- name: Export YAML
run: |
php scripts/export_yaml.php
php bin/console export:yaml
- name: Export CSV
run: |
php scripts/export_csv.php
php bin/console export:csv
- name: Export MySQL SQL
run: |
Expand Down Expand Up @@ -148,7 +149,7 @@ jobs:
- name: Export SQL Server
run: |
php scripts/export_sql_server.php
php bin/console export:sqlserver
- name: Update README.md
run: |
Expand Down
123 changes: 38 additions & 85 deletions bin/Commands/ExportCscNpm.php
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;

}
}
}
126 changes: 64 additions & 62 deletions bin/Commands/ExportCsv.php
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;
}

}
Loading

0 comments on commit a309a71

Please sign in to comment.