From 2230dd4e6b148346c0ec596b9e3e12996a762ed8 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Thu, 22 May 2014 12:28:28 -0400 Subject: Code refactoring (add autoloader and change files organization) --- core/.htaccess | 1 - core/event.php | 149 ------------------------------ core/helper.php | 262 ---------------------------------------------------- core/registry.php | 79 ---------------- core/request.php | 56 ----------- core/response.php | 138 --------------------------- core/router.php | 66 ------------- core/session.php | 56 ----------- core/template.php | 40 -------- core/translator.php | 130 -------------------------- 10 files changed, 977 deletions(-) delete mode 100644 core/.htaccess delete mode 100644 core/event.php delete mode 100644 core/helper.php delete mode 100644 core/registry.php delete mode 100644 core/request.php delete mode 100644 core/response.php delete mode 100644 core/router.php delete mode 100644 core/session.php delete mode 100644 core/template.php delete mode 100644 core/translator.php (limited to 'core') diff --git a/core/.htaccess b/core/.htaccess deleted file mode 100644 index 14249c50..00000000 --- a/core/.htaccess +++ /dev/null @@ -1 +0,0 @@ -Deny from all \ No newline at end of file diff --git a/core/event.php b/core/event.php deleted file mode 100644 index 0941acac..00000000 --- a/core/event.php +++ /dev/null @@ -1,149 +0,0 @@ -listeners[$eventName])) { - $this->listeners[$eventName] = array(); - } - - $this->listeners[$eventName][] = $listener; - } - - /** - * Trigger an event - * - * @access public - * @param string $eventName Event name - * @param array $data Event data - */ - public function trigger($eventName, array $data) - { - $this->lastEvent = $eventName; - $this->events[] = $eventName; - - if (isset($this->listeners[$eventName])) { - foreach ($this->listeners[$eventName] as $listener) { - if ($listener->execute($data)) { - $this->lastListener = get_class($listener); - } - } - } - } - - /** - * Get the last listener executed - * - * @access public - * @return string Event name - */ - public function getLastListenerExecuted() - { - return $this->lastListener; - } - - /** - * Get the last fired event - * - * @access public - * @return string Event name - */ - public function getLastTriggeredEvent() - { - return $this->lastEvent; - } - - /** - * Get a list of triggered events - * - * @access public - * @return array - */ - public function getTriggeredEvents() - { - return $this->events; - } - - /** - * Check if a listener bind to an event - * - * @access public - * @param string $eventName Event name - * @param mixed $instance Instance name or object itself - * @return bool Yes or no - */ - public function hasListener($eventName, $instance) - { - if (isset($this->listeners[$eventName])) { - foreach ($this->listeners[$eventName] as $listener) { - if ($listener instanceof $instance) { - return true; - } - } - } - - return false; - } -} diff --git a/core/helper.php b/core/helper.php deleted file mode 100644 index 8351328a..00000000 --- a/core/helper.php +++ /dev/null @@ -1,262 +0,0 @@ -load($name, $args); -} - -function is_current_user($user_id) -{ - return $_SESSION['user']['id'] == $user_id; -} - -function is_admin() -{ - return $_SESSION['user']['is_admin'] == 1; -} - -function get_username() -{ - return $_SESSION['user']['username']; -} - -function parse($text) -{ - $text = markdown($text); - $text = preg_replace('!#(\d+)!i', '$0', $text); - return $text; -} - -function markdown($text) -{ - require_once __DIR__.'/../vendor/Michelf/MarkdownExtra.inc.php'; - - $parser = new \Michelf\MarkdownExtra; - $parser->no_markup = true; - $parser->no_entities = true; - - return $parser->transform($text); -} - -function get_current_base_url() -{ - $url = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; - $url .= $_SERVER['SERVER_NAME']; - $url .= $_SERVER['SERVER_PORT'] == 80 || $_SERVER['SERVER_PORT'] == 443 ? '' : ':'.$_SERVER['SERVER_PORT']; - $url .= dirname($_SERVER['PHP_SELF']) !== '/' ? dirname($_SERVER['PHP_SELF']).'/' : '/'; - - return $url; -} - -function escape($value) -{ - return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false); -} - -function flash($html) -{ - $data = ''; - - if (isset($_SESSION['flash_message'])) { - $data = sprintf($html, escape($_SESSION['flash_message'])); - unset($_SESSION['flash_message']); - } - - return $data; -} - -function flash_error($html) -{ - $data = ''; - - if (isset($_SESSION['flash_error_message'])) { - $data = sprintf($html, escape($_SESSION['flash_error_message'])); - unset($_SESSION['flash_error_message']); - } - - return $data; -} - -function format_bytes($size, $precision = 2) -{ - $base = log($size) / log(1024); - $suffixes = array('', 'k', 'M', 'G', 'T'); - - return round(pow(1024, $base - floor($base)), $precision).$suffixes[floor($base)]; -} - -function get_host_from_url($url) -{ - return escape(parse_url($url, PHP_URL_HOST)) ?: $url; -} - -function summary($value, $min_length = 5, $max_length = 120, $end = '[...]') -{ - $length = strlen($value); - - if ($length > $max_length) { - return substr($value, 0, strpos($value, ' ', $max_length)).' '.$end; - } - else if ($length < $min_length) { - return ''; - } - - return $value; -} - -function contains($haystack, $needle) -{ - return strpos($haystack, $needle) !== false; -} - -function in_list($id, array $listing, $default_value = '?') -{ - if (isset($listing[$id])) { - return escape($listing[$id]); - } - - return $default_value; -} - -function error_class(array $errors, $name) -{ - return ! isset($errors[$name]) ? '' : ' form-error'; -} - -function error_list(array $errors, $name) -{ - $html = ''; - - if (isset($errors[$name])) { - - $html .= ''; - } - - return $html; -} - -function form_value($values, $name) -{ - if (isset($values->$name)) { - return 'value="'.escape($values->$name).'"'; - } - - return isset($values[$name]) ? 'value="'.escape($values[$name]).'"' : ''; -} - -function form_hidden($name, $values = array()) -{ - return ''; -} - -function form_default_select($name, array $options, $values = array(), array $errors = array(), $class = '') -{ - $options = array('' => '?') + $options; - return form_select($name, $options, $values, $errors, $class); -} - -function form_select($name, array $options, $values = array(), array $errors = array(), $class = '') -{ - $html = ''; - $html .= error_list($errors, $name); - - return $html; -} - -function form_radios($name, array $options, array $values = array()) -{ - $html = ''; - - foreach ($options as $value => $label) { - $html .= form_radio($name, $label, $value, isset($values[$name]) && $values[$name] == $value); - } - - return $html; -} - -function form_radio($name, $label, $value, $selected = false, $class = '') -{ - return ''; -} - -function form_checkbox($name, $label, $value, $checked = false, $class = '') -{ - return ''; -} - -function form_label($label, $name, array $attributes = array()) -{ - return ''; -} - -function form_textarea($name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') -{ - $class .= error_class($errors, $name); - - $html = ''; - if (in_array('required', $attributes)) $html .= '*'; - $html .= error_list($errors, $name); - - return $html; -} - -function form_input($type, $name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') -{ - $class .= error_class($errors, $name); - - $html = ''; - if (in_array('required', $attributes)) $html .= '*'; - $html .= error_list($errors, $name); - - return $html; -} - -function form_text($name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') -{ - return form_input('text', $name, $values, $errors, $attributes, $class); -} - -function form_password($name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') -{ - return form_input('password', $name, $values, $errors, $attributes, $class); -} - -function form_email($name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') -{ - return form_input('email', $name, $values, $errors, $attributes, $class); -} - -function form_date($name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') -{ - return form_input('date', $name, $values, $errors, $attributes, $class); -} - -function form_number($name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') -{ - return form_input('number', $name, $values, $errors, $attributes, $class); -} diff --git a/core/registry.php b/core/registry.php deleted file mode 100644 index f11d427c..00000000 --- a/core/registry.php +++ /dev/null @@ -1,79 +0,0 @@ -container[$name] = $value; - } - - /** - * Get a dependency - * - * @access public - * @param string $name Unique identifier for the service/parameter - * @return mixed The value of the parameter or an object - * @throws RuntimeException If the identifier is not found - */ - public function __get($name) - { - if (isset($this->container[$name])) { - - if (is_callable($this->container[$name])) { - return $this->container[$name](); - } - else { - return $this->container[$name]; - } - } - - throw new \RuntimeException('Identifier not found in the registry: '.$name); - } - - /** - * Return a shared instance of a dependency - * - * @access public - * @param string $name Unique identifier for the service/parameter - * @return mixed Same object instance of the dependency - */ - public function shared($name) - { - if (! isset($this->instances[$name])) { - $this->instances[$name] = $this->$name; - } - - return $this->instances[$name]; - } -} diff --git a/core/request.php b/core/request.php deleted file mode 100644 index df8ea41a..00000000 --- a/core/request.php +++ /dev/null @@ -1,56 +0,0 @@ -getValues(); - return isset($values[$name]) ? $values[$name] : null; - } - - public function getValues() - { - if (! empty($_POST)) return $_POST; - - $result = json_decode($this->getBody(), true); - if ($result) return $result; - - return array(); - } - - public function getBody() - { - return file_get_contents('php://input'); - } - - public function getFileContent($name) - { - if (isset($_FILES[$name])) { - return file_get_contents($_FILES[$name]['tmp_name']); - } - - return ''; - } - - public function isPost() - { - return isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST'; - } - - public function isAjax() - { - return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest'; - } -} diff --git a/core/response.php b/core/response.php deleted file mode 100644 index a5f0e4dc..00000000 --- a/core/response.php +++ /dev/null @@ -1,138 +0,0 @@ -status($status_code); - - header('Content-Type: application/json'); - echo json_encode($data); - - exit; - } - - public function text($data, $status_code = 200) - { - $this->status($status_code); - - header('Content-Type: text/plain; charset=utf-8'); - echo $data; - - exit; - } - - public function html($data, $status_code = 200) - { - $this->status($status_code); - - header('Content-Type: text/html; charset=utf-8'); - echo $data; - - exit; - } - - public function xml($data, $status_code = 200) - { - $this->status($status_code); - - header('Content-Type: text/xml; charset=utf-8'); - echo $data; - - exit; - } - - public function js($data, $status_code = 200) - { - $this->status($status_code); - - header('Content-Type: text/javascript; charset=utf-8'); - echo $data; - - exit; - } - - public function binary($data, $status_code = 200) - { - $this->status($status_code); - - header('Content-Transfer-Encoding: binary'); - header('Content-Type: application/octet-stream'); - echo $data; - - exit; - } - - public function csp(array $policies = array()) - { - $policies['default-src'] = "'self'"; - $values = ''; - - foreach ($policies as $policy => $hosts) { - - if (is_array($hosts)) { - - $acl = ''; - - foreach ($hosts as &$host) { - - if ($host === '*' || $host === 'self' || strpos($host, 'http') === 0) { - $acl .= $host.' '; - } - } - } - else { - - $acl = $hosts; - } - - $values .= $policy.' '.trim($acl).'; '; - } - - header('Content-Security-Policy: '.$values); - } - - public function nosniff() - { - header('X-Content-Type-Options: nosniff'); - } - - public function xss() - { - header('X-XSS-Protection: 1; mode=block'); - } - - public function hsts() - { - if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') { - header('Strict-Transport-Security: max-age=31536000'); - } - } - - public function xframe($mode = 'DENY', array $urls = array()) - { - header('X-Frame-Options: '.$mode.' '.implode(' ', $urls)); - } -} diff --git a/core/router.php b/core/router.php deleted file mode 100644 index 56a95e52..00000000 --- a/core/router.php +++ /dev/null @@ -1,66 +0,0 @@ -registry = $registry; - $this->controller = empty($_GET['controller']) ? $controller : $_GET['controller']; - $this->action = empty($_GET['action']) ? $controller : $_GET['action']; - } - - /** - * @param string $default_value - */ - public function sanitize($value, $default_value) - { - return ! ctype_alpha($value) || empty($value) ? $default_value : strtolower($value); - } - - /** - * @param string $filename - * @param string $class - */ - public function load($filename, $class, $method) - { - if (file_exists($filename)) { - - require $filename; - - if (! method_exists($class, $method)) return false; - - $instance = new $class($this->registry); - $instance->request = new Request; - $instance->response = new Response; - $instance->session = new Session; - $instance->template = new Template; - $instance->beforeAction($this->controller, $this->action); - $instance->$method(); - - return true; - } - - return false; - } - - public function execute() - { - $this->controller = $this->sanitize($this->controller, 'app'); - $this->action = $this->sanitize($this->action, 'index'); - - if (! $this->load('controllers/'.$this->controller.'.php', '\Controller\\'.$this->controller, $this->action)) { - die('Page not found!'); - } - } -} diff --git a/core/session.php b/core/session.php deleted file mode 100644 index 0c3ec2d9..00000000 --- a/core/session.php +++ /dev/null @@ -1,56 +0,0 @@ - 'value']); - public function load() - { - if (func_num_args() < 1 || func_num_args() > 2) { - die('Invalid template arguments'); - } - - if (! file_exists(self::PATH.func_get_arg(0).'.php')) { - die('Unable to load the template: "'.func_get_arg(0).'"'); - } - - if (func_num_args() === 2) { - - if (! is_array(func_get_arg(1))) { - die('Template variables must be an array'); - } - - extract(func_get_arg(1)); - } - - ob_start(); - - include self::PATH.func_get_arg(0).'.php'; - - return ob_get_clean(); - } - - public function layout($template_name, array $template_args = array(), $layout_name = 'layout') - { - return $this->load($layout_name, $template_args + array('content_for_layout' => $this->load($template_name, $template_args))); - } -} diff --git a/core/translator.php b/core/translator.php deleted file mode 100644 index 8adb1bfd..00000000 --- a/core/translator.php +++ /dev/null @@ -1,130 +0,0 @@ -getFilename(), '.php') !== false) { - $locales = array_merge($locales, include $fileinfo->getPathname()); - } - } - } - - container($locales); - } - - function container($locales = null) - { - static $values = array(); - - if ($locales !== null) { - $values = $locales; - } - - return $values; - } -} - - -namespace { - - function t() { - return call_user_func_array('\Translator\translate', func_get_args()); - } - - function c() { - return call_user_func_array('\Translator\currency', func_get_args()); - } - - function n() { - return call_user_func_array('\Translator\number', func_get_args()); - } - - function dt() { - return call_user_func_array('\Translator\datetime', func_get_args()); - } - - function p($value, $t1, $t2) { - return $value > 1 ? $t2 : $t1; - } -} -- cgit v1.2.3