Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into 20240212-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin committed Feb 12, 2024
2 parents 6824f15 + a178beb commit 7ec90a7
Show file tree
Hide file tree
Showing 42 changed files with 1,926 additions and 539 deletions.
681 changes: 660 additions & 21 deletions LICENSE.md

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ It contains:

MIT license.

### how to
### How to import the INSEE database

- first, * replace all semi colons with commas in the CSV provided by the INSEE
* `sed 's/;/,/g' yourfile.csv > newfile.csv`
- next, run the command `php artisan openname:import`
- finally, run the command `php artisan openname:count-total-after-import`
- next, run the command `php artisan touslesprenoms:import`
- finally, run the command `php artisan touslesprenoms:count-total-after-import`

### Cache

There is a lot of cache instances in this project. Everything is cached. If you encounter any problem while debugging, remember to clear the cache with `php artisan cache:clear`.
20 changes: 20 additions & 0 deletions app/Console/Commands/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command as CommandBase;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @codeCoverageIgnore
*/
abstract class Command extends CommandBase
{
protected function artisan(string $message, string $command, array $options = []): void
{
$this->info($message);
$this->getOutput()->getOutput()->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE
? $this->call($command, $options)
: $this->callSilent($command, $options);
}
}
5 changes: 4 additions & 1 deletion app/Console/Commands/CountTotalAfterImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
use App\Models\Name;
use Illuminate\Console\Command;

/**
* @codeCoverageIgnore
*/
class CountTotalAfterImport extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'openname:count-total-after-import';
protected $signature = 'touslesprenoms:count-total-after-import';

/**
* The console command description.
Expand Down
5 changes: 4 additions & 1 deletion app/Console/Commands/CreateMainCharacteristics.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
use Illuminate\Console\Command;
use Illuminate\Support\Str;

/**
* @codeCoverageIgnore
*/
class CreateMainCharacteristics extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'openname:create-main-characteristics';
protected $signature = 'touslesprenoms:create-main-characteristics';

/**
* The console command description.
Expand Down
5 changes: 4 additions & 1 deletion app/Console/Commands/FetchMetaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
use App\Models\Name;
use Illuminate\Console\Command;

/**
* @codeCoverageIgnore
*/
class FetchMetaData extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'openname:fetch-meta-data';
protected $signature = 'touslesprenoms:fetch-meta-data';

/**
* The console command description.
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Commands/GenerateSitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class GenerateSitemap extends Command
*
* @var string
*/
protected $signature = 'openname:sitemap';
protected $signature = 'touslesprenoms:sitemap';

/**
* The console command description.
Expand Down
14 changes: 5 additions & 9 deletions app/Console/Commands/ImportInseeCSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@

use App\Models\Name;
use App\Models\NameStatistic;
use Illuminate\Console\Command;
use Spatie\SimpleExcel\SimpleExcelReader;

/**
* @codeCoverageIgnore
*/
class ImportInseeCSV extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'openname:import';
protected $signature = 'touslesprenoms:import';

/**
* The console command description.
Expand Down Expand Up @@ -60,12 +62,6 @@ public function handle(): void
]);
});

$this->artisan('☐ Counting total after import', 'openname:count-total-after-import');
}

private function artisan(string $message, string $command, array $arguments = []): void
{
$this->info($message);
$this->callSilent($command, $arguments);
$this->artisan('☐ Counting total after import', 'touslesprenoms:count-total-after-import');
}
}
5 changes: 4 additions & 1 deletion app/Console/Commands/LocalizeApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
use Stichoza\GoogleTranslate\GoogleTranslate;
use Symfony\Component\Finder\Finder;

/**
* @codeCoverageIgnore
*/
class LocalizeApplication extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'openname:localize';
protected $signature = 'touslesprenoms:localize';

/**
* The console command description.
Expand Down
111 changes: 111 additions & 0 deletions app/Console/Commands/SetupApplication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\ConfirmableTrait;
use Illuminate\Support\Facades\Schema;

/**
* @codeCoverageIgnore
*/
class SetupApplication extends Command
{
use ConfirmableTrait;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'setup
{--force : Force the operation to run when in production.}
{--skip-storage-link : Skip storage link create.}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Install or update the application, and run migrations after a new release';

/**
* Execute the console command.
*/
public function handle(): void
{
if ($this->confirmToProceed()) {
$this->resetCache();
$this->clearConfig();
$this->symlink();
$this->migrate();
$this->cacheConfig();
$this->scout();
}
}

/**
* Clear or rebuild all cache.
*/
protected function resetCache(): void
{
if (config('cache.default') != 'database' || Schema::hasTable(config('cache.stores.database.table'))) {
$this->artisan('✓ Resetting application cache', 'cache:clear');
}
}

/**
* Clear configuration.
*/
protected function clearConfig(): void
{
if ($this->getLaravel()->environment() == 'production') {
$this->artisan('✓ Clear config cache', 'config:clear');
$this->artisan('✓ Resetting route cache', 'route:cache');
$this->artisan('✓ Resetting view cache', 'view:clear');
} else {
$this->artisan('✓ Clear config cache', 'config:clear');
$this->artisan('✓ Clear route cache', 'route:clear');
$this->artisan('✓ Clear view cache', 'view:clear');
}
}

/**
* Cache configuration.
*/
protected function cacheConfig(): void
{
// Cache config
if ($this->getLaravel()->environment() == 'production'
&& (config('cache.default') != 'database' || Schema::hasTable(config('cache.stores.database.table')))) {
$this->artisan('✓ Cache configuraton', 'config:cache');
}
}

/**
* Symlink storage to public.
*/
protected function symlink(): void
{
if ($this->option('skip-storage-link') !== true
&& $this->getLaravel()->environment() != 'testing'
&& ! file_exists(public_path('storage'))) {
$this->artisan('✓ Symlink the storage folder', 'storage:link');
}
}

/**
* Run migrations.
*/
protected function migrate(): void
{
$this->artisan('✓ Performing migrations', 'migrate', ['--force' => true]);
}

/**
* Configure scout.
*/
protected function scout(): void
{
$this->artisan('✓ Setup scout', 'scout:setup', ['--force' => true]);
}
}
82 changes: 82 additions & 0 deletions app/Console/Commands/SetupScout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\ConfirmableTrait;

/**
* @codeCoverageIgnore
*/
class SetupScout extends Command
{
use ConfirmableTrait;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'scout:setup
{--flush : Flush the indexes.}
{--import : Import the models into the search index.}
{--force : Force the operation to run when in production.}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Setup scout indexes.';

/**
* Execute the console command.
*/
public function handle(): void
{
if ($this->confirmToProceed()) {
$this->scoutConfigure();
$this->scoutFlush();
$this->scoutImport();
}
}

/**
* Configure scout.
*/
protected function scoutConfigure(): void
{
if (config('scout.driver') === 'meilisearch' && config('scout.meilisearch.host') !== '') {
$this->artisan('☐ Updating indexes on Meilisearch', 'scout:sync-index-settings', ['--verbose' => true]);
}
}

/**
* Import models.
*/
protected function scoutFlush(): void
{
if (config('scout.driver') !== null && $this->option('flush')) {
foreach (config('scout.meilisearch.index-settings') as $index => $settings) {
$name = (new $index)->getTable();
$this->artisan("☐ Flush {$name} index", 'scout:flush', ['model' => $index, '--verbose' => true]);
}

$this->info('✓ Indexes flushed');
}
}

/**
* Import models.
*/
protected function scoutImport(): void
{
if (config('scout.driver') !== null && $this->option('import')) {
foreach (config('scout.meilisearch.index-settings') as $index => $settings) {
$name = (new $index)->getTable();
$this->artisan("☐ Import {$name}", 'scout:import', ['model' => $index, '--verbose' => true]);
}

$this->info('✓ Indexes imported');
}
}
}
2 changes: 1 addition & 1 deletion app/Console/Commands/WarmCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class WarmCache extends Command
*
* @var string
*/
protected $signature = 'openname:warm';
protected $signature = 'touslesprenoms:warm';

/**
* The console command description.
Expand Down
5 changes: 5 additions & 0 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ public function index(): View
});
}

$lists = Cache::remember('admin-lists', 604800, function () {
return HomeViewModel::adminLists();
});

return view('home.index', [
'twentyMostPopularNames' => $popularNames,
'stats' => $stats,
'nameSpotlight' => HomeViewModel::nameSpotlight(),
'favorites' => $favoritedNamesForLoggedUser,
'lists' => $lists,
]);
}
}
1 change: 1 addition & 0 deletions app/Http/Controllers/ListNameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function store(Request $request, int $listId, int $nameId): Response
Cache::forget('route-list-' . $requestedList->id);
Cache::forget('user-lists-' . auth()->id());
Cache::forget('list-details-' . $requestedList->id);
Cache::forget('admin-lists');

return new HtmxResponseClientRedirect(route('list.show', [
'liste' => $requestedList->id,
Expand Down
Loading

0 comments on commit 7ec90a7

Please sign in to comment.