aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/comments/backend/classes/javascriptbuild.php
blob: 14fedcdc9063abd0f01d2e1f77804637873c8877 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php namespace HashOver;

// Copyright (C) 2018 Jacob Barkdull
// This file is part of HashOver.
//
// HashOver is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// HashOver is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with HashOver.  If not, see <http://www.gnu.org/licenses/>.


class JavaScriptBuild
{
	protected $directory;
	protected $files = array ();

	public function __construct ($directory = '.')
	{
		$this->changeDirectory ($directory);
	}

	public function changeDirectory ($directory = '.')
	{
		$this->directory = trim ($directory, '/') . '/';
	}

	protected function addFile ($file)
	{
		// Add file to files array if it isn't already present
		if (!in_array ($file, $this->files, true)) {
			$this->files[] = $file;
		}
	}

	protected function addDependencies ($file, array $dependencies)
	{
		// Add each dependency to files array
		foreach ($dependencies as $dependency) {
			$dependency = realpath(__DIR__ . '/' . '/../../' . $this->directory) . '/' . $dependency;

			// Check if the file exists
			if (file_exists ($file)) {
				// If so, add file to files array
				$this->addFile ($dependency);
			} else {
				// If not, throw exception on failure
				$exception = '"%s" depends on "%s" but it does not exist.';
				$exception = sprintf ($exception, $file, $dependency);

				throw new \Exception ($exception);
			}
		}

		return true;
	}

	protected function includeFile ($file)
	{
		// Attempt to read JavaScript file
		$file = @file_get_contents ($file);

		// Check if the file read successfully
		if ($file !== false) {
			// If so, return the contents
			return trim ($file);
		}

		// Otherwise throw exception
		throw new \Exception (
			sprintf ('Unable to include "%s"', $file)
		);
	}

	public function registerFile ($file, array $options = array ())
	{
		$file = realpath(__DIR__ . '/' . '/../../' . $this->directory) . '/' . $file;

		if (!empty ($options)) {
			// Check if there is an include condition
			if (isset ($options['include'])) {
				// If so, return void if include is false
				if ($options['include'] === false) {
					return;
				}
			}

			// Add optional dependencies to files array
			if (!empty ($options['dependencies'])) {
				$dependencies = $options['dependencies'];
				$this->addDependencies ($file, $dependencies);
			}
		}

		// Check if the file exists
		if (file_exists ($file)) {
			// If so, add file to files array
			$this->addFile ($file);
		} else {
			// If not, throw exception
			throw new \Exception ('"' . $file . '" does not exist.');
		}

		return true;
	}

	public function build ($minify = false, $minify_level = 0)
	{
		// Array for included JavaScript files
		$files = array ();

		// Attempt to include registered JavaScript files
		foreach ($this->files as $file) {
			$files[] = $this->includeFile ($file);
		}

		// Join the included JavaScript files
		$javascript = implode (PHP_EOL . PHP_EOL, $files);

		// Minify the JavaScript if told to
		if (!isset ($_GET['hashover-unminified'])) {
			if ($minify === true and $minify_level > 0) {
				// Instantiate JavaScript minification class
				$minifier = new JavaScriptMinifier ();

				// Minify JavaScript build result
				$minified = $minifier->minify ($javascript, $minify_level);

				// Set minified result as JavaScript output
				$javascript = $minified;
			}
		}

		// Return normal JavaScript code
		return $javascript;
	}
}