Skip to content

Commit

Permalink
New cli console && rename directory (#808)
Browse files Browse the repository at this point in the history
  • Loading branch information
tigran90 authored Sep 2, 2024
1 parent 29927c8 commit e2fdeef
Show file tree
Hide file tree
Showing 46 changed files with 1,074 additions and 1,049 deletions.
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
.DS_Store
scripts/vendor/vendor
scripts/vendor/data/*
bin/vendor/vendor
bin/scripts/data/*
issues/
csc/
base.php
.idea
/bin/vendor/
composer.lock

1 change: 1 addition & 0 deletions bin/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
APP_ENVIRONMENT=production
110 changes: 110 additions & 0 deletions bin/Commands/ExportCscNpm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?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;

class ExportCscNpm extends Command
{
protected static $defaultName = 'export:export-csc-npm';

protected function execute(InputInterface $input, OutputInterface $output)
{
$db = Config::getConfig()->getDB();
$rootDir = PATH_BASE . '../..';

$i = 0;
$j = 0;
$k = 0;

$countriesArray = array();
$statesArray = array();
$citiesArray = array();

$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);

$i++;
}
}


$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'];

$j++;
}
}


$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++;
}
}

$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);
}
$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;

}
}
81 changes: 81 additions & 0 deletions bin/Commands/ExportCsv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php


namespace bin\Commands;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ExportCsv extends Command
{
protected static $defaultName = 'export:export-csv';

protected function execute(InputInterface $input, OutputInterface $output)
{

$rootDir = PATH_BASE . '../..';

$files = array(
'countries' => array(
'from' => '/countries.json',
'to' => '/csv/countries.csv',
),
'states' => array(
'from' => '/states.json',
'to' => '/csv/states.csv',
),
'cities' => array(
'from' => '/cities.json',
'to' => '/csv/cities.csv',
),
'regions' => array(
'from' => '/regions.json',
'to' => '/csv/regions.csv',
),
'subregions' => array(
'from' => '/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']);
}

// No translations please.
unset($row['translations']);

// Write the row to the CSV file.
fputcsv($fp, $row);
};

fclose($fp);

$output->writeln( 'CSV Exported to ' . $rootDir . $v['to'] );
}

return 1;
}

}
Loading

0 comments on commit e2fdeef

Please sign in to comment.