Passed
Push — develop ( cb779b...18e359 )
by Jens
02:35
created
src/CloudControl.php 1 patch
Indentation   +145 added lines, -145 removed lines patch added patch discarded remove patch
@@ -11,150 +11,150 @@
 block discarded – undo
11 11
 
12 12
 class CloudControl
13 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
-    }
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 159
 
160 160
 }
161 161
\ No newline at end of file
Please login to merge, or discard this patch.
src/cc/Application.php 1 patch
Indentation   +273 added lines, -273 removed lines patch added patch discarded remove patch
@@ -2,277 +2,277 @@
 block discarded – undo
2 2
 
3 3
 namespace CloudControl\Cms\cc {
4 4
 
5
-    use CloudControl\Cms\components\Component;
6
-    use CloudControl\Cms\storage\Storage;
7
-    use Whoops\Handler\PrettyPageHandler;
8
-    use Whoops\Run;
9
-
10
-    class Application
11
-    {
12
-        /**
13
-         * @var \stdClass
14
-         */
15
-        private $config;
16
-        /**
17
-         * @var \CloudControl\Cms\storage\Storage
18
-         */
19
-        private $storage;
20
-
21
-        /**
22
-         * @var \CloudControl\Cms\cc\Request
23
-         */
24
-        private $request;
25
-
26
-        /**
27
-         * @var array
28
-         */
29
-        private $matchedSitemapItems = array();
30
-
31
-        /**
32
-         * @var array
33
-         */
34
-        private $applicationComponents = array();
35
-
36
-        /**
37
-         * Application constructor.
38
-         */
39
-        public function __construct()
40
-        {
41
-            $this->config();
42
-            $this->storage();
43
-
44
-            $this->request = new Request();
45
-
46
-            $whoops = new Run;
47
-            $whoops->pushHandler(new PrettyPageHandler);
48
-            $whoops->register();
49
-
50
-            $this->redirectMatching($this->request);
51
-            $this->sitemapMatching($this->request);
52
-
53
-            $this->getApplicationComponents();
54
-
55
-            $this->runApplicationComponents();
56
-            $this->runSitemapComponents();
57
-
58
-            $this->renderApplicationComponents();
59
-            $this->renderSitemapComponents();
60
-        }
61
-
62
-        /**
63
-         * Initialize the config
64
-         *
65
-         * @throws \Exception
66
-         */
67
-        private function config()
68
-        {
69
-            $configPath = __DIR__ . '/../../config.json';
70
-            if (realpath($configPath) !== false) {
71
-                $json = file_get_contents($configPath);
72
-                $this->config = json_decode($json);
73
-            } else {
74
-                throw new \Exception('Framework not initialized yet. Consider running composer install');
75
-            }
76
-        }
77
-
78
-        /**
79
-         * Initialize the storage
80
-         */
81
-        private function storage()
82
-        {
83
-            $this->storage = new Storage($this->config->rootDir . DIRECTORY_SEPARATOR . $this->config->storageDir, $this->config->imagesDir, $this->config->filesDir);
84
-        }
85
-
86
-        private function redirectMatching($request)
87
-        {
88
-            $redirects = $this->storage->getRedirects()->getRedirects();
89
-            $relativeUri = '/' . $request::$relativeUri;
90
-
91
-            foreach ($redirects as $redirect) {
92
-                if (preg_match_all($redirect->fromUrl, $relativeUri, $matches)) {
93
-                    $toUrl = preg_replace($redirect->fromUrl, $redirect->toUrl, $relativeUri);
94
-                    if (substr($toUrl, 0, 1) == '/') {
95
-                        $toUrl = substr($toUrl, 1);
96
-                    }
97
-                    if ($redirect->type == '301') {
98
-                        header('HTTP/1.1 301 Moved Permanently');
99
-                        header('Location: ' . $request::$subfolders . $toUrl);
100
-                        exit;
101
-                    } elseif ($redirect->type == '302') {
102
-                        header('Location: ' . $request::$subfolders . $toUrl, true, 302);
103
-                        exit;
104
-                    } else {
105
-                        throw new \Exception('Invalid redirect type.');
106
-                    }
107
-                }
108
-            }
109
-        }
110
-
111
-        /**
112
-         * Loop through sitemap items and see if one matches the requestUri.
113
-         * If it does, add it tot the matchedSitemapItems array
114
-         *
115
-         * @param $request
116
-         */
117
-        private function sitemapMatching($request)
118
-        {
119
-            $sitemap = $this->storage->getSitemap()->getSitemap();
120
-            $relativeUri = '/' . $request::$relativeUri;
121
-
122
-            foreach ($sitemap as $sitemapItem) {
123
-                if ($sitemapItem->regex) {
124
-                    $matches = array();
125
-                    if (preg_match_all($sitemapItem->url, $relativeUri, $matches)) {
126
-                        // Make a clone, so it doesnt add the matches to the original
127
-                        $matchedClone = clone $sitemapItem;
128
-                        $matchedClone->matches = $matches;
129
-                        $this->matchedSitemapItems[] = $matchedClone;
130
-                        return;
131
-                    }
132
-                } else {
133
-                    if ($sitemapItem->url == $relativeUri) {
134
-                        $this->matchedSitemapItems[] = $sitemapItem;
135
-                        return;
136
-                    }
137
-                }
138
-            }
139
-        }
140
-
141
-        /**
142
-         * Loop through all application components and run them
143
-         *
144
-         * @throws \Exception
145
-         */
146
-        private function runApplicationComponents()
147
-        {
148
-            foreach ($this->applicationComponents as $key => $applicationComponent) {
149
-                $class = $applicationComponent->component;
150
-                $parameters = $applicationComponent->parameters;
151
-                $this->applicationComponents[$key]->{'object'} = $this->getComponentObject($class, null, $parameters, null);
152
-                $this->applicationComponents[$key]->{'object'}->run($this->storage);
153
-            }
154
-        }
155
-
156
-        /**
157
-         * Loop through all (matched) sitemap components and run them
158
-         *
159
-         * @throws \Exception
160
-         */
161
-        private function runSitemapComponents()
162
-        {
163
-            foreach ($this->matchedSitemapItems as $key => $sitemapItem) {
164
-                $class = $sitemapItem->component;
165
-                $template = $sitemapItem->template;
166
-                $parameters = $sitemapItem->parameters;
167
-
168
-                $this->matchedSitemapItems[$key]->object = $this->getComponentObject($class, $template, $parameters, $sitemapItem);
169
-
170
-                $this->matchedSitemapItems[$key]->object->run($this->storage);
171
-            }
172
-        }
173
-
174
-        /**
175
-         * @param string $class
176
-         * @param string $template
177
-         * @param array $parameters
178
-         * @param \stdClass|null $matchedSitemapItem
179
-         *
180
-         * @return mixed
181
-         * @throws \Exception
182
-         */
183
-        private function getComponentObject($class = '', $template = '', $parameters = array(), $matchedSitemapItem)
184
-        {
185
-            $libraryComponentName = '\\CloudControl\Cms\\components\\' . $class;
186
-            $userComponentName = '\\components\\' . $class;
187
-
188
-            if (!class_exists($libraryComponentName, false)) {
189
-                $component = new $libraryComponentName($template, $this->request, $parameters, $matchedSitemapItem);
190
-            } elseif (!class_exists($userComponentName, false)) {
191
-                $component = new $userComponentName($template, $this->request, $parameters, $matchedSitemapItem);
192
-            } else {
193
-                throw new \Exception('Could not load component ' . $class);
194
-            }
195
-
196
-            if (!$component instanceof Component) {
197
-                throw new \Exception('Component not of type Component. Must inherit \CloudControl\Cms\components\Component');
198
-            }
199
-
200
-            return $component;
201
-        }
202
-
203
-        /**
204
-         * Loop through all application components and render them
205
-         */
206
-        private function renderApplicationComponents()
207
-        {
208
-            foreach ($this->applicationComponents as $applicationComponent) {
209
-                $applicationComponent->{'object'}->render();
210
-            }
211
-        }
212
-
213
-        /**
214
-         * Loop through all (matched) sitemap components and render them
215
-         */
216
-        private function renderSitemapComponents()
217
-        {
218
-            foreach ($this->matchedSitemapItems as $sitemapItem) {
219
-                $this->setCachingHeaders();
220
-                $sitemapItem->object->render($this);
221
-                ob_clean();
222
-                echo $sitemapItem->object->get();
223
-                ob_end_flush();
224
-                exit;
225
-            }
226
-        }
227
-
228
-        public function getAllApplicationComponentParameters()
229
-        {
230
-            $allParameters = array();
231
-            foreach ($this->applicationComponents as $applicationComponent) {
232
-                $parameters = $applicationComponent->{'object'}->getParameters();
233
-                $allParameters[] = $parameters;
234
-            }
235
-            return $allParameters;
236
-        }
237
-
238
-        public function unlockApplicationComponentParameters()
239
-        {
240
-            foreach ($this->applicationComponents as $applicationComponent) {
241
-                $parameters = $applicationComponent->{'object'}->getParameters();
242
-                extract($parameters);
243
-            }
244
-        }
245
-
246
-        /**
247
-         * Set the default caching of pages to 2 days
248
-         */
249
-        public function setCachingHeaders()
250
-        {
251
-            header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60 * 24 * 2))); // 2 days
252
-            header("Cache-Control: max-age=" . (60 * 60 * 24 * 2));
253
-        }
254
-
255
-        /**
256
-         * @return string
257
-         */
258
-        public function getTemplateDir()
259
-        {
260
-            return $this->config->templateDir;
261
-        }
262
-
263
-        public function getStorageDir()
264
-        {
265
-            return $this->config->storageDir;
266
-        }
267
-
268
-        public function getApplicationComponents()
269
-        {
270
-            $this->applicationComponents = $this->storage->getApplicationComponents()->getApplicationComponents();
271
-        }
272
-
273
-        public function getRootDir()
274
-        {
275
-            return $this->config->rootDir;
276
-        }
277
-    }
5
+	use CloudControl\Cms\components\Component;
6
+	use CloudControl\Cms\storage\Storage;
7
+	use Whoops\Handler\PrettyPageHandler;
8
+	use Whoops\Run;
9
+
10
+	class Application
11
+	{
12
+		/**
13
+		 * @var \stdClass
14
+		 */
15
+		private $config;
16
+		/**
17
+		 * @var \CloudControl\Cms\storage\Storage
18
+		 */
19
+		private $storage;
20
+
21
+		/**
22
+		 * @var \CloudControl\Cms\cc\Request
23
+		 */
24
+		private $request;
25
+
26
+		/**
27
+		 * @var array
28
+		 */
29
+		private $matchedSitemapItems = array();
30
+
31
+		/**
32
+		 * @var array
33
+		 */
34
+		private $applicationComponents = array();
35
+
36
+		/**
37
+		 * Application constructor.
38
+		 */
39
+		public function __construct()
40
+		{
41
+			$this->config();
42
+			$this->storage();
43
+
44
+			$this->request = new Request();
45
+
46
+			$whoops = new Run;
47
+			$whoops->pushHandler(new PrettyPageHandler);
48
+			$whoops->register();
49
+
50
+			$this->redirectMatching($this->request);
51
+			$this->sitemapMatching($this->request);
52
+
53
+			$this->getApplicationComponents();
54
+
55
+			$this->runApplicationComponents();
56
+			$this->runSitemapComponents();
57
+
58
+			$this->renderApplicationComponents();
59
+			$this->renderSitemapComponents();
60
+		}
61
+
62
+		/**
63
+		 * Initialize the config
64
+		 *
65
+		 * @throws \Exception
66
+		 */
67
+		private function config()
68
+		{
69
+			$configPath = __DIR__ . '/../../config.json';
70
+			if (realpath($configPath) !== false) {
71
+				$json = file_get_contents($configPath);
72
+				$this->config = json_decode($json);
73
+			} else {
74
+				throw new \Exception('Framework not initialized yet. Consider running composer install');
75
+			}
76
+		}
77
+
78
+		/**
79
+		 * Initialize the storage
80
+		 */
81
+		private function storage()
82
+		{
83
+			$this->storage = new Storage($this->config->rootDir . DIRECTORY_SEPARATOR . $this->config->storageDir, $this->config->imagesDir, $this->config->filesDir);
84
+		}
85
+
86
+		private function redirectMatching($request)
87
+		{
88
+			$redirects = $this->storage->getRedirects()->getRedirects();
89
+			$relativeUri = '/' . $request::$relativeUri;
90
+
91
+			foreach ($redirects as $redirect) {
92
+				if (preg_match_all($redirect->fromUrl, $relativeUri, $matches)) {
93
+					$toUrl = preg_replace($redirect->fromUrl, $redirect->toUrl, $relativeUri);
94
+					if (substr($toUrl, 0, 1) == '/') {
95
+						$toUrl = substr($toUrl, 1);
96
+					}
97
+					if ($redirect->type == '301') {
98
+						header('HTTP/1.1 301 Moved Permanently');
99
+						header('Location: ' . $request::$subfolders . $toUrl);
100
+						exit;
101
+					} elseif ($redirect->type == '302') {
102
+						header('Location: ' . $request::$subfolders . $toUrl, true, 302);
103
+						exit;
104
+					} else {
105
+						throw new \Exception('Invalid redirect type.');
106
+					}
107
+				}
108
+			}
109
+		}
110
+
111
+		/**
112
+		 * Loop through sitemap items and see if one matches the requestUri.
113
+		 * If it does, add it tot the matchedSitemapItems array
114
+		 *
115
+		 * @param $request
116
+		 */
117
+		private function sitemapMatching($request)
118
+		{
119
+			$sitemap = $this->storage->getSitemap()->getSitemap();
120
+			$relativeUri = '/' . $request::$relativeUri;
121
+
122
+			foreach ($sitemap as $sitemapItem) {
123
+				if ($sitemapItem->regex) {
124
+					$matches = array();
125
+					if (preg_match_all($sitemapItem->url, $relativeUri, $matches)) {
126
+						// Make a clone, so it doesnt add the matches to the original
127
+						$matchedClone = clone $sitemapItem;
128
+						$matchedClone->matches = $matches;
129
+						$this->matchedSitemapItems[] = $matchedClone;
130
+						return;
131
+					}
132
+				} else {
133
+					if ($sitemapItem->url == $relativeUri) {
134
+						$this->matchedSitemapItems[] = $sitemapItem;
135
+						return;
136
+					}
137
+				}
138
+			}
139
+		}
140
+
141
+		/**
142
+		 * Loop through all application components and run them
143
+		 *
144
+		 * @throws \Exception
145
+		 */
146
+		private function runApplicationComponents()
147
+		{
148
+			foreach ($this->applicationComponents as $key => $applicationComponent) {
149
+				$class = $applicationComponent->component;
150
+				$parameters = $applicationComponent->parameters;
151
+				$this->applicationComponents[$key]->{'object'} = $this->getComponentObject($class, null, $parameters, null);
152
+				$this->applicationComponents[$key]->{'object'}->run($this->storage);
153
+			}
154
+		}
155
+
156
+		/**
157
+		 * Loop through all (matched) sitemap components and run them
158
+		 *
159
+		 * @throws \Exception
160
+		 */
161
+		private function runSitemapComponents()
162
+		{
163
+			foreach ($this->matchedSitemapItems as $key => $sitemapItem) {
164
+				$class = $sitemapItem->component;
165
+				$template = $sitemapItem->template;
166
+				$parameters = $sitemapItem->parameters;
167
+
168
+				$this->matchedSitemapItems[$key]->object = $this->getComponentObject($class, $template, $parameters, $sitemapItem);
169
+
170
+				$this->matchedSitemapItems[$key]->object->run($this->storage);
171
+			}
172
+		}
173
+
174
+		/**
175
+		 * @param string $class
176
+		 * @param string $template
177
+		 * @param array $parameters
178
+		 * @param \stdClass|null $matchedSitemapItem
179
+		 *
180
+		 * @return mixed
181
+		 * @throws \Exception
182
+		 */
183
+		private function getComponentObject($class = '', $template = '', $parameters = array(), $matchedSitemapItem)
184
+		{
185
+			$libraryComponentName = '\\CloudControl\Cms\\components\\' . $class;
186
+			$userComponentName = '\\components\\' . $class;
187
+
188
+			if (!class_exists($libraryComponentName, false)) {
189
+				$component = new $libraryComponentName($template, $this->request, $parameters, $matchedSitemapItem);
190
+			} elseif (!class_exists($userComponentName, false)) {
191
+				$component = new $userComponentName($template, $this->request, $parameters, $matchedSitemapItem);
192
+			} else {
193
+				throw new \Exception('Could not load component ' . $class);
194
+			}
195
+
196
+			if (!$component instanceof Component) {
197
+				throw new \Exception('Component not of type Component. Must inherit \CloudControl\Cms\components\Component');
198
+			}
199
+
200
+			return $component;
201
+		}
202
+
203
+		/**
204
+		 * Loop through all application components and render them
205
+		 */
206
+		private function renderApplicationComponents()
207
+		{
208
+			foreach ($this->applicationComponents as $applicationComponent) {
209
+				$applicationComponent->{'object'}->render();
210
+			}
211
+		}
212
+
213
+		/**
214
+		 * Loop through all (matched) sitemap components and render them
215
+		 */
216
+		private function renderSitemapComponents()
217
+		{
218
+			foreach ($this->matchedSitemapItems as $sitemapItem) {
219
+				$this->setCachingHeaders();
220
+				$sitemapItem->object->render($this);
221
+				ob_clean();
222
+				echo $sitemapItem->object->get();
223
+				ob_end_flush();
224
+				exit;
225
+			}
226
+		}
227
+
228
+		public function getAllApplicationComponentParameters()
229
+		{
230
+			$allParameters = array();
231
+			foreach ($this->applicationComponents as $applicationComponent) {
232
+				$parameters = $applicationComponent->{'object'}->getParameters();
233
+				$allParameters[] = $parameters;
234
+			}
235
+			return $allParameters;
236
+		}
237
+
238
+		public function unlockApplicationComponentParameters()
239
+		{
240
+			foreach ($this->applicationComponents as $applicationComponent) {
241
+				$parameters = $applicationComponent->{'object'}->getParameters();
242
+				extract($parameters);
243
+			}
244
+		}
245
+
246
+		/**
247
+		 * Set the default caching of pages to 2 days
248
+		 */
249
+		public function setCachingHeaders()
250
+		{
251
+			header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60 * 24 * 2))); // 2 days
252
+			header("Cache-Control: max-age=" . (60 * 60 * 24 * 2));
253
+		}
254
+
255
+		/**
256
+		 * @return string
257
+		 */
258
+		public function getTemplateDir()
259
+		{
260
+			return $this->config->templateDir;
261
+		}
262
+
263
+		public function getStorageDir()
264
+		{
265
+			return $this->config->storageDir;
266
+		}
267
+
268
+		public function getApplicationComponents()
269
+		{
270
+			$this->applicationComponents = $this->storage->getApplicationComponents()->getApplicationComponents();
271
+		}
272
+
273
+		public function getRootDir()
274
+		{
275
+			return $this->config->rootDir;
276
+		}
277
+	}
278 278
 }
