summaryrefslogtreecommitdiff
path: root/src/ScormServiceProvider.php
blob: 1806775ab81a89df5c49559356cb4f59e70a5187 (plain)
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
61
62
<?php


namespace Peopleaps\Scorm;


use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider;
use Peopleaps\Scorm\Manager\ScormManager;

class ScormServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind('scorm-manager', function($app) {
            return new ScormManager();
        });
    }

    public function boot()
    {
        $this->offerPublishing();
    }

    protected function offerPublishing() {
        // function not available and 'publish' not relevant in Lumen
        if (! function_exists('config_path')) {
            return;
        }

        $this->publishes([
            __DIR__.'/../config/scorm.php' => config_path('scorm.php'),
        ], 'config');

        $this->publishes([
            __DIR__.'/../database/migrations/create_scorm_tables.php.stub' => $this->getMigrationFileName('create_scorm_tables.php'),
        ], 'migrations');
    }

    /**
     * Returns existing migration file if found, else uses the current timestamp.
     *
     * @return string
     */
    protected function getMigrationFileName($migrationFileName): string
    {
        $timestamp = date('Y_m_d_His');

        $filesystem = $this->app->make(Filesystem::class);

        return Collection::make($this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR)
            ->flatMap(function ($path) use ($filesystem) {
                return $filesystem->glob($path.'*_create_scorm_tables.php');
            })->push($this->app->databasePath()."/migrations/{$timestamp}_create_scorm_tables.php")
            ->flatMap(function ($path) use ($filesystem, $migrationFileName) {
                return $filesystem->glob($path.'*_'.$migrationFileName);
            })
            ->push($this->app->databasePath()."/migrations/{$timestamp}_{$migrationFileName}")
            ->first();
    }
}