Example #1
0
 /**
  * @param string $locale
  */
 protected function load(string $locale)
 {
     if ($this->isLoaded($locale)) {
         return;
     }
     $this->loaded[$locale] = $this->loader->load($locale);
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $countries = [];
     $data = $this->loader->load('en');
     foreach ($data as $key => $name) {
         $countries[] = ['name' => $name, 'code' => "{$key}"];
     }
     $countries = Collection::make($countries);
     $hash = md5($countries->toJson());
     if (!$this->option('force') && $hash === $this->hash) {
         $this->line("No new country.");
         return false;
     }
     $countryCodes = $countries->pluck('code')->unique();
     $existingCountryIDs = Collection::make(DB::table($this->countries)->whereIn('code', $countryCodes)->pluck('id', 'code'));
     $countries = $countries->map(function ($item) use($existingCountryIDs) {
         if ($existingCountryIDs->has($item['code'])) {
             $item['id'] = $existingCountryIDs->get($item['code']);
         }
         return $item;
     });
     $countries = $countries->groupBy(function ($item) {
         return array_has($item, 'id') ? 'update' : 'create';
     });
     DB::transaction(function () use($countries, $hash) {
         $create = $countries->get('create', Collection::make());
         $update = $countries->get('update', Collection::make());
         foreach ($create->chunk(static::QUERY_LIMIT) as $entries) {
             DB::table($this->countries)->insert($entries->toArray());
         }
         foreach ($update as $entries) {
             DB::table($this->countries)->where('id', $entries['id'])->update($entries);
         }
         $this->line("{$create->count()} countries created. {$update->count()} countries updated.");
         $this->files->put(storage_path(static::INSTALL_HISTORY), $hash);
     });
     return true;
 }
Example #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('en')));
 }