279 279
\ No newline at end of file
Please login to merge, or discard this patch.
src/storage/Repository.php 1 patch
Indentation   +329 added lines, -329 removed lines patch added patch discarded remove patch
@@ -24,210 +24,210 @@  discard block
 block discarded – undo
24 24
  */
25 25
 class Repository
26 26
 {
27
-    protected $storagePath;
28
-
29
-    protected $fileBasedSubsets = array('sitemap', 'applicationComponents', 'documentTypes', 'bricks', 'imageSet', 'images', 'files', 'users', 'valuelists', 'redirects');
30
-
31
-    protected $sitemap;
32
-    protected $sitemapChanges = false;
33
-
34
-    protected $applicationComponents;
35
-    protected $applicationComponentsChanges = false;
36
-
37
-    protected $documentTypes;
38
-    protected $documentTypesChanges = false;
39
-
40
-    protected $bricks;
41
-    protected $bricksChanges = false;
42
-
43
-    protected $imageSet;
44
-    protected $imageSetChanges = false;
45
-
46
-    protected $images;
47
-    protected $imagesChanges = false;
48
-
49
-    protected $files;
50
-    protected $filesChanges = false;
51
-
52
-    protected $users;
53
-    protected $usersChanges = false;
54
-
55
-    protected $valuelists;
56
-    protected $valuelistsChanges = false;
57
-
58
-    protected $redirects;
59
-    protected $redirectsChanges = false;
60
-
61
-    protected $contentDbHandle;
62
-
63
-
64
-    /**
65
-     * Repository constructor.
66
-     * @param $storagePath
67
-     * @throws \Exception
68
-     */
69
-    public function __construct($storagePath)
70
-    {
71
-        $storagePath = realpath($storagePath);
72
-        if (is_dir($storagePath) && $storagePath !== false) {
73
-            $this->storagePath = $storagePath;
74
-        } else {
75
-            throw new \Exception('Repository not yet initialized.');
76
-        }
77
-    }
78
-
79
-    /**
80
-     * Creates the folder in which to create all storage related files
81
-     *
82
-     * @param $storagePath
83
-     * @return bool
84
-     */
85
-    public static function create($storagePath)
86
-    {
87
-        return mkdir($storagePath);
88
-    }
89
-
90
-    /**
91
-     * Initiates default storage
92
-     * @param $baseStorageDefaultPath
93
-     * @param $baseStorageSqlPath
94
-     */
95
-    public function init($baseStorageDefaultPath, $baseStorageSqlPath)
96
-    {
97
-        $storageDefaultPath = realpath($baseStorageDefaultPath);
98
-        $contentSqlPath = realpath($baseStorageSqlPath);
99
-
100
-        $this->initConfigStorage($storageDefaultPath);
101
-        $this->initContentDb($contentSqlPath);
102
-
103
-        $this->save();
104
-    }
105
-
106
-    /**
107
-     * Load filebased subset and return it's contents
108
-     *
109
-     * @param $name
110
-     * @return mixed|string
111
-     * @throws \Exception
112
-     */
113
-    public function __get($name)
114
-    {
115
-        if (isset($this->$name)) {
116
-            if (in_array($name, $this->fileBasedSubsets)) {
117
-                return $this->$name;
118
-            } else {
119
-                throw new \Exception('Trying to get undefined property from Repository: ' . $name);
120
-            }
121
-        } else {
122
-            if (in_array($name, $this->fileBasedSubsets)) {
123
-                return $this->loadSubset($name);
124
-            } else {
125
-                throw new \Exception('Trying to get undefined property from Repository: ' . $name);
126
-            }
127
-        }
128
-    }
129
-
130
-    /**
131
-     * Set filebased subset contents
132
-     * @param $name
133
-     * @param $value
134
-     * @throws \Exception
135
-     */
136
-    public function __set($name, $value)
137
-    {
138
-        if (in_array($name, $this->fileBasedSubsets)) {
139
-            $this->$name = $value;
140
-            $changes = $name . 'Changes';
141
-            $this->$changes = true;
142
-        } else {
143
-            throw new \Exception('Trying to persist unknown subset in repository: ' . $name . ' <br /><pre>' . print_r($value, true) . '</pre>');
144
-        }
145
-    }
146
-
147
-    /**
148
-     * Persist all subsets
149
-     */
150
-    public function save()
151
-    {
152
-        $host = $this;
153
-        array_map(function ($value) use ($host) {
154
-            $host->saveSubset($value);
27
+	protected $storagePath;
28
+
29
+	protected $fileBasedSubsets = array('sitemap', 'applicationComponents', 'documentTypes', 'bricks', 'imageSet', 'images', 'files', 'users', 'valuelists', 'redirects');
30
+
31
+	protected $sitemap;
32
+	protected $sitemapChanges = false;
33
+
34
+	protected $applicationComponents;
35
+	protected $applicationComponentsChanges = false;
36
+
37
+	protected $documentTypes;
38
+	protected $documentTypesChanges = false;
39
+
40
+	protected $bricks;
41
+	protected $bricksChanges = false;
42
+
43
+	protected $imageSet;
44
+	protected $imageSetChanges = false;
45
+
46
+	protected $images;
47
+	protected $imagesChanges = false;
48
+
49
+	protected $files;
50
+	protected $filesChanges = false;
51
+
52
+	protected $users;
53
+	protected $usersChanges = false;
54
+
55
+	protected $valuelists;
56
+	protected $valuelistsChanges = false;
57
+
58
+	protected $redirects;
59
+	protected $redirectsChanges = false;
60
+
61
+	protected $contentDbHandle;
62
+
63
+
64
+	/**
65
+	 * Repository constructor.
66
+	 * @param $storagePath
67
+	 * @throws \Exception
68
+	 */
69
+	public function __construct($storagePath)
70
+	{
71
+		$storagePath = realpath($storagePath);
72
+		if (is_dir($storagePath) && $storagePath !== false) {
73
+			$this->storagePath = $storagePath;
74
+		} else {
75
+			throw new \Exception('Repository not yet initialized.');
76
+		}
77
+	}
78
+
79
+	/**
80
+	 * Creates the folder in which to create all storage related files
81
+	 *
82
+	 * @param $storagePath
83
+	 * @return bool
84
+	 */
85
+	public static function create($storagePath)
86
+	{
87
+		return mkdir($storagePath);
88
+	}
89
+
90
+	/**
91
+	 * Initiates default storage
92
+	 * @param $baseStorageDefaultPath
93
+	 * @param $baseStorageSqlPath
94
+	 */
95
+	public function init($baseStorageDefaultPath, $baseStorageSqlPath)
96
+	{
97
+		$storageDefaultPath = realpath($baseStorageDefaultPath);
98
+		$contentSqlPath = realpath($baseStorageSqlPath);
99
+
100
+		$this->initConfigStorage($storageDefaultPath);
101
+		$this->initContentDb($contentSqlPath);
102
+
103
+		$this->save();
104
+	}
105
+
106
+	/**
107
+	 * Load filebased subset and return it's contents
108
+	 *
109
+	 * @param $name
110
+	 * @return mixed|string
111
+	 * @throws \Exception
112
+	 */
113
+	public function __get($name)
114
+	{
115
+		if (isset($this->$name)) {
116
+			if (in_array($name, $this->fileBasedSubsets)) {
117
+				return $this->$name;
118
+			} else {
119
+				throw new \Exception('Trying to get undefined property from Repository: ' . $name);
120
+			}
121
+		} else {
122
+			if (in_array($name, $this->fileBasedSubsets)) {
123
+				return $this->loadSubset($name);
124
+			} else {
125
+				throw new \Exception('Trying to get undefined property from Repository: ' . $name);
126
+			}
127
+		}
128
+	}
129
+
130
+	/**
131
+	 * Set filebased subset contents
132
+	 * @param $name
133
+	 * @param $value
134
+	 * @throws \Exception
135
+	 */
136
+	public function __set($name, $value)
137
+	{
138
+		if (in_array($name, $this->fileBasedSubsets)) {
139
+			$this->$name = $value;
140
+			$changes = $name . 'Changes';
141
+			$this->$changes = true;
142
+		} else {
143
+			throw new \Exception('Trying to persist unknown subset in repository: ' . $name . ' <br /><pre>' . print_r($value, true) . '</pre>');
144
+		}
145
+	}
146
+
147
+	/**
148
+	 * Persist all subsets
149
+	 */
150
+	public function save()
151
+	{
152
+		$host = $this;
153
+		array_map(function ($value) use ($host) {
154
+			$host->saveSubset($value);
155 155
 		}, $this->fileBasedSubsets);
156
-    }
157
-
158
-    /**
159
-     * Persist subset to disk
160
-     * @param $subset
161
-     */
162
-    public function saveSubset($subset)
163
-    {
156
+	}
157
+
158
+	/**
159
+	 * Persist subset to disk
160
+	 * @param $subset
161
+	 */
162
+	public function saveSubset($subset)
163
+	{
164 164
 		$changes = $subset . 'Changes';
165 165
 		if ($this->$changes === true) {
166
-            if (!defined('JSON_PRETTY_PRINT')) {
167
-                $json = json_encode($this->$subset);
168
-            } else {
169
-                $json = json_encode($this->$subset, JSON_PRETTY_PRINT);
170
-            }
166
+			if (!defined('JSON_PRETTY_PRINT')) {
167
+				$json = json_encode($this->$subset);
168
+			} else {
169
+				$json = json_encode($this->$subset, JSON_PRETTY_PRINT);
170
+			}
171 171
 			$subsetStoragePath = $this->storagePath . DIRECTORY_SEPARATOR . $subset . '.json';
172 172
 			file_put_contents($subsetStoragePath, $json);
173 173
 
174 174
 			$this->$changes = false;
175 175
 		}
176
-    }
177
-
178
-    /**
179
-     * Load subset from disk
180
-     * @param $subset
181
-     * @return mixed|string
182
-     */
183
-    protected function loadSubset($subset)
184
-    {
185
-        $subsetStoragePath = $this->storagePath . DIRECTORY_SEPARATOR . $subset . '.json';
186
-        $json = file_get_contents($subsetStoragePath);
187
-        $json = json_decode($json);
188
-        $this->$subset = $json;
189
-        return $json;
190
-    }
191
-
192
-    /**
193
-     * @param $contentSqlPath
194
-     */
195
-    protected function initContentDb($contentSqlPath)
196
-    {
197
-        $db = $this->getContentDbHandle();
198
-        $sql = file_get_contents($contentSqlPath);
199
-        $db->exec($sql);
200
-    }
201
-
202
-    /**
203
-     * @param $storageDefaultPath
204
-     */
205
-    protected function initConfigStorage($storageDefaultPath)
206
-    {
207
-        $json = file_get_contents($storageDefaultPath);
208
-        $json = json_decode($json);
209
-        $this->initConfigIfNotExists($json, 'sitemap');
210
-        $this->initConfigIfNotExists($json, 'applicationComponents');
211
-        $this->initConfigIfNotExists($json, 'documentTypes');
212
-        $this->initConfigIfNotExists($json, 'bricks');
213
-        $this->initConfigIfNotExists($json, 'imageSet');
214
-        $this->initConfigIfNotExists($json, 'images');
215
-        $this->initConfigIfNotExists($json, 'files');
216
-        $this->initConfigIfNotExists($json, 'users');
217
-        $this->initConfigIfNotExists($json, 'valuelists');
218
-        $this->initConfigIfNotExists($json, 'redirects');
219
-    }
220
-
221
-    /**
222
-     * @return \PDO
223
-     */
224
-    public function getContentDbHandle()
225
-    {
226
-        if ($this->contentDbHandle === null) {
227
-            $this->contentDbHandle = new \PDO('sqlite:' . $this->storagePath . DIRECTORY_SEPARATOR . 'content.db');
228
-        }
229
-        return $this->contentDbHandle;
230
-    }
176
+	}
177
+
178
+	/**
179
+	 * Load subset from disk
180
+	 * @param $subset
181
+	 * @return mixed|string
182
+	 */
183
+	protected function loadSubset($subset)
184
+	{
185
+		$subsetStoragePath = $this->storagePath . DIRECTORY_SEPARATOR . $subset . '.json';
186
+		$json = file_get_contents($subsetStoragePath);
187
+		$json = json_decode($json);
188
+		$this->$subset = $json;
189
+		return $json;
190
+	}
191
+
192
+	/**
193
+	 * @param $contentSqlPath
194
+	 */
195
+	protected function initContentDb($contentSqlPath)
196
+	{
197
+		$db = $this->getContentDbHandle();
198
+		$sql = file_get_contents($contentSqlPath);
199
+		$db->exec($sql);
200
+	}
201
+
202
+	/**
203
+	 * @param $storageDefaultPath
204
+	 */
205
+	protected function initConfigStorage($storageDefaultPath)
206
+	{
207
+		$json = file_get_contents($storageDefaultPath);
208
+		$json = json_decode($json);
209
+		$this->initConfigIfNotExists($json, 'sitemap');
210
+		$this->initConfigIfNotExists($json, 'applicationComponents');
211
+		$this->initConfigIfNotExists($json, 'documentTypes');
212
+		$this->initConfigIfNotExists($json, 'bricks');
213
+		$this->initConfigIfNotExists($json, 'imageSet');
214
+		$this->initConfigIfNotExists($json, 'images');
215
+		$this->initConfigIfNotExists($json, 'files');
216
+		$this->initConfigIfNotExists($json, 'users');
217
+		$this->initConfigIfNotExists($json, 'valuelists');
218
+		$this->initConfigIfNotExists($json, 'redirects');
219
+	}
220
+
221
+	/**
222
+	 * @return \PDO
223
+	 */
224
+	public function getContentDbHandle()
225
+	{
226
+		if ($this->contentDbHandle === null) {
227
+			$this->contentDbHandle = new \PDO('sqlite:' . $this->storagePath . DIRECTORY_SEPARATOR . 'content.db');
228
+		}
229
+		return $this->contentDbHandle;
230
+	}
231 231
 
232 232
 	/**
233 233
 	 * Get all documents
@@ -237,13 +237,13 @@  discard block
 block discarded – undo
237 237
 	 * @return array
238 238
 	 * @throws \Exception
239 239
 	 */
240
-    public function getDocuments($state = 'published')
241
-    {
240
+	public function getDocuments($state = 'published')
241
+	{
242 242
 		if (!in_array($state, Document::$DOCUMENT_STATES)) {
243 243
 			throw new \Exception('Unsupported document state: ' . $state);
244 244
 		}
245
-        return $this->getDocumentsByPath('/', $state);
246
-    }
245
+		return $this->getDocumentsByPath('/', $state);
246
+	}
247 247
 
248 248
 	public function getDocumentsWithState($folderPath = '/')
249 249
 	{
@@ -290,51 +290,51 @@  discard block
 block discarded – undo
290 290
 	 * @return array
291 291
 	 * @throws \Exception
292 292
 	 */
293
-    public function getDocumentsByPath($folderPath, $state = 'published')
294
-    {
295
-    	if (!in_array($state, Document::$DOCUMENT_STATES)) {
296
-    		throw new \Exception('Unsupported document state: ' . $state);
293
+	public function getDocumentsByPath($folderPath, $state = 'published')
294
+	{
295
+		if (!in_array($state, Document::$DOCUMENT_STATES)) {
296
+			throw new \Exception('Unsupported document state: ' . $state);
297 297
 		}
298
-        $db = $this->getContentDbHandle();
299
-        $folderPathWithWildcard = $folderPath . '%';
298
+		$db = $this->getContentDbHandle();
299
+		$folderPathWithWildcard = $folderPath . '%';
300 300
 
301
-        $sql = 'SELECT *
301
+		$sql = 'SELECT *
302 302
               FROM documents_' . $state . '
303 303
              WHERE `path` LIKE ' . $db->quote($folderPathWithWildcard) . '
304 304
                AND substr(`path`, ' . (strlen($folderPath) + 1) . ') NOT LIKE "%/%"
305 305
                AND path != ' . $db->quote($folderPath) . '
306 306
           ORDER BY `type` DESC, `path` ASC';
307
-        $stmt = $this->getDbStatement($sql);
307
+		$stmt = $this->getDbStatement($sql);
308 308
 
309
-        $documents = $stmt->fetchAll(\PDO::FETCH_CLASS, '\CloudControl\Cms\storage\Document');
310
-        foreach ($documents as $key => $document) {
309
+		$documents = $stmt->fetchAll(\PDO::FETCH_CLASS, '\CloudControl\Cms\storage\Document');
310
+		foreach ($documents as $key => $document) {
311 311
 			$documents = $this->setAssetsToDocumentFolders($document, $db, $documents, $key);
312
-        }
313
-        return $documents;
314
-    }
315
-
316
-
317
-    /**
318
-     * @param $path
319
-     * @return bool|Document
320
-     */
321
-    public function getDocumentContainerByPath($path)
322
-    {
323
-        $document = $this->getDocumentByPath($path, 'unpublished');
324
-        if ($document === false) {
325
-            return false;
326
-        }
327
-        $slugLength = strlen($document->slug);
328
-        $containerPath = substr($path, 0, -$slugLength);
329
-        if ($containerPath === '/') {
330
-            return $this->getRootFolder();
331
-        }
332
-        if (substr($containerPath, -1) === '/'){
312
+		}
313
+		return $documents;
314
+	}
315
+
316
+
317
+	/**
318
+	 * @param $path
319
+	 * @return bool|Document
320
+	 */
321
+	public function getDocumentContainerByPath($path)
322
+	{
323
+		$document = $this->getDocumentByPath($path, 'unpublished');
324
+		if ($document === false) {
325
+			return false;
326
+		}
327
+		$slugLength = strlen($document->slug);
328
+		$containerPath = substr($path, 0, -$slugLength);
329
+		if ($containerPath === '/') {
330
+			return $this->getRootFolder();
331
+		}
332
+		if (substr($containerPath, -1) === '/'){
333 333
 			$containerPath = substr($containerPath, 0, -1);
334 334
 		}
335
-        $containerFolder = $this->getDocumentByPath($containerPath, 'unpublished');
336
-        return $containerFolder;
337
-    }
335
+		$containerFolder = $this->getDocumentByPath($containerPath, 'unpublished');
336
+		return $containerFolder;
337
+	}
338 338
 
339 339
 	/**
340 340
 	 * @param        $path
@@ -343,23 +343,23 @@  discard block
 block discarded – undo
343 343
 	 * @return bool|\CloudControl\Cms\storage\Document
344 344
 	 * @throws \Exception
345 345
 	 */
346
-    public function getDocumentByPath($path, $state = 'published')
347
-    {
346
+	public function getDocumentByPath($path, $state = 'published')
347
+	{
348 348
 		if (!in_array($state, Document::$DOCUMENT_STATES)) {
349 349
 			throw new \Exception('Unsupported document state: ' . $state);
350 350
 		}
351
-        $db = $this->getContentDbHandle();
352
-        $document = $this->fetchDocument('
351
+		$db = $this->getContentDbHandle();
352
+		$document = $this->fetchDocument('
353 353
             SELECT *
354 354
               FROM documents_' .  $state . '
355 355
              WHERE path = ' . $db->quote($path) . '
356 356
         ');
357
-        if ($document instanceof Document && $document->type === 'folder') {
358
-            $document->dbHandle = $db;
359
-            $document->documentStorage = new DocumentStorage($this);
360
-        }
361
-        return $document;
362
-    }
357
+		if ($document instanceof Document && $document->type === 'folder') {
358
+			$document->dbHandle = $db;
359
+			$document->documentStorage = new DocumentStorage($this);
360
+		}
361
+		return $document;
362
+	}
363 363
 
364 364
 	/**
365 365
 	 * Returns the count of all documents stored in the db
@@ -454,58 +454,58 @@  discard block
 block discarded – undo
454 454
 	}
455 455
 
456 456
 	/**
457
-     * Return the results of the query as array of Documents
458
-     * @param $sql
459
-     * @return array
460
-     * @throws \Exception
461
-     */
462
-    protected function fetchAllDocuments($sql)
463
-    {
464
-        $stmt = $this->getDbStatement($sql);
465
-        return $stmt->fetchAll(\PDO::FETCH_CLASS, '\CloudControl\Cms\storage\Document');
466
-    }
467
-
468
-    /**
469
-     * Return the result of the query as Document
470
-     * @param $sql
471
-     * @return mixed
472
-     * @throws \Exception
473
-     */
474
-    protected function fetchDocument($sql)
475
-    {
476
-        $stmt = $this->getDbStatement($sql);
477
-        return $stmt->fetchObject('\CloudControl\Cms\storage\Document');
478
-    }
479
-
480
-    /**
481
-     * Prepare the sql statement
482
-     * @param $sql
483
-     * @return \PDOStatement
484
-     * @throws \Exception
485
-     */
486
-    protected function getDbStatement($sql)
487
-    {
488
-        $db = $this->getContentDbHandle();
489
-        $stmt = $db->query($sql);
490
-        if ($stmt === false) {
491
-            $errorInfo = $db->errorInfo();
492
-            $errorMsg = $errorInfo[2];
493
-            throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
494
-        }
495
-        return $stmt;
496
-    }
497
-
498
-    /**
499
-     * Create a (non-existent) root folder Document and return it
500
-     * @return Document
501
-     */
502
-    protected function getRootFolder()
503
-    {
504
-        $rootFolder = new Document();
505
-        $rootFolder->path = '/';
506
-        $rootFolder->type = 'folder';
507
-        return $rootFolder;
508
-    }
457
+	 * Return the results of the query as array of Documents
458
+	 * @param $sql
459
+	 * @return array
460
+	 * @throws \Exception
461
+	 */
462
+	protected function fetchAllDocuments($sql)
463
+	{
464
+		$stmt = $this->getDbStatement($sql);
465
+		return $stmt->fetchAll(\PDO::FETCH_CLASS, '\CloudControl\Cms\storage\Document');
466
+	}
467
+
468
+	/**
469
+	 * Return the result of the query as Document
470
+	 * @param $sql
471
+	 * @return mixed
472
+	 * @throws \Exception
473
+	 */
474
+	protected function fetchDocument($sql)
475
+	{
476
+		$stmt = $this->getDbStatement($sql);
477
+		return $stmt->fetchObject('\CloudControl\Cms\storage\Document');
478
+	}
479
+
480
+	/**
481
+	 * Prepare the sql statement
482
+	 * @param $sql
483
+	 * @return \PDOStatement
484
+	 * @throws \Exception
485
+	 */
486
+	protected function getDbStatement($sql)
487
+	{
488
+		$db = $this->getContentDbHandle();
489
+		$stmt = $db->query($sql);
490
+		if ($stmt === false) {
491
+			$errorInfo = $db->errorInfo();
492
+			$errorMsg = $errorInfo[2];
493
+			throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
494
+		}
495
+		return $stmt;
496
+	}
497
+
498
+	/**
499
+	 * Create a (non-existent) root folder Document and return it
500
+	 * @return Document
501
+	 */
502
+	protected function getRootFolder()
503
+	{
504
+		$rootFolder = new Document();
505
+		$rootFolder->path = '/';
506
+		$rootFolder->type = 'folder';
507
+		return $rootFolder;
508
+	}
509 509
 
510 510
 	/**
511 511
 	 * Save the document to the database
@@ -517,13 +517,13 @@  discard block
 block discarded – undo
517 517
 	 * @throws \Exception
518 518
 	 * @internal param $path
519 519
 	 */
520
-    public function saveDocument($documentObject, $state = 'published')
521
-    {
520
+	public function saveDocument($documentObject, $state = 'published')
521
+	{
522 522
 		if (!in_array($state, Document::$DOCUMENT_STATES)) {
523 523
 			throw new \Exception('Unsupported document state: ' . $state);
524 524
 		}
525
-        $db = $this->getContentDbHandle();
526
-        $stmt = $this->getDbStatement('
525
+		$db = $this->getContentDbHandle();
526
+		$stmt = $this->getDbStatement('
527 527
             INSERT OR REPLACE INTO documents_' . $state . ' (`path`,`title`,`slug`,`type`,`documentType`,`documentTypeSlug`,`state`,`lastModificationDate`,`creationDate`,`lastModifiedBy`,`fields`,`bricks`,`dynamicBricks`)
528 528
             VALUES(
529 529
               ' . $db->quote($documentObject->path) . ',
@@ -541,9 +541,9 @@  discard block
 block discarded – undo
541 541
               ' . $db->quote(json_encode($documentObject->dynamicBricks)) . '
542 542
             )
543 543
         ');
544
-        $result = $stmt->execute();
545
-        return $result;
546
-    }
544
+		$result = $stmt->execute();
545
+		return $result;
546
+	}
547 547
 
548 548
 	/**
549 549
 	 * Delete the document from the database
@@ -554,29 +554,29 @@  discard block
 block discarded – undo
554 554
 	 * @internal param string $state
555 555
 	 *
556 556
 	 */
557
-    public function deleteDocumentByPath($path)
558
-    {
559
-        $db = $this->getContentDbHandle();
560
-        $documentToDelete = $this->getDocumentByPath($path, 'unpublished');
561
-        if ($documentToDelete instanceof Document) {
562
-            if ($documentToDelete->type == 'document') {
563
-                $stmt = $this->getDbStatement('
557
+	public function deleteDocumentByPath($path)
558
+	{
559
+		$db = $this->getContentDbHandle();
560
+		$documentToDelete = $this->getDocumentByPath($path, 'unpublished');
561
+		if ($documentToDelete instanceof Document) {
562
+			if ($documentToDelete->type == 'document') {
563
+				$stmt = $this->getDbStatement('
564 564
                     DELETE FROM documents_unpublished
565 565
                           WHERE path = ' . $db->quote($path) . '
566 566
                 ');
567
-                $stmt->execute();
568
-            } elseif ($documentToDelete->type == 'folder') {
569
-                $folderPathWithWildcard = $path . '%';
570
-                $stmt = $this->getDbStatement('
567
+				$stmt->execute();
568
+			} elseif ($documentToDelete->type == 'folder') {
569
+				$folderPathWithWildcard = $path . '%';
570
+				$stmt = $this->getDbStatement('
571 571
                     DELETE FROM documents_unpublished
572 572
                           WHERE (path LIKE ' . $db->quote($folderPathWithWildcard) . '
573 573
                             AND substr(`path`, ' . (strlen($path) + 1) . ', 1) = "/")
574 574
                             OR path = ' . $db->quote($path) . '
575 575
                 ');
576
-                $stmt->execute();
577
-            }
578
-        }
579
-    }
576
+				$stmt->execute();
577
+			}
578
+		}
579
+	}
580 580
 
581 581
 	/**
582 582
 	 * @param $document
@@ -597,15 +597,15 @@  discard block
 block discarded – undo
597 597
 		return $documents;
598 598
 	}
599 599
 
600
-    private function initConfigIfNotExists($json, $subsetName)
601
-    {
602
-        $subsetFileName = $this->storagePath . DIRECTORY_SEPARATOR . $subsetName . '.json';
603
-        if (file_exists($subsetFileName)) {
604
-            $this->loadSubset($subsetName);
605
-        } else {
606
-            $changes = $subsetName . 'Changes';
607
-            $this->$subsetName = $json->$subsetName;
608
-            $this->$changes = true;
609
-        }
610
-    }
600
+	private function initConfigIfNotExists($json, $subsetName)
601
+	{
602
+		$subsetFileName = $this->storagePath . DIRECTORY_SEPARATOR . $subsetName . '.json';
603
+		if (file_exists($subsetFileName)) {
604
+			$this->loadSubset($subsetName);
605
+		} else {
606
+			$changes = $subsetName . 'Changes';
607
+			$this->$subsetName = $json->$subsetName;
608
+			$this->$changes = true;
609
+		}
610
+	}
611 611
 }
612 612
\ No newline at end of file
Please login to merge, or discard this patch.
src/storage/storage/RedirectsStorage.php 1 patch
Indentation   +83 added lines, -83 removed lines patch added patch discarded remove patch
@@ -10,94 +10,94 @@
 block discarded – undo
10 10
 
11 11
 class RedirectsStorage extends AbstractStorage
12 12
 {
13
-    /**
14
-     * Get all redirects
15
-     *
16
-     * @return mixed
17
-     */
18
-    public function getRedirects()
19
-    {
20
-        $redirects = $this->repository->redirects;
21
-        if ($redirects === null) {
22
-            $redirects = array();
23
-        }
24
-        usort($redirects, array($this, 'cmp'));
25
-        return $redirects;
26
-    }
13
+	/**
14
+	 * Get all redirects
15
+	 *
16
+	 * @return mixed
17
+	 */
18
+	public function getRedirects()
19
+	{
20
+		$redirects = $this->repository->redirects;
21
+		if ($redirects === null) {
22
+			$redirects = array();
23
+		}
24
+		usort($redirects, array($this, 'cmp'));
25
+		return $redirects;
26
+	}
27 27
 
28
-    /**
29
-     * Add a new redirect
30
-     * @param $postValues
31
-     */
32
-    public function addRedirect($postValues) {
33
-        $redirectObject = RedirectsFactory::createRedirectFromPostValues($postValues);
34
-        $redirects = $this->repository->redirects;
35
-        $redirects[] = $redirectObject;
36
-        $this->repository->redirects = $redirects;
37
-        $this->save();
38
-    }
28
+	/**
29
+	 * Add a new redirect
30
+	 * @param $postValues
31
+	 */
32
+	public function addRedirect($postValues) {
33
+		$redirectObject = RedirectsFactory::createRedirectFromPostValues($postValues);
34
+		$redirects = $this->repository->redirects;
35
+		$redirects[] = $redirectObject;
36
+		$this->repository->redirects = $redirects;
37
+		$this->save();
38
+	}
39 39
 
40
-    /**
41
-     * Get a redirect by it's slug
42
-     *
43
-     * @param $slug
44
-     * @return \stdClass|null
45
-     */
46
-    public function getRedirectBySlug($slug)
47
-    {
48
-        $redirects = $this->repository->redirects;
49
-        foreach ($redirects as $redirect) {
50
-            if ($redirect->slug == $slug) {
51
-                return $redirect;
52
-            }
53
-        }
40
+	/**
41
+	 * Get a redirect by it's slug
42
+	 *
43
+	 * @param $slug
44
+	 * @return \stdClass|null
45
+	 */
46
+	public function getRedirectBySlug($slug)
47
+	{
48
+		$redirects = $this->repository->redirects;
49
+		foreach ($redirects as $redirect) {
50
+			if ($redirect->slug == $slug) {
51
+				return $redirect;
52
+			}
53
+		}
54 54
 
55
-        return null;
56
-    }
55
+		return null;
56
+	}
57 57
 
58
-    /**
59
-     * Save a redirect by it's slug
60
-     * @param $slug
61
-     * @param $postValues
62
-     */
63
-    public function saveRedirect($slug, $postValues)
64
-    {
65
-        $redirectObject = RedirectsFactory::createRedirectFromPostValues($postValues);
58
+	/**
59
+	 * Save a redirect by it's slug
60
+	 * @param $slug
61
+	 * @param $postValues
62
+	 */
63
+	public function saveRedirect($slug, $postValues)
64
+	{
65
+		$redirectObject = RedirectsFactory::createRedirectFromPostValues($postValues);
66 66
 
67
-        $redirects = $this->repository->redirects;
68
-        foreach ($redirects as $key => $redirect) {
69
-            if ($redirect->slug == $slug) {
70
-                $redirects[$key] = $redirectObject;
71
-            }
72
-        }
73
-        $this->repository->redirects = $redirects;
74
-        $this->save();
75
-    }
67
+		$redirects = $this->repository->redirects;
68
+		foreach ($redirects as $key => $redirect) {
69
+			if ($redirect->slug == $slug) {
70
+				$redirects[$key] = $redirectObject;
71
+			}
72
+		}
73
+		$this->repository->redirects = $redirects;
74
+		$this->save();
75
+	}
76 76
 
77
-    /**
78
-     * Delete a redirect by it's slug
79
-     * @param $slug
80
-     */
81
-    public function deleteRedirectBySlug($slug)
82
-    {
83
-        $redirects = $this->repository->redirects;
84
-        foreach ($redirects as $key => $redirect) {
85
-            if ($redirect->slug == $slug) {
86
-                unset($redirects[$key]);
87
-            }
88
-        }
89
-        $redirects = array_values($redirects);
90
-        $this->repository->redirects = $redirects;
91
-        $this->save();
92
-    }
77
+	/**
78
+	 * Delete a redirect by it's slug
79
+	 * @param $slug
80
+	 */
81
+	public function deleteRedirectBySlug($slug)
82
+	{
83
+		$redirects = $this->repository->redirects;
84
+		foreach ($redirects as $key => $redirect) {
85
+			if ($redirect->slug == $slug) {
86
+				unset($redirects[$key]);
87
+			}
88
+		}
89
+		$redirects = array_values($redirects);
90
+		$this->repository->redirects = $redirects;
91
+		$this->save();
92
+	}
93 93
 
94
-    /**
95
-     * Compare a redirect by it's title
96
-     * @param $a
97
-     * @param $b
98
-     * @return int
99
-     */
100
-    public static function cmp($a, $b) {
101
-        return strcmp($a->title, $b->title);
102
-    }
94
+	/**
95
+	 * Compare a redirect by it's title
96
+	 * @param $a
97
+	 * @param $b
98
+	 * @return int
99
+	 */
100
+	public static function cmp($a, $b) {
101
+		return strcmp($a->title, $b->title);
102
+	}
103 103
 }
104 104
\ No newline at end of file
Please login to merge, or discard this patch.
src/storage/storage/FilesStorage.php 1 patch
Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -8,16 +8,16 @@  discard block
 block discarded – undo
8 8
 
9 9
 class FilesStorage extends AbstractStorage
10 10
 {
11
-    protected $filesDir;
11
+	protected $filesDir;
12 12
 
13
-    public function __construct($repository, $filesDir)
14
-    {
15
-        parent::__construct($repository);
16
-        $this->filesDir = $filesDir;
17
-    }
13
+	public function __construct($repository, $filesDir)
14
+	{
15
+		parent::__construct($repository);
16
+		$this->filesDir = $filesDir;
17
+	}
18 18
 
19 19
 
20
-    /**
20
+	/**
21 21
 	 * @return array
22 22
 	 */
23 23
 	public function getFiles() {
@@ -100,15 +100,15 @@  discard block
 block discarded – undo
100 100
 		}
101 101
 	}
102 102
 
103
-    /**
104
-     * @return mixed
105
-     */
106
-    public function getFilesDir()
107
-    {
108
-        return $this->filesDir;
109
-    }
103
+	/**
104
+	 * @return mixed
105
+	 */
106
+	public function getFilesDir()
107
+	{
108
+		return $this->filesDir;
109
+	}
110 110
 
111
-    /**
111
+	/**
112 112
 	 * @param $a
113 113
 	 * @param $b
114 114
 	 *
@@ -119,9 +119,9 @@  discard block
 block discarded – undo
119 119
 		return strcmp($a->file, $b->file);
120 120
 	}
121 121
 
122
-    protected function getDestinationPath()
123
-    {
124
-        $destinationPath = realpath($this->filesDir . DIRECTORY_SEPARATOR);
125
-        return $destinationPath;
126
-    }
122
+	protected function getDestinationPath()
123
+	{
124
+		$destinationPath = realpath($this->filesDir . DIRECTORY_SEPARATOR);
125
+		return $destinationPath;
126
+	}
127 127
 }
128 128
\ No newline at end of file
Please login to merge, or discard this patch.