Move mapping creation to a function in lib/output.php
| | |
| | | <?php |
| | | function createMapping($db, $mapFunction, $saveConfig = FALSE) { |
| | | $mapping = []; |
| | | foreach($db->getAll() as $vehicle) { |
| | | $mapping[$vehicle['id']] = $mapFunction($vehicle['num']); |
| | | } |
| | | |
| | | if($saveConfig) { |
| | | $json = json_encode($mapping); |
| | | if(!file_put_contents($saveConfig['result_temp'], $json)) { |
| | | throw new Exception('Result save failed'); |
| | | } |
| | | rename($saveConfig['result_temp'], $saveConfig['result']); |
| | | } |
| | | |
| | | return $mapping; |
| | | } |
| | | |
| | | function createVehiclesList($trips, $mapping, $saveConfig = FALSE) { |
| | | $lines = []; |
| | | foreach($trips as $trip) { |
| | |
| | | |
| | | $db->addMapping($mapping); |
| | | |
| | | $jsonContent = []; |
| | | foreach($db->getAll() as $vehicle) { |
| | | $jsonContent[$vehicle['id']] = $source['mapper']($vehicle['num']); |
| | | } |
| | | |
| | | $json = json_encode($jsonContent); |
| | | if(!file_put_contents($source['result_temp'], $json)) { |
| | | throw new Exception('Result save failed'); |
| | | } |
| | | rename($source['result_temp'], $source['result']); |
| | | createMapping($db, $source['mapper'], $source); |
| | | |
| | | |
| | | $logger->info('Creating vehicle list...'); |
| | |
| | | <?php |
| | | require_once(__DIR__.'/vendor/autoload.php'); |
| | | require_once(__DIR__.'/lib/database.php'); |
| | | require_once(__DIR__.'/lib/output.php'); |
| | | require_once(__DIR__.'/lib/vehicle_types.php'); |
| | | require_once(__DIR__.'/config.php'); |
| | | |
| | |
| | | $logger = new Monolog\Logger('regenerate_'.$name); |
| | | try { |
| | | $logger->info('Regenerating '.$name.'...'); |
| | | |
| | | $db = new Database($source['database']); |
| | | |
| | | $jsonContent = []; |
| | | foreach($db->getAll() as $vehicle) { |
| | | $jsonContent[$vehicle['id']] = $source['mapper']($vehicle['num']); |
| | | } |
| | | |
| | | $json = json_encode($jsonContent); |
| | | if(!file_put_contents($source['result_temp'], $json)) { |
| | | throw new Exception('Result save failed'); |
| | | } |
| | | rename($source['result_temp'], $source['result']); |
| | | createMapping($db, $source['mapper'], $source); |
| | | $logger->info('Finished'); |
| | | } catch(Throwable $e) { |
| | | $logger->error($e->getMessage(), ['exception' => $e, 'exception_string' => (string)$e]); |