Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
SaliBhdr committed Jul 22, 2021
2 parents 30c5760 + 4a7236d commit 95e7e3e
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 87 deletions.
61 changes: 61 additions & 0 deletions src/Commands/Abstracts/AbstractCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace SaliBhdr\TyphoonIranCities\Commands\Abstracts;

use Illuminate\Console\Command;

abstract class AbstractCommand extends Command
{
/**
* @param string $question
* @return bool
*/
protected function askBoolQuestion($question)
{
$answer = strtolower($this->ask("$question (y/n)", 'y'));

$answerMap = [
'y' => true,
'yes' => true,
'n' => false,
'no' => false,
];

if (isset($answerMap[$answer]))
return $answerMap[$answer];

return $this->askBoolQuestion($question);
}

/**
* @param $path
* @return array|null
* @throws \Exception
*/
protected function csvToArray($path)
{
if (!$path || !file_exists($path))
throw new \Exception('File ' . $path . ' not exists');

$csv = array_map('str_getcsv', file($path));

array_walk($csv, function (&$a) use ($csv) {
$a = array_combine($csv[0], $a);
});

array_shift($csv);

return $csv;
}

/**
* prevents php throw error for importing very large data sets to database based on limits
*/
protected function removePhpLimits()
{
@set_time_limit(0);
@ini_set("memory_limit", "-1");
@ini_set("max_execution_time", '-1');
@ini_set('max_input_vars', '5000');
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
<?php

namespace SaliBhdr\TyphoonIranCities\Commands;
namespace SaliBhdr\TyphoonIranCities\Commands\Abstracts;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Console\Input\InputOption;

abstract class AbstractImport extends Command
abstract class AbstractImport extends AbstractCommand
{
/**
* AbstractImportCommand constructor.
*/

public function __construct()
{
parent::__construct();
Expand All @@ -23,6 +20,7 @@ public function __construct()
/**
* Execute the console command.
* @return void
* @throws \Exception
*/
public function handle()
{
Expand All @@ -33,7 +31,8 @@ public function handle()
$files = $this->getFiles();

foreach ($files as $fileName) {
$rows = $this->csvToArray($fileName);

$rows = $this->csvToArray(__DIR__ . '/../../../csv/' . $fileName);

if (empty($rows))
continue;
Expand Down Expand Up @@ -67,35 +66,12 @@ public function handle()
*/
abstract protected function getFiles();

/**
* @param string $file
* @return array|null
*/
protected function csvToArray($file)
{
$filePath = __DIR__ . '/../../csv/' . $file;

if (!file_exists($filePath))
return null;

$csv = array_map('str_getcsv', file($filePath));

array_walk($csv, function (&$a) use ($csv) {
$a = array_combine($csv[0], $a);
});

array_shift($csv);

return $csv;
}

/**
* @param string $dbName
* @param array $data
*/
private function insertToDb($dbName, $data)
protected function insertToDb($dbName, $data)
{

if (!$this->option('force') && DB::table($dbName)->where('id', $data['id'])->exists())
return;

Expand All @@ -106,15 +82,4 @@ private function insertToDb($dbName, $data)
DB::table($dbName)->updateOrInsert(['id' => $data['id']], $data);
}

/**
* prevents php throw error for importing very large data sets to database based on limits
*/
private function removePhpLimits()
{
@set_time_limit(0);
@ini_set("memory_limit", "-1");
@ini_set("max_execution_time", '-1');
@ini_set('max_input_vars', '5000');
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
namespace SaliBhdr\TyphoonIranCities\Commands;

use Illuminate\Console\Command;
namespace SaliBhdr\TyphoonIranCities\Commands\Abstracts;

use Symfony\Component\Console\Input\InputOption;

abstract class AbstractPublish extends Command
abstract class AbstractPublish extends AbstractCommand
{
public function __construct()
{
Expand All @@ -25,7 +25,11 @@ public function handle()

foreach ($map as $src => $destination) {
if ($this->option('force') || !file_exists($destination)) {
$this->copyFile($src,$destination);
$this->copyFile($src, $destination);
} elseif (file_exists($destination) && !$this->option('force')) {
if ($this->askBoolQuestion('The file ' . $destination . ' is exists. do you want to overwrite it?')) {
$this->copyFile($src, $destination);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Commands/ImportCities.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SaliBhdr\TyphoonIranCities\Commands;

use SaliBhdr\TyphoonIranCities\IranCsvEnum;
use SaliBhdr\TyphoonIranCities\Commands\Abstracts\AbstractImport;

class ImportCities extends AbstractImport
{
Expand Down
1 change: 1 addition & 0 deletions src/Commands/ImportCityDistricts.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SaliBhdr\TyphoonIranCities\Commands;

use SaliBhdr\TyphoonIranCities\IranCsvEnum;
use SaliBhdr\TyphoonIranCities\Commands\Abstracts\AbstractImport;

class ImportCityDistricts extends AbstractImport
{
Expand Down
1 change: 1 addition & 0 deletions src/Commands/ImportCounties.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SaliBhdr\TyphoonIranCities\Commands;

use SaliBhdr\TyphoonIranCities\IranCsvEnum;
use SaliBhdr\TyphoonIranCities\Commands\Abstracts\AbstractImport;

class ImportCounties extends AbstractImport
{
Expand Down
1 change: 1 addition & 0 deletions src/Commands/ImportIran.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SaliBhdr\TyphoonIranCities\IranCsvEnum;
use Symfony\Component\Console\Input\InputOption;
use SaliBhdr\TyphoonIranCities\Commands\Abstracts\AbstractImport;

class ImportIran extends AbstractImport
{
Expand Down
1 change: 1 addition & 0 deletions src/Commands/ImportProvinces.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SaliBhdr\TyphoonIranCities\Commands;

use SaliBhdr\TyphoonIranCities\IranCsvEnum;
use SaliBhdr\TyphoonIranCities\Commands\Abstracts\AbstractImport;

class ImportProvinces extends AbstractImport
{
Expand Down
1 change: 1 addition & 0 deletions src/Commands/ImportRuralDistricts.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SaliBhdr\TyphoonIranCities\Commands;

use SaliBhdr\TyphoonIranCities\IranCsvEnum;
use SaliBhdr\TyphoonIranCities\Commands\Abstracts\AbstractImport;

class ImportRuralDistricts extends AbstractImport
{
Expand Down
1 change: 1 addition & 0 deletions src/Commands/ImportSectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SaliBhdr\TyphoonIranCities\Commands;

use SaliBhdr\TyphoonIranCities\IranCsvEnum;
use SaliBhdr\TyphoonIranCities\Commands\Abstracts\AbstractImport;

class ImportSectors extends AbstractImport
{
Expand Down
1 change: 1 addition & 0 deletions src/Commands/ImportVillages.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SaliBhdr\TyphoonIranCities\Commands;

use SaliBhdr\TyphoonIranCities\IranCsvEnum;
use SaliBhdr\TyphoonIranCities\Commands\Abstracts\AbstractImport;

class ImportVillages extends AbstractImport
{
Expand Down
47 changes: 6 additions & 41 deletions src/Commands/IranInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace SaliBhdr\TyphoonIranCities\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Symfony\Component\Console\Input\InputOption;
use Illuminate\Console\BufferedConsoleOutput as ConsoleBuffer;
use SaliBhdr\TyphoonIranCities\Commands\Abstracts\AbstractCommand;

class IranInit extends Command
class IranInit extends AbstractCommand
{
/**
* The name and signature of the console command.
Expand All @@ -34,14 +32,14 @@ public function __construct()
public function handle()
{
if ($this->askBoolQuestion('Do you want to publish package migrations?')) {
$this->artisanCall('iran:publish:migrations', [
$this->call('iran:publish:migrations', [
'--force' => $this->option('force'),
'--region' => $this->option('region'),
]);
}

if ($this->askBoolQuestion('Do you want to publish package models?')) {
$this->artisanCall('iran:publish:models', [
$this->call('iran:publish:models', [
'--force' => $this->option('force'),
'--region' => $this->option('region'),
]);
Expand All @@ -50,47 +48,14 @@ public function handle()
if (!$this->askBoolQuestion('Do you want to run `php artisan migrate` to migrate package migrations?'))
return;

$this->artisanCall('migrate');
$this->call('migrate');

if ($this->askBoolQuestion('Do you want to import data?')) {
$this->artisanCall('iran:import', [
$this->call('iran:import', [
'--force' => $this->option('force'),
'--region' => $this->option('region'),
]);
}
}

/**
* @param string $question
* @return bool
*/
private function askBoolQuestion($question)
{
$answer = strtolower($this->ask("$question (y/n)", 'y'));

$answerMap = [
'y' => true,
'yes' => true,
'n' => false,
'no' => false,
];

if (isset($answerMap[$answer]))
return $answerMap[$answer];

$this->error('Unknown Answer');

return $this->askBoolQuestion($question);
}

private function artisanCall($command, array $parameters = [])
{
$laravelVersion = $this->laravel->version();

if ($laravelVersion < 5.6) {
Artisan::call($command, $parameters);
} else {
Artisan::call($command, $parameters, new ConsoleBuffer);
}
}
}
1 change: 1 addition & 0 deletions src/Commands/PublishMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Collection;
use Illuminate\Filesystem\Filesystem;
use SaliBhdr\TyphoonIranCities\Commands\Abstracts\AbstractPublish;

class PublishMigrations extends AbstractPublish
{
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/PublishModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace SaliBhdr\TyphoonIranCities\Commands;

use SaliBhdr\TyphoonIranCities\Commands\Abstracts\AbstractPublish;

class PublishModels extends AbstractPublish
{
/**
Expand Down

0 comments on commit 95e7e3e

Please sign in to comment.