xref: /web-bugs/src/Utils/ComposerScripts.php (revision ae57162e)
1<?php
2
3namespace App\Utils;
4
5use Composer\Script\Event;
6
7/**
8 * Service for running Composer scripts when installing application.
9 */
10class ComposerScripts
11{
12    private static $cacheDir = __DIR__.'/../../var/cache';
13    private static $uploadsDir = __DIR__.'/../../uploads';
14
15    /**
16     * Create a default configuration settings for the development environment.
17     */
18    public static function installConfig(Event $event)
19    {
20        $sampleFile = __DIR__.'/../../local_config.php.sample';
21        $targetFile = __DIR__.'/../../local_config.php';
22
23        if ($event->isDevMode() && !file_exists($targetFile)) {
24            copy($sampleFile, $targetFile);
25        }
26    }
27
28    /**
29     * Create application temporary and upload directories which are not tracked
30     * in Git.
31     */
32    public static function createDirectories(Event $event)
33    {
34        if (!$event->isDevMode()) {
35            return;
36        }
37
38        $vendorDir = $event->getComposer()->getConfig()->get('vendor-dir');
39
40        require_once $vendorDir.'/autoload.php';
41
42        if (!file_exists(self::$cacheDir)) {
43            mkdir(self::$cacheDir, 0777, true);
44            chmod(self::$cacheDir, 0777);
45        }
46
47        if (!file_exists(self::$uploadsDir)) {
48            mkdir(self::$uploadsDir, 0777, true);
49            chmod(self::$uploadsDir, 0777);
50        }
51    }
52}
53