Skip to content

Commit a309a71

Browse files
committed
feat: start using Symfony Console and Phinx Migration
1 parent b048971 commit a309a71

File tree

12 files changed

+1015
-623
lines changed

12 files changed

+1015
-623
lines changed

.github/workflows/export.yml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
- master
77
paths-ignore:
88
- "**"
9-
- "!scripts/export**"
9+
- "!bin/Commands/Export**"
1010
workflow_dispatch:
1111
inputs:
1212
pass:
@@ -16,11 +16,11 @@ on:
1616
jobs:
1717
export:
1818
name: JSON/XML/YAML/CSV/MYSQL/PSQL/SQLITE/SQLSERVER
19-
runs-on: ubuntu-latest
19+
runs-on: ubuntu-24.04
2020

2121
strategy:
2222
matrix:
23-
php-version: [8.1]
23+
php-version: [8.2]
2424
node-version: [20.x]
2525
fail-fast: false
2626

@@ -99,24 +99,25 @@ jobs:
9999
100100
- name: Composer Dependencies
101101
run: |
102-
cd scripts/vendor
102+
cd bin
103103
composer install
104+
php bin/console list
104105
105106
- name: Export JSON
106107
run: |
107-
php scripts/export.php
108+
php bin/console export:json
108109
109110
- name: Export XML
110111
run: |
111-
php scripts/export_xml.php
112+
php bin/console export:xml
112113
113114
- name: Export YAML
114115
run: |
115-
php scripts/export_yaml.php
116+
php bin/console export:yaml
116117
117118
- name: Export CSV
118119
run: |
119-
php scripts/export_csv.php
120+
php bin/console export:csv
120121
121122
- name: Export MySQL SQL
122123
run: |
@@ -148,7 +149,7 @@ jobs:
148149
149150
- name: Export SQL Server
150151
run: |
151-
php scripts/export_sql_server.php
152+
php bin/console export:sqlserver
152153
153154
- name: Update README.md
154155
run: |

bin/Commands/ExportCscNpm.php

Lines changed: 38 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,63 @@
11
<?php
22

3-
43
namespace bin\Commands;
54

65
use bin\Support\Config;
76
use Symfony\Component\Console\Command\Command;
87
use Symfony\Component\Console\Input\InputInterface;
98
use Symfony\Component\Console\Output\OutputInterface;
9+
use Symfony\Component\Console\Style\SymfonyStyle;
10+
use Symfony\Component\Filesystem\Filesystem;
1011

1112
class 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+
}

bin/Commands/ExportCsv.php

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,83 @@
11
<?php
22

3-
43
namespace bin\Commands;
54

65
use Symfony\Component\Console\Command\Command;
76
use Symfony\Component\Console\Input\InputInterface;
87
use Symfony\Component\Console\Output\OutputInterface;
8+
use Symfony\Component\Console\Style\SymfonyStyle;
9+
use Symfony\Component\Filesystem\Filesystem;
910

