Exemplo n.º 1
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $countries = $this->files->directories($this->path);
     $cities = [];
     foreach ($countries as $countryDirectory) {
         $states = $this->files->files($countryDirectory);
         $country = $this->last(explode(DIRECTORY_SEPARATOR, $countryDirectory));
         foreach ($states as $stateDirectory) {
             list($state, $_) = explode('.', $this->last(explode(DIRECTORY_SEPARATOR, $stateDirectory)));
             $data = $this->loader->load($country, $state, 'en');
             foreach ($data as $key => $name) {
                 $cities[] = ['name' => $name, 'code' => "{$country} {$state} {$key}", 'state_id' => "{$country} {$state}"];
             }
         }
     }
     $cities = Collection::make($cities);
     $hash = md5($cities->toJson());
     if (!$this->option('force') && $hash === $this->hash) {
         $this->line('No new city.');
         return false;
     }
     $cityCodes = $cities->pluck('code');
     $stateCodes = $cities->pluck('state_id')->unique();
     $stateIds = Collection::make(DB::table($this->states)->whereIn('code', $stateCodes)->pluck('id', 'code'));
     $cities = $cities->map(function ($item) use($stateIds) {
         $item['state_id'] = $stateIds->get($item['state_id']);
         return $item;
     });
     $existingCityIDs = Collection::make(DB::table($this->cities)->whereIn('code', $cityCodes)->pluck('id', 'code'));
     $cities = $cities->map(function ($item) use($existingCityIDs) {
         if ($existingCityIDs->has($item['code'])) {
             $item['id'] = $existingCityIDs->get($item['code']);
         }
         return $item;
     });
     $cities = $cities->groupBy(function ($item) {
         return array_has($item, 'id') ? 'update' : 'create';
     });
     DB::transaction(function () use($cities, $hash) {
         $create = Collection::make($cities->get('create'));
         $update = Collection::make($cities->get('update'));
         foreach ($create->chunk(static::QUERY_LIMIT) as $entries) {
             DB::table($this->cities)->insert($entries->toArray());
         }
         foreach ($update as $entries) {
             DB::table($this->cities)->where('id', $entries['id'])->update($entries);
         }
         $this->line("{$create->count()} cities created. {$update->count()} cities updated.");
         $this->files->put(storage_path(static::INSTALL_HISTORY), $hash);
     });
     return true;
 }
Exemplo n.º 2
0
 /**
  * @param string $country
  * @param string $state
  * @param string $locale
  */
 protected function load(string $country, string $state, string $locale)
 {
     if ($this->isLoaded($country, $state, $locale)) {
         return;
     }
     $this->loaded[$country][$state][$locale] = $this->loader->load($country, $state, $locale);
 }
Exemplo n.º 3
0
 public function test_it_can_load_with_slash_at_end_of_path()
 {
     $loader = new FileLoader(new Filesystem(), dirname(__DIR__) . '/data/');
     $this->assertTrue(is_array($loader->load('in', 'as', 'en')));
 }