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