1011
class ExportCsv extends Command
1112
{
12-
protected static $defaultName = 'export:export-csv';
13+
protected static $defaultName = 'export:csv';
14+
protected static $defaultDescription = 'Export data to CSV format';
1315

14-
protected function execute(InputInterface $input, OutputInterface $output)
15-
{
16+
private const FILES = [
17+
'countries' => ['from' => '/json/countries.json', 'to' => '/csv/countries.csv'],
18+
'states' => ['from' => '/json/states.json', 'to' => '/csv/states.csv'],
19+
'cities' => ['from' => '/json/cities.json', 'to' => '/csv/cities.csv'],
20+
'regions' => ['from' => '/json/regions.json', 'to' => '/csv/regions.csv'],
21+
'subregions' => ['from' => '/json/subregions.json', 'to' => '/csv/subregions.csv'],
22+
];
1623

17-
$rootDir = PATH_BASE . '../..';
18-
19-
$files = array(
20-
'countries' => array(
21-
'from' => '/json/countries.json',
22-
'to' => '/csv/countries.csv',
23-
),
24-
'states' => array(
25-
'from' => '/json/states.json',
26-
'to' => '/csv/states.csv',
27-
),
28-
'cities' => array(
29-
'from' => '/json/cities.json',
30-
'to' => '/csv/cities.csv',
31-
),
32-
'regions' => array(
33-
'from' => '/json/regions.json',
34-
'to' => '/csv/regions.csv',
35-
),
36-
'subregions' => array(
37-
'from' => '/json/subregions.json',
38-
'to' => '/csv/subregions.csv',
39-
),
40-
);
41-
42-
foreach ($files as $root => $v) {
43-
// Gets JSON file
44-
$json = file_get_contents($rootDir . $v['from']);
45-
46-
$csc = json_decode($json, true);
47-
48-
$fp = fopen($rootDir . $v['to'], 'w'); // Putting Array to XML
49-
50-
// Set headings
51-
$headings = $csc[0];
52-
53-
// No translations please.
54-
unset($headings['translations']);
55-
fputcsv($fp, array_keys($headings));
56-
57-
// Loop through the associative array.
58-
foreach ($csc as $row) {
59-
// Update timezones to make readable
60-
if (!empty($row['timezones'])) {
61-
$row['timezones'] = json_encode($row['timezones']);
62-
$row['timezones'] = preg_replace('/"/', "'", $row['timezones']);
63-
$row['timezones'] = preg_replace("/'([a-zA-Z]+[a-zA-Z0-9_]*)':/", '$1:', $row['timezones']);
64-
}
24+
private Filesystem $filesystem;
25+
26+
public function __construct()
27+
{
28+
parent::__construct(self::$defaultName);
29+
$this->filesystem = new Filesystem();
30+
}
6531

66-
// No translations please.
67-
unset($row['translations']);
32+
protected function configure(): void
33+
{
34+
$this->setHelp('This command exports the database to CSV format');
35+
}
6836

69-
// Write the row to the CSV file.
70-
fputcsv($fp, $row);
71-
};
37+
protected function execute(InputInterface $input, OutputInterface $output): int
38+
{
39+
$io = new SymfonyStyle($input, $output);
40+
$rootDir = dirname(PATH_BASE);
41+
42+
$io->title('Exporting CSV data to ' . $rootDir);
43+
44+
try {
45+
foreach (self::FILES as $root => $v) {
46+
$io->section("Processing: $root");
47+
48+
$jsonData = $this->filesystem->exists($rootDir . $v['from'])
49+
? file_get_contents($rootDir . $v['from'])
50+
: throw new \RuntimeException("JSON file not found: {$v['from']}");
51+
52+
$csc = json_decode($jsonData, true)
53+
?: throw new \RuntimeException("Invalid JSON in {$v['from']}");
54+
55+
$fp = fopen($rootDir . $v['to'], 'w');
56+
57+
// Set headings
58+
$headings = $csc[0];
59+
unset($headings['translations']);
60+
fputcsv($fp, array_keys($headings));
61+
62+
// Write data
63+
foreach ($csc as $row) {
64+
if (!empty($row['timezones'])) {
65+
$row['timezones'] = json_encode($row['timezones']);
66+
$row['timezones'] = preg_replace('/"/', "'", $row['timezones']);
67+
$row['timezones'] = preg_replace("/'([a-zA-Z]+[a-zA-Z0-9_]*)':/", '$1:', $row['timezones']);
68+
}
69+
unset($row['translations']);
70+
fputcsv($fp, $row);
71+
}
7272

73-
fclose($fp);
73+
fclose($fp);
74+
$io->success("Exported $root to CSV");
75+
}
7476

75-
$output->writeln( 'CSV Exported to ' . $rootDir . $v['to'] );
77+
return Command::SUCCESS;
78+
} catch (\Exception $e) {
79+
$io->error("Export failed: {$e->getMessage()}");
80+
return Command::FAILURE;
7681
}
77-
78-
return 1;
7982
}
80-
8183
}

0 commit comments

Comments
 (0)