|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use App\Console\AuxiliaryClasses\DataBaseFunctions; |
|
6
|
|
|
use DB; |
|
7
|
|
|
use Illuminate\Console\Command; |
|
8
|
|
|
use SoapBox\Formatter\Formatter; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class DBToFile |
|
12
|
|
|
* @package App\Console\Commands |
|
13
|
|
|
*/ |
|
14
|
|
|
class DBToFile extends Command |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var DataBaseFunctions |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $db_functions; |
|
20
|
|
|
/** |
|
21
|
|
|
* The name and signature of the console command. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $signature = 'file_creator:create'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The console command description. |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $description = 'Create files from DB and transform him'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Create a new command instance. |
|
36
|
|
|
* @param DataBaseFunctions $db_functions |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(DataBaseFunctions $db_functions) |
|
39
|
|
|
{ |
|
40
|
|
|
parent::__construct(); |
|
41
|
|
|
|
|
42
|
|
|
$this->db_functions=$db_functions; |
|
43
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Execute the console command. |
|
48
|
|
|
* |
|
49
|
|
|
* @return mixed |
|
50
|
|
|
*/ |
|
51
|
|
|
public function handle() |
|
52
|
|
|
{ |
|
53
|
|
|
$exchange_history = $this->db_functions->getAllDataFromExchangeHistory(); |
|
54
|
|
|
|
|
55
|
|
|
for($i=0; $i<count($exchange_history); $i++) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
$company_history = $exchange_history[$i]; |
|
58
|
|
|
|
|
59
|
|
|
$this->transformData($company_history); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
echo ("The files have been created successfully\n"); |
|
63
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param $company_history |
|
68
|
|
|
*/ |
|
69
|
|
|
public function transformData($company_history) |
|
70
|
|
|
{ |
|
71
|
|
|
$name_file = $company_history->symbol; |
|
72
|
|
|
|
|
73
|
|
|
$this->createAndStore($company_history, "json", $name_file); |
|
74
|
|
|
|
|
75
|
|
|
$company_history=json_encode($company_history); |
|
76
|
|
|
|
|
77
|
|
|
$formatter = Formatter::make($company_history, Formatter::JSON); |
|
78
|
|
|
|
|
79
|
|
|
$csv = $formatter->toCsv(); |
|
80
|
|
|
$xml = $formatter->toXml(); |
|
81
|
|
|
$yaml = $formatter->toYaml(); |
|
82
|
|
|
|
|
83
|
|
|
$this->createAndStore($csv, "csv", $name_file); |
|
84
|
|
|
|
|
85
|
|
|
$this->createAndStore($xml, "xml", $name_file); |
|
86
|
|
|
|
|
87
|
|
|
$this->createAndStore($yaml, "yaml", $name_file); |
|
88
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param $to_store |
|
93
|
|
|
* @param $extension_file |
|
94
|
|
|
* @param $name_file |
|
95
|
|
|
*/ |
|
96
|
|
|
public function createAndStore($to_store, $extension_file, $name_file) |
|
97
|
|
|
{ |
|
98
|
|
|
$myfile = fopen("public/historic_info_files/".$extension_file."/".$name_file.".".$extension_file, "w"); |
|
99
|
|
|
//$myfile = fopen("public/historic_info_files/testfile.txt", "w"); |
|
|
|
|
|
|
100
|
|
|
fwrite($myfile, json_encode($to_store)); |
|
101
|
|
|
fclose($myfile); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: