generated from ghostwriter/wip
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateCSV.php
60 lines (52 loc) · 1.76 KB
/
generateCSV.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
declare(strict_types=1);
namespace Ghostwriter\TnTherapists;
use Ghostwriter\TnTherapists\Model\Therapist;
use Illuminate\Container\Container;
use Illuminate\Database\Capsule\Manager as Database;
use Illuminate\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Csv;
(static function () {
/** @var null|string $path */
$path = array_reduce(
[
__DIR__ . '/vendor/autoload.php',
__DIR__ . '/../../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../autoload.php',
],
static fn ($carry, $item) => (($carry === null) && file_exists($item)) ? $item : $carry
);
if ($path === null) {
fwrite(STDERR, 'Cannot locate autoloader; please run "composer install"' . PHP_EOL);
exit(1);
}
require $path;
$filesystem = new Filesystem();
$databasePath = './database.sqlite';
if ($filesystem->missing($databasePath)) {
$filesystem->put($databasePath, '');
}
$database = new Database();
$database->addConnection([
'driver' => 'sqlite',
'database' => 'database.sqlite',
'prefix' => '',
]);
$database->setEventDispatcher(new Dispatcher(new Container()));
$database->setAsGlobal();
$database->bootEloquent();
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->fromArray(
Therapist::whereNot('contact', '')
->orderBy('title')
->get()
->prepend(['Name', 'Title', 'Bio', 'Photo', 'Contact', 'Location', 'Type', 'Status'])
->toArray()
);
$writer = new Csv($spreadsheet);
$writer->save('Therapist.csv');
})();