Completed
Pull Request — master (#286)
by Frank
07:37
created
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.
lib/Phile/ServiceLocator/TemplateInterface.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -12,19 +12,19 @@
 block discarded – undo
12 12
  * @package Phile\ServiceLocator
13 13
  */
14 14
 interface TemplateInterface {
15
-	/**
16
-	 * render the template
17
-	 *
18
-	 * @return mixed
19
-	 */
20
-	public function render();
15
+    /**
16
+     * render the template
17
+     *
18
+     * @return mixed
19
+     */
20
+    public function render();
21 21
 
22
-	/**
23
-	 * set current page
24
-	 *
25
-	 * @param \Phile\Model\Page $page
26
-	 *
27
-	 * @return mixed
28
-	 */
29
-	public function setCurrentPage(\Phile\Model\Page $page);
22
+    /**
23
+     * set current page
24
+     *
25
+     * @param \Phile\Model\Page $page
26
+     *
27
+     * @return mixed
28
+     */
29
+    public function setCurrentPage(\Phile\Model\Page $page);
30 30
 }
Please login to merge, or discard this patch.
lib/Phile/ServiceLocator/ParserInterface.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -1,7 +1,7 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 /**
3
- * The ServiceLocator interface
4
- */
3
+     * The ServiceLocator interface
4
+     */
5 5
 namespace Phile\ServiceLocator;
6 6
 
7 7
 /**
@@ -12,12 +12,12 @@  discard block
 block discarded – undo
12 12
  * @package Phile\ServiceLocator
13 13
  */
14 14
 interface ParserInterface {
15
-	/**
16
-	 * parse data
17
-	 *
18
-	 * @param $data
19
-	 *
20
-	 * @return mixed
21
-	 */
22
-	public function parse($data);
15
+    /**
16
+     * parse data
17
+     *
18
+     * @param $data
19
+     *
20
+     * @return mixed
21
+     */
22
+    public function parse($data);
23 23
 }
Please login to merge, or discard this patch.
lib/Phile/Gateway/EventObserverInterface.php 1 patch
Indentation   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -1,7 +1,7 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 /**
3
- * The EventObserverInterface
4
- */
3
+     * The EventObserverInterface
4
+     */
5 5
 namespace Phile\Gateway;
6 6
 
7 7
 /**
@@ -13,13 +13,13 @@  discard block
 block discarded – undo
13 13
  * @package Phile\Gateway
14 14
  */
15 15
 interface EventObserverInterface {
16
-	/**
17
-	 * event method
18
-	 *
19
-	 * @param string $eventKey
20
-	 * @param mixed  $data
21
-	 *
22
-	 * @return mixed
23
-	 */
24
-	public function on($eventKey, $data = null);
16
+    /**
17
+     * event method
18
+     *
19
+     * @param string $eventKey
20
+     * @param mixed  $data
21
+     *
22
+     * @return mixed
23
+     */
24
+    public function on($eventKey, $data = null);
25 25
 }
Please login to merge, or discard this patch.
lib/Phile/FilterIterator/GeneralFileFilterIterator.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -1,21 +1,21 @@
 block discarded – undo
1 1
 <?php
2 2
 /**
3
- * the filter class for all files
4
- */
3
+     * the filter class for all files
4
+     */
5 5
 namespace Phile\FilterIterator;
6 6
 /**
7
- * Class GeneralFileFilterIterator
8
- *
9
- * @package Phile\FilterIterator
10
- */
7
+     * Class GeneralFileFilterIterator
8
+     *
9
+     * @package Phile\FilterIterator
10
+     */
11 11
 class GeneralFileFilterIterator extends \FilterIterator {
12
-	/**
13
-	 * method to decide if file is filterd or not
14
-	 * @return bool
15
-	 */
16
-	public function accept() {
17
-		// accept all kind of files, no filter
18
-		return true;
19
-	}
12
+    /**
13
+     * method to decide if file is filterd or not
14
+     * @return bool
15
+     */
16
+    public function accept() {
17
+        // accept all kind of files, no filter
18
+        return true;
19
+    }
20 20
 
21 21
 }
22 22
\ No newline at end of file
Please login to merge, or discard this patch.
plugins/phile/phpFastCache/Classes/PhpFastCache.php 1 patch
Indentation   +64 added lines, -64 removed lines patch added patch discarded remove patch
@@ -1,7 +1,7 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 /**
3
- * The PhpFastCache implemenation class
4
- */
3
+     * The PhpFastCache implemenation class
4
+     */
5 5
 namespace Phile\Plugin\Phile\PhpFastCache;
6 6
 
7 7
 /**
@@ -13,73 +13,73 @@  discard block
 block discarded – undo
13 13
  * @package Phile\Plugin\Phile\PhpFastCache
14 14
  */
15 15
 class PhpFastCache implements \Phile\ServiceLocator\CacheInterface {
16
-	/**
17
-	 * @var \BasePhpFastCache the cache engine
18
-	 */
19
-	protected $cacheEngine;
16
+    /**
17
+     * @var \BasePhpFastCache the cache engine
18
+     */
19
+    protected $cacheEngine;
20 20
 
21
-	/**
22
-	 * the constructor
23
-	 *
24
-	 * @param \BasePhpFastCache $cacheEngine
25
-	 */
26
-	public function __construct(\BasePhpFastCache $cacheEngine) {
27
-		$this->cacheEngine = $cacheEngine;
28
-	}
21
+    /**
22
+     * the constructor
23
+     *
24
+     * @param \BasePhpFastCache $cacheEngine
25
+     */
26
+    public function __construct(\BasePhpFastCache $cacheEngine) {
27
+        $this->cacheEngine = $cacheEngine;
28
+    }
29 29
 
30
-	/**
31
-	 * method to check if cache has entry for given key
32
-	 *
33
-	 * @param $key
34
-	 *
35
-	 * @return bool|mixed
36
-	 */
37
-	public function has($key) {
38
-		return ($this->cacheEngine->get($key) !== null);
39
-	}
30
+    /**
31
+     * method to check if cache has entry for given key
32
+     *
33
+     * @param $key
34
+     *
35
+     * @return bool|mixed
36
+     */
37
+    public function has($key) {
38
+        return ($this->cacheEngine->get($key) !== null);
39
+    }
40 40
 
41
-	/**
42
-	 * method to get cache entry
43
-	 *
44
-	 * @param $key
45
-	 *
46
-	 * @return mixed|null
47
-	 */
48
-	public function get($key) {
49
-		return $this->cacheEngine->get($key);
50
-	}
41
+    /**
42
+     * method to get cache entry
43
+     *
44
+     * @param $key
45
+     *
46
+     * @return mixed|null
47
+     */
48
+    public function get($key) {
49
+        return $this->cacheEngine->get($key);
50
+    }
51 51
 
52
-	/**
53
-	 * method to set cache entry
54
-	 *
55
-	 * @param string $key
56
-	 * @param string $value
57
-	 * @param int    $time
58
-	 * @param array  $options
59
-	 *
60
-	 * @return mixed|void
61
-	 */
62
-	public function set($key, $value, $time = 300, array $options = array()) {
63
-		$this->cacheEngine->set($key, $value, $time, $options);
64
-	}
52
+    /**
53
+     * method to set cache entry
54
+     *
55
+     * @param string $key
56
+     * @param string $value
57
+     * @param int    $time
58
+     * @param array  $options
59
+     *
60
+     * @return mixed|void
61
+     */
62
+    public function set($key, $value, $time = 300, array $options = array()) {
63
+        $this->cacheEngine->set($key, $value, $time, $options);
64
+    }
65 65
 
66
-	/**
67
-	 * method to delete cache entry
68
-	 *
69
-	 * @param string $key
70
-	 * @param array  $options
71
-	 *
72
-	 * @return mixed|void
73
-	 */
74
-	public function delete($key, array $options = array()) {
75
-		$this->cacheEngine->delete($key, $options);
76
-	}
66
+    /**
67
+     * method to delete cache entry
68
+     *
69
+     * @param string $key
70
+     * @param array  $options
71
+     *
72
+     * @return mixed|void
73
+     */
74
+    public function delete($key, array $options = array()) {
75
+        $this->cacheEngine->delete($key, $options);
76
+    }
77 77
 
78
-	/**
79
-	 * clean complete cache and delete all cached entries
80
-	 */
81
-	public function clean() {
82
-		$this->cacheEngine->clean();
83
-	}
78
+    /**
79
+     * clean complete cache and delete all cached entries
80
+     */
81
+    public function clean() {
82
+        $this->cacheEngine->clean();
83
+    }
84 84
 
85 85
 }
Please login to merge, or discard this patch.
plugins/phile/phpFastCache/config.php 1 patch
Indentation   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -3,48 +3,48 @@
 block discarded – undo
3 3
  * config file for plugin
4 4
  */
5 5
 $config = [
6
-	/**
7
-	 * Default storage engine
8
-	 *
9
-	 * e.g. "files": $cache = phpFastCache(); <-- will be files cache
10
-	 *
11
-	 * auto, files, sqlite, auto, apc, wincache, xcache, memcache, memcached,
12
-	 */
13
-	'storage' => 'auto',
14
-
15
-	/**
16
-	 * Default Path for File Cache
17
-	 *
18
-	 * Use full PATH like /home/username/cache
19
-	 * Keep it blank "", it will automatic setup for you
20
-	 */
21
-	'path' => CACHE_DIR,
22
-
23
-	/**
24
-	 * Permissions for file storage
25
-	 */
6
+    /**
7
+     * Default storage engine
8
+     *
9
+     * e.g. "files": $cache = phpFastCache(); <-- will be files cache
10
+     *
11
+     * auto, files, sqlite, auto, apc, wincache, xcache, memcache, memcached,
12
+     */
13
+    'storage' => 'auto',
14
+
15
+    /**
16
+     * Default Path for File Cache
17
+     *
18
+     * Use full PATH like /home/username/cache
19
+     * Keep it blank "", it will automatic setup for you
20
+     */
21
+    'path' => CACHE_DIR,
22
+
23
+    /**
24
+     * Permissions for file storage
25
+     */
26 26
 //    'default_chmod' => 0777, // For security, please use 0666 for module and 0644 for cgi.
27 27
 
28 28
 
29 29
 //	"securityKey" => "auto", // default will good. It will create a path by PATH/securityKey
30 30
 
31
-	/*
31
+    /*
32 32
 	 * FallBack Driver
33 33
 	 * Example, in your code, you use memcached, apc..etc, but when you moved your web hosting
34 34
 	 * The new hosting don't have memcached, or apc. What you do? Set fallback that driver to other driver.
35 35
 	 */
36 36
 //    "fallback"  => "files",
37 37
 
38
-	/*
38
+    /*
39 39
 	 * .htaccess protect
40 40
 	 * default will be  true
41 41
 	 */
42 42
 //	"htaccess"    => true,
43 43
 
44
-	/*
44
+    /*
45 45
 	 * Default Memcache Server for all $cache = phpFastCache("memcache");
46 46
 	 */
47
-	/*
47
+    /*
48 48
 	"memcache"        =>  array(
49 49
 		array("127.0.0.1",11211,1),
50 50
 		//  array("new.host.ip",11211,1),
Please login to merge, or discard this patch.