Completed
Pull Request — master (#286)
by Frank
09:39 queued 06:18
created
lib/Phile/Core/Session.php 1 patch
Indentation   +96 added lines, -96 removed lines patch added patch discarded remove patch
@@ -1,7 +1,7 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 /**
3
- * The Session class
4
- */
3
+     * The Session class
4
+     */
5 5
 namespace Phile\Core;
6 6
 
7 7
 /**
@@ -13,109 +13,109 @@  discard block
 block discarded – undo
13 13
  * @package Phile\Core
14 14
  */
15 15
 class Session {
16
-	/** @var bool mark if session is started */
17
-	static public $isStarted = false;
16
+    /** @var bool mark if session is started */
17
+    static public $isStarted = false;
18 18
 
19
-	/** @var string the session id */
20
-	static public $sessionId = '';
19
+    /** @var string the session id */
20
+    static public $sessionId = '';
21 21
 
22
-	/**
23
-	 * method to start the session
24
-	 */
25
-	static public function start() {
26
-		if (self::$isStarted === false) {
27
-			session_cache_limiter('private');
28
-			session_cache_expire(120);
29
-			if (session_start()) {
30
-				self::$isStarted = true;
31
-			}
32
-			if (self::$isStarted) {
33
-				if (PHILE_CLI_MODE) {
34
-					$_SERVER['REMOTE_ADDR'] = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1';
35
-				}
36
-				if (self::get('REMOTE_ADDR') != $_SERVER['REMOTE_ADDR']) {
37
-					session_destroy();
38
-					session_start();
39
-					session_regenerate_id();
40
-					self::set('REMOTE_ADDR', $_SERVER['REMOTE_ADDR']);
41
-				}
42
-				if (self::get('REMOTE_ADDR') === null) {
43
-					self::set('REMOTE_ADDR', $_SERVER['REMOTE_ADDR']);
44
-				}
45
-			}
46
-		}
47
-		self::$sessionId = session_id();
48
-	}
22
+    /**
23
+     * method to start the session
24
+     */
25
+    static public function start() {
26
+        if (self::$isStarted === false) {
27
+            session_cache_limiter('private');
28
+            session_cache_expire(120);
29
+            if (session_start()) {
30
+                self::$isStarted = true;
31
+            }
32
+            if (self::$isStarted) {
33
+                if (PHILE_CLI_MODE) {
34
+                    $_SERVER['REMOTE_ADDR'] = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1';
35
+                }
36
+                if (self::get('REMOTE_ADDR') != $_SERVER['REMOTE_ADDR']) {
37
+                    session_destroy();
38
+                    session_start();
39
+                    session_regenerate_id();
40
+                    self::set('REMOTE_ADDR', $_SERVER['REMOTE_ADDR']);
41
+                }
42
+                if (self::get('REMOTE_ADDR') === null) {
43
+                    self::set('REMOTE_ADDR', $_SERVER['REMOTE_ADDR']);
44
+                }
45
+            }
46
+        }
47
+        self::$sessionId = session_id();
48
+    }
49 49
 
50
-	/**
51
-	 * method to destroy the session
52
-	 */
53
-	static public function destroy() {
54
-		unset($_SESSION);
55
-		session_destroy();
56
-	}
50
+    /**
51
+     * method to destroy the session
52
+     */
53
+    static public function destroy() {
54
+        unset($_SESSION);
55
+        session_destroy();
56
+    }
57 57
 
58
-	/**
59
-	 * method to save and close the session
60
-	 */
61
-	static public function save() {
62
-		session_write_close();
63
-	}
58
+    /**
59
+     * method to save and close the session
60
+     */
61
+    static public function save() {
62
+        session_write_close();
63
+    }
64 64
 
65
-	/**
66
-	 * method to set value into session
67
-	 *
68
-	 * @param string $key
69
-	 * @param mixed  $value
70
-	 */
71
-	static public function set($key, $value) {
72
-		if (!self::$isStarted) {
73
-			self::start();
74
-		}
75
-		$_SESSION[$key] = $value;
76
-	}
65
+    /**
66
+     * method to set value into session
67
+     *
68
+     * @param string $key
69
+     * @param mixed  $value
70
+     */
71
+    static public function set($key, $value) {
72
+        if (!self::$isStarted) {
73
+            self::start();
74
+        }
75
+        $_SESSION[$key] = $value;
76
+    }
77 77
 
78
-	/**
79
-	 * method to get value from session
80
-	 *
81
-	 * @param string $key
82
-	 * @param mixed  $default
83
-	 *
84
-	 * @return null|mixed
85
-	 */
86
-	static public function get($key, $default = null) {
87
-		if (!self::$isStarted) {
88
-			self::start();
89
-		}
78
+    /**
79
+     * method to get value from session
80
+     *
81
+     * @param string $key
82
+     * @param mixed  $default
83
+     *
84
+     * @return null|mixed
85
+     */
86
+    static public function get($key, $default = null) {
87
+        if (!self::$isStarted) {
88
+            self::start();
89
+        }
90 90
 
91
-		return (self::isEmpty($key)) ? $default : $_SESSION[$key];
92
-	}
91
+        return (self::isEmpty($key)) ? $default : $_SESSION[$key];
92
+    }
93 93
 
94
-	/**
95
-	 * get the session id
96
-	 *
97
-	 * @return string
98
-	 */
99
-	static public function getSessionId() {
100
-		if (!self::$isStarted) {
101
-			self::start();
102
-		}
94
+    /**
95
+     * get the session id
96
+     *
97
+     * @return string
98
+     */
99
+    static public function getSessionId() {
100
+        if (!self::$isStarted) {
101
+            self::start();
102
+        }
103 103
 
104
-		return self::$sessionId;
105
-	}
104
+        return self::$sessionId;
105
+    }
106 106
 
107
-	/**
108
-	 * check id key is empty/set or not
109
-	 *
110
-	 * @param $key
111
-	 *
112
-	 * @return bool
113
-	 */
114
-	static public function isEmpty($key) {
115
-		if (!self::$isStarted) {
116
-			self::start();
117
-		}
107
+    /**
108
+     * check id key is empty/set or not
109
+     *
110
+     * @param $key
111
+     *
112
+     * @return bool
113
+     */
114
+    static public function isEmpty($key) {
115
+        if (!self::$isStarted) {
116
+            self::start();
117
+        }
118 118
 
119
-		return (!isset($_SESSION[$key]));
120
-	}
119
+        return (!isset($_SESSION[$key]));
120
+    }
121 121
 }
Please login to merge, or discard this patch.
lib/Phile/Bootstrap.php 3 patches
Indentation   +168 added lines, -168 removed lines patch added patch discarded remove patch
@@ -16,172 +16,172 @@
 block discarded – undo
16 16
  * @version 0.1
17 17
  */
18 18
 class Bootstrap {
19
-	/**
20
-	 * @var \Phile\Bootstrap instance of Bootstrap class
21
-	 */
22
-	static protected $instance = NULL;
23
-
24
-	/**
25
-	 * @var array the settings array
26
-	 */
27
-	protected $settings;
28
-
29
-	/**
30
-	 * @var array the loaded plugins
31
-	 */
32
-	protected $plugins;
33
-
34
-	/**
35
-	 * the constructor
36
-	 * Disable direct creation of this object.
37
-	 */
38
-	protected function __construct() {
39
-	}
40
-
41
-	/**
42
-	 * Disable direct cloning of this object.
43
-	 */
44
-	protected function __clone() {
45
-	}
46
-
47
-	/**
48
-	 * Return instance of Bootstrap class as singleton
49
-	 *
50
-	 * @return Bootstrap
51
-	 */
52
-	static public function getInstance() {
53
-		if (is_null(static::$instance)) {
54
-			self::$instance = new Bootstrap();
55
-		}
56
-		return static::$instance;
57
-	}
58
-
59
-	/**
60
-	 * initialize basics
61
-	 */
62
-	public function initializeBasics() {
63
-		$this->initializeDefinitions();
64
-		$this->initializeAutoloader();
65
-		$this->initializeConfiguration();
66
-		$this->initializeFilesAndFolders();
67
-		$this->initializePlugins();
68
-		return $this;
69
-	}
70
-
71
-	/**
72
-	 * initialize the global definitions
73
-	 */
74
-	protected function initializeDefinitions() {
75
-		// for php unit testings, we need to check if constant is defined
76
-		// before setting them, because there is a bug in PHPUnit which
77
-		// init our bootstrap multiple times.
78
-		defined('PHILE_VERSION') 	or define('PHILE_VERSION',   '1.6.0');
79
-		defined('PHILE_CLI_MODE') 	or define('PHILE_CLI_MODE',  (php_sapi_name() == "cli") ? true : false);
80
-		defined('ROOT_DIR') 		or define('ROOT_DIR',        realpath(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR);
81
-		defined('CONTENT_DIR') 		or define('CONTENT_DIR',     ROOT_DIR . 'content' . DIRECTORY_SEPARATOR);
82
-		defined('CONTENT_EXT') 		or define('CONTENT_EXT',     '.md');
83
-		defined('LIB_DIR') 			or define('LIB_DIR',         ROOT_DIR . 'lib' . DIRECTORY_SEPARATOR);
84
-		defined('PLUGINS_DIR') 		or define('PLUGINS_DIR',     ROOT_DIR . 'plugins' . DIRECTORY_SEPARATOR);
85
-		defined('THEMES_DIR') 		or define('THEMES_DIR',      ROOT_DIR . 'themes' . DIRECTORY_SEPARATOR);
86
-		defined('CACHE_DIR') 		or define('CACHE_DIR',       LIB_DIR . 'cache' . DIRECTORY_SEPARATOR);
87
-		defined('STORAGE_DIR') or define('STORAGE_DIR', LIB_DIR . 'datastorage' . DIRECTORY_SEPARATOR);
88
-	}
89
-
90
-	/**
91
-	 * initialize the autoloader
92
-	 */
93
-	protected function initializeAutoloader() {
94
-		spl_autoload_extensions(".php");
95
-		// load phile core
96
-		spl_autoload_register(function ($className) {
97
-			$fileName = LIB_DIR . str_replace("\\", DIRECTORY_SEPARATOR, $className) . '.php';
98
-			if (file_exists($fileName)) {
99
-				require_once $fileName;
100
-			}
101
-		});
102
-		// load phile plugins
103
-		spl_autoload_register('\Phile\Plugin\PluginRepository::autoload');
104
-
105
-		require(LIB_DIR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php');
106
-	}
107
-
108
-	/**
109
-	 * initialize configuration
110
-	 */
111
-	protected function initializeConfiguration() {
112
-		$defaults      = Utility::load(ROOT_DIR . 'default_config.php');
113
-		$localSettings = Utility::load(ROOT_DIR . 'config.php');
114
-		if (is_array($localSettings)) {
115
-			$this->settings = array_replace_recursive($defaults, $localSettings);
116
-		} else {
117
-			$this->settings = $defaults;
118
-		}
119
-
120
-		Registry::set('Phile_Settings', $this->settings);
121
-		date_default_timezone_set($this->settings['timezone']);
122
-	}
123
-
124
-	/**
125
-	 * auto-setup of files and folders
126
-	 */
127
-	protected function initializeFilesAndFolders() {
128
-		$dirs = [
129
-			['path' => CACHE_DIR],
130
-			['path' => STORAGE_DIR]
131
-		];
132
-		$defaults = ['protected' => true];
133
-
134
-		foreach ($dirs as $dir) {
135
-			$dir += $defaults;
136
-			$path = $dir['path'];
137
-			if (empty($path) || strpos($path, ROOT_DIR) !== 0) {
138
-				continue;
139
-			}
140
-			if (!file_exists($path)) {
141
-				mkdir($path, 0775, true);
142
-			}
143
-			if ($dir['protected']) {
144
-				$file = "$path.htaccess";
145
-				if (!file_exists($file)) {
146
-					$content = "order deny,allow\ndeny from all\nallow from 127.0.0.1";
147
-					file_put_contents($file, $content);
148
-				}
149
-			}
150
-		}
151
-	}
152
-
153
-	/**
154
-	 * initialize plugins
155
-	 *
156
-	 * @throws Exception\PluginException
157
-	 */
158
-	protected function initializePlugins() {
159
-		$loader = new PluginRepository();
160
-		if (isset($this->settings['plugins']) && is_array($this->settings['plugins'])) {
161
-			$this->plugins = $loader->loadAll($this->settings['plugins']);
162
-		}
163
-
164
-		Event::triggerEvent('plugins_loaded', ['plugins' => $this->plugins]);
165
-
166
-		// throw not earlier to have the error-handler plugin loaded
167
-		// and initialized (by 'plugins_loaded' event)
168
-		$errors = $loader->getLoadErrors();
169
-		if (count($errors) > 0) {
170
-			throw new PluginException($errors[0]['message'], $errors[0]['code']);
171
-		}
172
-
173
-		// settings now include initialized plugin-configs
174
-		$this->settings = Registry::get('Phile_Settings');
175
-		Event::triggerEvent('config_loaded', ['config' => $this->settings]);
176
-	}
177
-
178
-	/**
179
-	 * method to get plugins
180
-	 * @return array
181
-	 * @deprecated since 1.5 will be removed
182
-	 * @use 'plugins_loaded' event
183
-	 */
184
-	public function getPlugins() {
185
-		return $this->plugins;
186
-	}
19
+    /**
20
+     * @var \Phile\Bootstrap instance of Bootstrap class
21
+     */
22
+    static protected $instance = NULL;
23
+
24
+    /**
25
+     * @var array the settings array
26
+     */
27
+    protected $settings;
28
+
29
+    /**
30
+     * @var array the loaded plugins
31
+     */
32
+    protected $plugins;
33
+
34
+    /**
35
+     * the constructor
36
+     * Disable direct creation of this object.
37
+     */
38
+    protected function __construct() {
39
+    }
40
+
41
+    /**
42
+     * Disable direct cloning of this object.
43
+     */
44
+    protected function __clone() {
45
+    }
46
+
47
+    /**
48
+     * Return instance of Bootstrap class as singleton
49
+     *
50
+     * @return Bootstrap
51
+     */
52
+    static public function getInstance() {
53
+        if (is_null(static::$instance)) {
54
+            self::$instance = new Bootstrap();
55
+        }
56
+        return static::$instance;
57
+    }
58
+
59
+    /**
60
+     * initialize basics
61
+     */
62
+    public function initializeBasics() {
63
+        $this->initializeDefinitions();
64
+        $this->initializeAutoloader();
65
+        $this->initializeConfiguration();
66
+        $this->initializeFilesAndFolders();
67
+        $this->initializePlugins();
68
+        return $this;
69
+    }
70
+
71
+    /**
72
+     * initialize the global definitions
73
+     */
74
+    protected function initializeDefinitions() {
75
+        // for php unit testings, we need to check if constant is defined
76
+        // before setting them, because there is a bug in PHPUnit which
77
+        // init our bootstrap multiple times.
78
+        defined('PHILE_VERSION') 	or define('PHILE_VERSION',   '1.6.0');
79
+        defined('PHILE_CLI_MODE') 	or define('PHILE_CLI_MODE',  (php_sapi_name() == "cli") ? true : false);
80
+        defined('ROOT_DIR') 		or define('ROOT_DIR',        realpath(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR);
81
+        defined('CONTENT_DIR') 		or define('CONTENT_DIR',     ROOT_DIR . 'content' . DIRECTORY_SEPARATOR);
82
+        defined('CONTENT_EXT') 		or define('CONTENT_EXT',     '.md');
83
+        defined('LIB_DIR') 			or define('LIB_DIR',         ROOT_DIR . 'lib' . DIRECTORY_SEPARATOR);
84
+        defined('PLUGINS_DIR') 		or define('PLUGINS_DIR',     ROOT_DIR . 'plugins' . DIRECTORY_SEPARATOR);
85
+        defined('THEMES_DIR') 		or define('THEMES_DIR',      ROOT_DIR . 'themes' . DIRECTORY_SEPARATOR);
86
+        defined('CACHE_DIR') 		or define('CACHE_DIR',       LIB_DIR . 'cache' . DIRECTORY_SEPARATOR);
87
+        defined('STORAGE_DIR') or define('STORAGE_DIR', LIB_DIR . 'datastorage' . DIRECTORY_SEPARATOR);
88
+    }
89
+
90
+    /**
91
+     * initialize the autoloader
92
+     */
93
+    protected function initializeAutoloader() {
94
+        spl_autoload_extensions(".php");
95
+        // load phile core
96
+        spl_autoload_register(function ($className) {
97
+            $fileName = LIB_DIR . str_replace("\\", DIRECTORY_SEPARATOR, $className) . '.php';
98
+            if (file_exists($fileName)) {
99
+                require_once $fileName;
100
+            }
101
+        });
102
+        // load phile plugins
103
+        spl_autoload_register('\Phile\Plugin\PluginRepository::autoload');
104
+
105
+        require(LIB_DIR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php');
106
+    }
107
+
108
+    /**
109
+     * initialize configuration
110
+     */
111
+    protected function initializeConfiguration() {
112
+        $defaults      = Utility::load(ROOT_DIR . 'default_config.php');
113
+        $localSettings = Utility::load(ROOT_DIR . 'config.php');
114
+        if (is_array($localSettings)) {
115
+            $this->settings = array_replace_recursive($defaults, $localSettings);
116
+        } else {
117
+            $this->settings = $defaults;
118
+        }
119
+
120
+        Registry::set('Phile_Settings', $this->settings);
121
+        date_default_timezone_set($this->settings['timezone']);
122
+    }
123
+
124
+    /**
125
+     * auto-setup of files and folders
126
+     */
127
+    protected function initializeFilesAndFolders() {
128
+        $dirs = [
129
+            ['path' => CACHE_DIR],
130
+            ['path' => STORAGE_DIR]
131
+        ];
132
+        $defaults = ['protected' => true];
133
+
134
+        foreach ($dirs as $dir) {
135
+            $dir += $defaults;
136
+            $path = $dir['path'];
137
+            if (empty($path) || strpos($path, ROOT_DIR) !== 0) {
138
+                continue;
139
+            }
140
+            if (!file_exists($path)) {
141
+                mkdir($path, 0775, true);
142
+            }
143
+            if ($dir['protected']) {
144
+                $file = "$path.htaccess";
145
+                if (!file_exists($file)) {
146
+                    $content = "order deny,allow\ndeny from all\nallow from 127.0.0.1";
147
+                    file_put_contents($file, $content);
148
+                }
149
+            }
150
+        }
151
+    }
152
+
153
+    /**
154
+     * initialize plugins
155
+     *
156
+     * @throws Exception\PluginException
157
+     */
158
+    protected function initializePlugins() {
159
+        $loader = new PluginRepository();
160
+        if (isset($this->settings['plugins']) && is_array($this->settings['plugins'])) {
161
+            $this->plugins = $loader->loadAll($this->settings['plugins']);
162
+        }
163
+
164
+        Event::triggerEvent('plugins_loaded', ['plugins' => $this->plugins]);
165
+
166
+        // throw not earlier to have the error-handler plugin loaded
167
+        // and initialized (by 'plugins_loaded' event)
168
+        $errors = $loader->getLoadErrors();
169
+        if (count($errors) > 0) {
170
+            throw new PluginException($errors[0]['message'], $errors[0]['code']);
171
+        }
172
+
173
+        // settings now include initialized plugin-configs
174
+        $this->settings = Registry::get('Phile_Settings');
175
+        Event::triggerEvent('config_loaded', ['config' => $this->settings]);
176
+    }
177
+
178
+    /**
179
+     * method to get plugins
180
+     * @return array
181
+     * @deprecated since 1.5 will be removed
182
+     * @use 'plugins_loaded' event
183
+     */
184
+    public function getPlugins() {
185
+        return $this->plugins;
186
+    }
187 187
 }
Please login to merge, or discard this patch.
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -75,15 +75,15 @@  discard block
 block discarded – undo
75 75
 		// for php unit testings, we need to check if constant is defined
76 76
 		// before setting them, because there is a bug in PHPUnit which
77 77
 		// init our bootstrap multiple times.
78
-		defined('PHILE_VERSION') 	or define('PHILE_VERSION',   '1.6.0');
79
-		defined('PHILE_CLI_MODE') 	or define('PHILE_CLI_MODE',  (php_sapi_name() == "cli") ? true : false);
80
-		defined('ROOT_DIR') 		or define('ROOT_DIR',        realpath(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR);
81
-		defined('CONTENT_DIR') 		or define('CONTENT_DIR',     ROOT_DIR . 'content' . DIRECTORY_SEPARATOR);
82
-		defined('CONTENT_EXT') 		or define('CONTENT_EXT',     '.md');
83
-		defined('LIB_DIR') 			or define('LIB_DIR',         ROOT_DIR . 'lib' . DIRECTORY_SEPARATOR);
84
-		defined('PLUGINS_DIR') 		or define('PLUGINS_DIR',     ROOT_DIR . 'plugins' . DIRECTORY_SEPARATOR);
85
-		defined('THEMES_DIR') 		or define('THEMES_DIR',      ROOT_DIR . 'themes' . DIRECTORY_SEPARATOR);
86
-		defined('CACHE_DIR') 		or define('CACHE_DIR',       LIB_DIR . 'cache' . DIRECTORY_SEPARATOR);
78
+		defined('PHILE_VERSION') or define('PHILE_VERSION', '1.6.0');
79
+		defined('PHILE_CLI_MODE') or define('PHILE_CLI_MODE', (php_sapi_name() == "cli") ? true : false);
80
+		defined('ROOT_DIR') or define('ROOT_DIR', realpath(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR);
81
+		defined('CONTENT_DIR') or define('CONTENT_DIR', ROOT_DIR . 'content' . DIRECTORY_SEPARATOR);
82
+		defined('CONTENT_EXT') or define('CONTENT_EXT', '.md');
83
+		defined('LIB_DIR') or define('LIB_DIR', ROOT_DIR . 'lib' . DIRECTORY_SEPARATOR);
84
+		defined('PLUGINS_DIR') or define('PLUGINS_DIR', ROOT_DIR . 'plugins' . DIRECTORY_SEPARATOR);
85
+		defined('THEMES_DIR') or define('THEMES_DIR', ROOT_DIR . 'themes' . DIRECTORY_SEPARATOR);
86
+		defined('CACHE_DIR') or define('CACHE_DIR', LIB_DIR . 'cache' . DIRECTORY_SEPARATOR);
87 87
 		defined('STORAGE_DIR') or define('STORAGE_DIR', LIB_DIR . 'datastorage' . DIRECTORY_SEPARATOR);
88 88
 	}
89 89
 
@@ -93,7 +93,7 @@  discard block
 block discarded – undo
93 93
 	protected function initializeAutoloader() {
94 94
 		spl_autoload_extensions(".php");
95 95
 		// load phile core
96
-		spl_autoload_register(function ($className) {
96
+		spl_autoload_register(function($className) {
97 97
 			$fileName = LIB_DIR . str_replace("\\", DIRECTORY_SEPARATOR, $className) . '.php';
98 98
 			if (file_exists($fileName)) {
99 99
 				require_once $fileName;
Please login to merge, or discard this patch.
Upper-Lower-Casing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -19,7 +19,7 @@
 block discarded – undo
19 19
 	/**
20 20
 	 * @var \Phile\Bootstrap instance of Bootstrap class
21 21
 	 */
22
-	static protected $instance = NULL;
22
+	static protected $instance = null;
23 23
 
24 24
 	/**
25 25
 	 * @var array the settings array
Please login to merge, or discard this patch.
lib/Phile/Repository/Page.php 1 patch
Indentation   +178 added lines, -178 removed lines patch added patch discarded remove patch
@@ -1,7 +1,7 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 /**
3
- * the page repository class
4
- */
3
+     * the page repository class
4
+     */
5 5
 namespace Phile\Repository;
6 6
 
7 7
 use Phile\Core\Registry;
@@ -17,181 +17,181 @@  discard block
 block discarded – undo
17 17
  * @package Phile\Repository
18 18
  */
19 19
 class Page {
20
-	/**
21
-	 * @var array the settings array
22
-	 */
23
-	protected $settings;
24
-
25
-	/**
26
-	 * @var array object storage for initialized objects, to prevent multiple loading of objects.
27
-	 */
28
-	protected $storage = array();
29
-
30
-	/**
31
-	 * @var \Phile\ServiceLocator\CacheInterface the cache implementation
32
-	 */
33
-	protected $cache = null;
34
-
35
-	/**
36
-	 * the constructor
37
-	 */
38
-	public function __construct($settings = null) {
39
-		if ($settings === null) {
40
-			$settings = Registry::get('Phile_Settings');
41
-		}
42
-		$this->settings = $settings;
43
-		if (ServiceLocator::hasService('Phile_Cache')) {
44
-			$this->cache = ServiceLocator::getService('Phile_Cache');
45
-		}
46
-	}
47
-
48
-	/**
49
-	 * find a page by path
50
-	 *
51
-	 * @param string $pageId
52
-	 * @param string $folder
53
-	 *
54
-	 * @return null|\Phile\Model\Page
55
-	 */
56
-	public function findByPath($pageId, $folder = CONTENT_DIR) {
57
-		// be merciful to lazy third-party-usage and accept a leading slash
58
-		$pageId = ltrim($pageId, '/');
59
-		// 'sub/' should serve page 'sub/index'
60
-		if ($pageId === '' || substr($pageId, -1) === '/') {
61
-			$pageId .= 'index';
62
-		}
63
-
64
-		$file = $folder . $pageId . CONTENT_EXT;
65
-		if (!file_exists($file)) {
66
-			if (substr($pageId, -6) === '/index') {
67
-				// try to resolve sub-directory 'sub/' to page 'sub'
68
-				$pageId = substr($pageId, 0, strlen($pageId) - 6);
69
-			} else {
70
-				// try to resolve page 'sub' to sub-directory 'sub/'
71
-				$pageId .= '/index';
72
-			}
73
-			$file = $folder . $pageId . CONTENT_EXT;
74
-		}
75
-		if (!file_exists($file)) {
76
-			return null;
77
-		}
78
-		return $this->getPage($file, $folder);
79
-	}
80
-
81
-	/**
82
-	 * find all pages (*.md) files and returns an array of Page models
83
-	 *
84
-	 * @param array  $options
85
-	 * @param string $folder
86
-	 *
87
-	 * @return PageCollection of \Phile\Model\Page objects
88
-	 */
89
-	public function findAll(array $options = array(), $folder = CONTENT_DIR) {
90
-		return new PageCollection(function() use ($options, $folder){
91
-			$options += $this->settings;
92
-			// ignore files with a leading '.' in its filename
93
-			$files = Utility::getFiles($folder, '\Phile\FilterIterator\ContentFileFilterIterator');
94
-			$pages = array();
95
-			foreach ($files as $file) {
96
-				if (str_replace($folder, '', $file) == '404' . CONTENT_EXT) {
97
-					// jump to next page if file is the 404 page
98
-					continue;
99
-				}
100
-				$pages[] = $this->getPage($file, $folder);
101
-			}
102
-
103
-			if (empty($options['pages_order'])) {
104
-				return $pages;
105
-			}
106
-
107
-			// parse search	criteria
108
-			$terms = preg_split('/\s+/', $options['pages_order'], -1, PREG_SPLIT_NO_EMPTY);
109
-			foreach ($terms as $term) {
110
-				$term = explode('.', $term);
111
-				if (count($term) > 1) {
112
-					$type = array_shift($term);
113
-				} else {
114
-					$type = null;
115
-				}
116
-				$term = explode(':', $term[0]);
117
-				$sorting[] = array('type' => $type, 'key' => $term[0], 'order' => $term[1]);
118
-			}
119
-
120
-			// prepare search criteria for array_multisort
121
-			foreach ($sorting as $sort) {
122
-				$key = $sort['key'];
123
-				$column = array();
124
-				foreach ($pages as $page) {
125
-					/** @var \Phile\Model\Page $page */
126
-					$meta = $page->getMeta();
127
-					if ($sort['type'] === 'page') {
128
-						$method = 'get' . ucfirst($key);
129
-						$value = $page->$method();
130
-					} elseif ($sort['type'] === 'meta') {
131
-						$value = $meta->get($key);
132
-					} else {
133
-						continue 2; // ignore unhandled search term
134
-					}
135
-					$column[] = $value;
136
-				}
137
-				$sortHelper[] = $column;
138
-				$sortHelper[] = constant('SORT_' . strtoupper($sort['order']));
139
-			}
140
-			$sortHelper[] = &$pages;
141
-
142
-			call_user_func_array('array_multisort', $sortHelper);
143
-
144
-			return $pages;
145
-		});
146
-	}
147
-
148
-	/**
149
-	 * return page at offset from $page in applied search order
150
-	 *
151
-	 * @param \Phile\Model\Page $page
152
-	 * @param int $offset
153
-	 * @return null|\Phile\Model\Page
154
-	 */
155
-	public function getPageOffset(\Phile\Model\Page $page, $offset = 0) {
156
-		$pages = $this->findAll();
157
-		$order = array();
158
-		foreach ($pages as $p) {
159
-			$order[] = $p->getFilePath();
160
-		}
161
-		$key = array_search($page->getFilePath(), $order) + $offset;
162
-		if (!isset($order[$key])) {
163
-			return null;
164
-		}
165
-		return $this->getPage($order[$key]);
166
-	}
167
-
168
-	/**
169
-	 * get page from cache or filepath
170
-	 *
171
-	 * @param        $filePath
172
-	 * @param string $folder
173
-	 *
174
-	 * @return mixed|\Phile\Model\Page
175
-	 */
176
-	protected function getPage($filePath, $folder = CONTENT_DIR) {
177
-		$key = 'Phile_Model_Page_' . md5($filePath);
178
-		if (isset($this->storage[$key])) {
179
-			return $this->storage[$key];
180
-		}
181
-
182
-		if ($this->cache !== null) {
183
-			if ($this->cache->has($key)) {
184
-				$page = $this->cache->get($key);
185
-			} else {
186
-				$page = new \Phile\Model\Page($filePath, $folder);
187
-				$this->cache->set($key, $page);
188
-			}
189
-		} else {
190
-			$page = new \Phile\Model\Page($filePath, $folder);
191
-		}
192
-		$this->storage[$key] = $page;
193
-
194
-		return $page;
195
-	}
20
+    /**
21
+     * @var array the settings array
22
+     */
23
+    protected $settings;
24
+
25
+    /**
26
+     * @var array object storage for initialized objects, to prevent multiple loading of objects.
27
+     */
28
+    protected $storage = array();
29
+
30
+    /**
31
+     * @var \Phile\ServiceLocator\CacheInterface the cache implementation
32
+     */
33
+    protected $cache = null;
34
+
35
+    /**
36
+     * the constructor
37
+     */
38
+    public function __construct($settings = null) {
39
+        if ($settings === null) {
40
+            $settings = Registry::get('Phile_Settings');
41
+        }
42
+        $this->settings = $settings;
43
+        if (ServiceLocator::hasService('Phile_Cache')) {
44
+            $this->cache = ServiceLocator::getService('Phile_Cache');
45
+        }
46
+    }
47
+
48
+    /**
49
+     * find a page by path
50
+     *
51
+     * @param string $pageId
52
+     * @param string $folder
53
+     *
54
+     * @return null|\Phile\Model\Page
55
+     */
56
+    public function findByPath($pageId, $folder = CONTENT_DIR) {
57
+        // be merciful to lazy third-party-usage and accept a leading slash
58
+        $pageId = ltrim($pageId, '/');
59
+        // 'sub/' should serve page 'sub/index'
60
+        if ($pageId === '' || substr($pageId, -1) === '/') {
61
+            $pageId .= 'index';
62
+        }
63
+
64
+        $file = $folder . $pageId . CONTENT_EXT;
65
+        if (!file_exists($file)) {
66
+            if (substr($pageId, -6) === '/index') {
67
+                // try to resolve sub-directory 'sub/' to page 'sub'
68
+                $pageId = substr($pageId, 0, strlen($pageId) - 6);
69
+            } else {
70
+                // try to resolve page 'sub' to sub-directory 'sub/'
71
+                $pageId .= '/index';
72
+            }
73
+            $file = $folder . $pageId . CONTENT_EXT;
74
+        }
75
+        if (!file_exists($file)) {
76
+            return null;
77
+        }
78
+        return $this->getPage($file, $folder);
79
+    }
80
+
81
+    /**
82
+     * find all pages (*.md) files and returns an array of Page models
83
+     *
84
+     * @param array  $options
85
+     * @param string $folder
86
+     *
87
+     * @return PageCollection of \Phile\Model\Page objects
88
+     */
89
+    public function findAll(array $options = array(), $folder = CONTENT_DIR) {
90
+        return new PageCollection(function() use ($options, $folder){
91
+            $options += $this->settings;
92
+            // ignore files with a leading '.' in its filename
93
+            $files = Utility::getFiles($folder, '\Phile\FilterIterator\ContentFileFilterIterator');
94
+            $pages = array();
95
+            foreach ($files as $file) {
96
+                if (str_replace($folder, '', $file) == '404' . CONTENT_EXT) {
97
+                    // jump to next page if file is the 404 page
98
+                    continue;
99
+                }
100
+                $pages[] = $this->getPage($file, $folder);
101
+            }
102
+
103
+            if (empty($options['pages_order'])) {
104
+                return $pages;
105
+            }
106
+
107
+            // parse search	criteria
108
+            $terms = preg_split('/\s+/', $options['pages_order'], -1, PREG_SPLIT_NO_EMPTY);
109
+            foreach ($terms as $term) {
110
+                $term = explode('.', $term);
111
+                if (count($term) > 1) {
112
+                    $type = array_shift($term);
113
+                } else {
114
+                    $type = null;
115
+                }
116
+                $term = explode(':', $term[0]);
117
+                $sorting[] = array('type' => $type, 'key' => $term[0], 'order' => $term[1]);
118
+            }
119
+
120
+            // prepare search criteria for array_multisort
121
+            foreach ($sorting as $sort) {
122
+                $key = $sort['key'];
123
+                $column = array();
124
+                foreach ($pages as $page) {
125
+                    /** @var \Phile\Model\Page $page */
126
+                    $meta = $page->getMeta();
127
+                    if ($sort['type'] === 'page') {
128
+                        $method = 'get' . ucfirst($key);
129
+                        $value = $page->$method();
130
+                    } elseif ($sort['type'] === 'meta') {
131
+                        $value = $meta->get($key);
132
+                    } else {
133
+                        continue 2; // ignore unhandled search term
134
+                    }
135
+                    $column[] = $value;
136
+                }
137
+                $sortHelper[] = $column;
138
+                $sortHelper[] = constant('SORT_' . strtoupper($sort['order']));
139
+            }
140
+            $sortHelper[] = &$pages;
141
+
142
+            call_user_func_array('array_multisort', $sortHelper);
143
+
144
+            return $pages;
145
+        });
146
+    }
147
+
148
+    /**
149
+     * return page at offset from $page in applied search order
150
+     *
151
+     * @param \Phile\Model\Page $page
152
+     * @param int $offset
153
+     * @return null|\Phile\Model\Page
154
+     */
155
+    public function getPageOffset(\Phile\Model\Page $page, $offset = 0) {
156
+        $pages = $this->findAll();
157
+        $order = array();
158
+        foreach ($pages as $p) {
159
+            $order[] = $p->getFilePath();
160
+        }
161
+        $key = array_search($page->getFilePath(), $order) + $offset;
162
+        if (!isset($order[$key])) {
163
+            return null;
164
+        }
165
+        return $this->getPage($order[$key]);
166
+    }
167
+
168
+    /**
169
+     * get page from cache or filepath
170
+     *
171
+     * @param        $filePath
172
+     * @param string $folder
173
+     *
174
+     * @return mixed|\Phile\Model\Page
175
+     */
176
+    protected function getPage($filePath, $folder = CONTENT_DIR) {
177
+        $key = 'Phile_Model_Page_' . md5($filePath);
178
+        if (isset($this->storage[$key])) {
179
+            return $this->storage[$key];
180
+        }
181
+
182
+        if ($this->cache !== null) {
183
+            if ($this->cache->has($key)) {
184
+                $page = $this->cache->get($key);
185
+            } else {
186
+                $page = new \Phile\Model\Page($filePath, $folder);
187
+                $this->cache->set($key, $page);
188
+            }
189
+        } else {
190
+            $page = new \Phile\Model\Page($filePath, $folder);
191
+        }
192
+        $this->storage[$key] = $page;
193
+
194
+        return $page;
195
+    }
196 196
 
197 197
 }
Please login to merge, or discard this patch.
lib/Phile/Repository/PageCollection.php 2 patches
Indentation   +59 added lines, -59 removed lines patch added patch discarded remove patch
@@ -11,73 +11,73 @@
 block discarded – undo
11 11
  * @package Phile\Repository
12 12
  */
13 13
 class PageCollection implements \ArrayAccess, \IteratorAggregate, \Countable {
14
-	/**
15
-	 * @var callback A function to be used for loading the pages.
16
-	 */
17
-	private	$loader;
14
+    /**
15
+     * @var callback A function to be used for loading the pages.
16
+     */
17
+    private	$loader;
18 18
 
19
-	/**
20
-	 * @var array of \Phile\Model\Page
21
-	 */
22
-	private	$pages;
19
+    /**
20
+     * @var array of \Phile\Model\Page
21
+     */
22
+    private	$pages;
23 23
 
24
-	/**
25
-	 * Constructor.
26
-	 *
27
-	 * @param callable $loader pages loader
28
-	 */
29
-	public function __construct(callable $loader) {
30
-		$this->loader = $loader;
31
-	}
24
+    /**
25
+     * Constructor.
26
+     *
27
+     * @param callable $loader pages loader
28
+     */
29
+    public function __construct(callable $loader) {
30
+        $this->loader = $loader;
31
+    }
32 32
 
33
-	/**
34
-	 * Perform page loading.
35
-	 *
36
-	 * @return void
37
-	 */
38
-	private function load() {
39
-		if ($this->pages === null) {
40
-			$this->pages = call_user_func($this->loader);
41
-		}
42
-	}
33
+    /**
34
+     * Perform page loading.
35
+     *
36
+     * @return void
37
+     */
38
+    private function load() {
39
+        if ($this->pages === null) {
40
+            $this->pages = call_user_func($this->loader);
41
+        }
42
+    }
43 43
 
44
-	/**
45
-	 * Get pages in a array.
46
-	 *
47
-	 * @return array of \Phile\Model\Page
48
-	 */
49
-	public function toArray() {
50
-		$this->load();
51
-		return $this->pages;
52
-	}
44
+    /**
45
+     * Get pages in a array.
46
+     *
47
+     * @return array of \Phile\Model\Page
48
+     */
49
+    public function toArray() {
50
+        $this->load();
51
+        return $this->pages;
52
+    }
53 53
 
54
-	public function getIterator() {
55
-		$this->load();
56
-		return new \ArrayIterator($this->pages);
57
-	}
54
+    public function getIterator() {
55
+        $this->load();
56
+        return new \ArrayIterator($this->pages);
57
+    }
58 58
 
59
-	public function offsetExists($offset) {
60
-		$this->load();
61
-		return isset($this->pages[$offset]);
62
-	}
59
+    public function offsetExists($offset) {
60
+        $this->load();
61
+        return isset($this->pages[$offset]);
62
+    }
63 63
 
64
-	public function	offsetGet($offset) {
65
-		$this->load();
66
-		return $this->pages[$offset];
67
-	}
64
+    public function	offsetGet($offset) {
65
+        $this->load();
66
+        return $this->pages[$offset];
67
+    }
68 68
 
69
-	public function	offsetSet($offset, $value) {
70
-		$this->load();
71
-		$this->pages[$offset] =	$value;
72
-	}
69
+    public function	offsetSet($offset, $value) {
70
+        $this->load();
71
+        $this->pages[$offset] =	$value;
72
+    }
73 73
 
74
-	public function	offsetUnset($offset) {
75
-		$this->load();
76
-		unset($this->pages[$offset]);
77
-	}
74
+    public function	offsetUnset($offset) {
75
+        $this->load();
76
+        unset($this->pages[$offset]);
77
+    }
78 78
 
79
-	public function	count() {
80
-		$this->load();
81
-		return count($this->pages);
82
-	}
79
+    public function	count() {
80
+        $this->load();
81
+        return count($this->pages);
82
+    }
83 83
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -68,7 +68,7 @@
 block discarded – undo
68 68
 
69 69
 	public function	offsetSet($offset, $value) {
70 70
 		$this->load();
71
-		$this->pages[$offset] =	$value;
71
+		$this->pages[$offset] = $value;
72 72
 	}
73 73
 
74 74
 	public function	offsetUnset($offset) {
Please login to merge, or discard this patch.