Passed
Push — develop ( 0cf906...cb779b )
by Jens
06:50
created

CloudControl::copyInstallFile()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
nc 6
nop 4
dl 0
loc 21
rs 8.7624
c 1
b 0
f 0
1
<?php
2
/**
3
 * Created by jensk on 14-8-2017.
4
 */
5
6
namespace CloudControl\Cms;
7
8
use CloudControl\Cms\cc\Application;
9
use CloudControl\Cms\storage\Repository;
10
use Composer\Script\Event;
11
12
class CloudControl
13
{
14
    public static function run()
15
    {
16
        new Application();
17
    }
18
19
    public static function postInstall(Event $event)
20
    {
21
        $event->getIO()->write("Post install");
22
        self::checkInstall($event);
23
    }
24
25
    public static function postUpdate(Event $event)
26
    {
27
        $event->getIO()->write("Post update");
28
        self::checkInstall($event);
29
    }
30
31
    /**
32
     * @param Event $event
33
     */
34
    private static function checkInstall(Event $event)
35
    {
36
        if (!function_exists('getRelativePath')) {
37
            require_once(__DIR__ . DIRECTORY_SEPARATOR . 'util' . DIRECTORY_SEPARATOR . 'functions.php');
38
        }
39
40
        $event->getIO()->write("");
41
        $event->getIO()->write("********************************************************");
42
        $event->getIO()->write("*** Checking installation of Cloud Control framework ***");
43
        $event->getIO()->write("********************************************************");
44
        $event->getIO()->write("");
45
46
        $vendorDir = $event->getComposer()->getConfig()->get('vendor-dir');
47
        $rootDir = realpath($vendorDir . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
48
49
        $baseConfigTargetPath = $rootDir . DIRECTORY_SEPARATOR . 'config.json';
50
        $configObject = self::getConfig($event, $baseConfigTargetPath);
51
52
        $configObject->{'vendorDir'} = realpath($vendorDir);
53
        $configObject->{'rootDir'} = $rootDir;
54
        $configObject->{'templateDir'} = self::createDir($event, $rootDir, 'templates');
55
        $configObject->{'storageDir'} = self::createDir($event, $rootDir, $configObject->{'storageDir'});
56
        $configObject->{'publicDir'} = self::createDir($event, $rootDir, 'public');
57
        $configObject->{'jsDir'} = self::createDir($event, $rootDir, $configObject->{'publicDir'} . 'js');
58
        $configObject->{'cssDir'} = self::createDir($event, $rootDir, $configObject->{'publicDir'} . 'css');
59
        $configObject->{'imagesDir'} = self::createDir($event, $rootDir, $configObject->{'publicDir'} . 'images');
60
        $configObject->{'filesDir'} = self::createDir($event, $rootDir, $configObject->{'publicDir'} . 'files');
61
62
        $baseStorageDefaultPath = __DIR__ . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . '_storage.json';
63
        $baseStorageSqlPath = __DIR__ . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . '_storage.sql';
64
65
        self::createStorage($configObject->{'storageDir'}, $baseStorageDefaultPath, $baseStorageSqlPath);
66
        self::saveConfig($event, $baseConfigTargetPath, $configObject);
67
        self::copyInstallFile($event, 'public.htaccess', $configObject->{'publicDir'}, '.htaccess');
68
        self::copyInstallFile($event, 'root.htaccess', $configObject->{'rootDir'}, '.htaccess');
69
        self::copyInstallFile($event, 'base.php', $configObject->{'templateDir'});
70
        self::copyInstallFile($event, 'cms.css', $configObject->{'cssDir'});
71
        self::copyInstallFile($event, 'cms.js', $configObject->{'jsDir'});
72
        self::copyInstallFile($event, 'index.php', $configObject->{'publicDir'});
73
74
        $event->getIO()->write("");
75
        $event->getIO()->write("[SUCCESS] Installation is complete");
76
        $event->getIO()->write("");
77
    }
78
79
    /**
80
     * @param Event $event
81
     * @param $baseConfigTargetPath
82
     * @param $configObject
83
     * @internal param $rootDir
84
     * @internal param $vendorDir
85
     * @internal param $templateDir
86
     * @internal param $storageDir
87
     * @internal param $baseConfigDefaultPath
88
     * @internal param $baseConfigTargetPath
89
     * @internal param $storageDir
90
     */
91
    private static function saveConfig(Event $event, $baseConfigTargetPath, $configObject)
92
    {
93
        file_put_contents($baseConfigTargetPath, json_encode($configObject));
94
        $event->getIO()->write("[INFO] Saved config to: " . $baseConfigTargetPath);
95
    }
96
97
    private static function copyInstallFile(Event $event, $sourceFileName, $destinationPath, $destinationFileName = null)
98
    {
99
        $sourceFilePath = realpath(__DIR__ . DIRECTORY_SEPARATOR . 'install/_' . $sourceFileName);
100
101
102
        if ($destinationFileName === null) {
103
            $destinationFileName = $sourceFileName;
104
        }
105
106
        if (file_exists($sourceFilePath) && realpath($destinationPath) !== false) {
107
            $destinationFullPath = realpath($destinationPath) . DIRECTORY_SEPARATOR . $destinationFileName;
108
            if (file_exists($destinationFullPath)) {
109
                $event->getIO()->write("[INFO] File already exists: " . $destinationFullPath);
110
            } else {
111
                copy($sourceFilePath, $destinationFullPath);
112
                $event->getIO()->write("[INSTALL] Copied file: " . $sourceFileName . ' to ' . $destinationPath);
113
            }
114
        } else {
115
            $event->getIO()->write("[ERROR] Couldnt copy file: " . $sourceFileName . ' to ' . $destinationPath);
116
        }
117
    }
118
119
    /**
120
     * @param $storageDir
121
     * @param $baseStorageDefaultPath
122
     * @param $baseStorageSqlPath
123
     */
124
    private static function createStorage($storageDir, $baseStorageDefaultPath, $baseStorageSqlPath)
125
    {
126
        $repository = new Repository($storageDir);
127
        $repository->init($baseStorageDefaultPath, $baseStorageSqlPath);
128
    }
129
130
    private static function createDir(Event $event, $rootDir, $dirName)
131
    {
132
        $dir = $rootDir . DIRECTORY_SEPARATOR . $dirName . DIRECTORY_SEPARATOR;
133
        if (!is_dir($dir)) {
134
            mkdir($dir);
135
            $event->getIO()->write("[INSTALL] Created dir: " . $dir);
136
        } else {
137
            $event->getIO()->write("[INFO] Dir already exists: " . $dir);
138
        }
139
        return \getRelativePath($rootDir, $dir);
140
    }
141
142
    /**
143
     * @param $configTargetPath
144
     * @return mixed
145
     */
146
    private static function getConfig(Event $event, $configTargetPath)
147
    {
148
        $baseConfigDefaultPath = realpath(__DIR__ . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR . '_config.json');
149
150
        if (file_exists($configTargetPath)) {
151
            $config = json_decode(file_get_contents($configTargetPath));
152
            $event->getIO()->write("[INFO] Using existing config");
153
        } else {
154
            $config = json_decode(file_get_contents($baseConfigDefaultPath));
155
            $event->getIO()->write("[INSTALL] Created default config");
156
        }
157
        return $config;
158
    }
159
160
}