From 252a3308920770b6a62fe4805b59bdd9e08c162f Mon Sep 17 00:00:00 2001 From: Rougin Gutib Date: Wed, 16 Sep 2015 14:20:29 +0800 Subject: [PATCH] Updated functionalities --- README.md | 9 ++-- src/GetInstance.php | 5 ++ src/Instance.php | 125 +++++++++++++++++++++++++++----------------- 3 files changed, 87 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 6dd79c2..cd9aba4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Spark Plug -[![Latest Stable Version](https://poser.pugx.org/rougin/spark-plug/v/stable)](https://packagist.org/packages/rougin/spark-plug) [![Total Downloads](https://poser.pugx.org/rougin/spark-plug/downloads)](https://packagist.org/packages/rougin/spark-plug) [![Latest Unstable Version](https://poser.pugx.org/rougin/spark-plug/v/unstable)](https://packagist.org/packages/rougin/spark-plug) [![License](https://poser.pugx.org/rougin/spark-plug/license)](https://packagist.org/packages/rougin/spark-plug) +[![Latest Stable Version](https://poser.pugx.org/rougin/spark-plug/v/stable)](https://packagist.org/packages/rougin/spark-plug) [![Total Downloads](https://poser.pugx.org/rougin/spark-plug/downloads)](https://packagist.org/packages/rougin/spark-plug) [![Latest Unstable Version](https://poser.pugx.org/rougin/spark-plug/v/unstable)](https://packagist.org/packages/rougin/spark-plug) [![License](https://poser.pugx.org/rougin/spark-plug/license)](https://packagist.org/packages/rougin/spark-plug) [![endorse](https://api.coderwall.com/rougin/endorsecount.png)](https://coderwall.com/rougin) Another way to access CodeIgniter's instance @@ -12,9 +12,9 @@ Install ```Spark Plug``` via [Composer](https://getcomposer.org): # Why -The purpose of this library is to provide an access to the CodeIgniter's instance that is based on this [link](codeinphp.github.io/post/codeigniter-tip-accessing-codeigniter-instance-outside/). I just package this via [Composer](https://getcomposer.org/) for easy access. This may help you in developing libraries for CodeIgniter that does not go through ```index.php```, giving you more flexibility to your application. +The purpose of this library is to provide an access to the CodeIgniter's instance that is based on this [link](codeinphp.github.io/post/codeigniter-tip-accessing-codeigniter-instance-outside/). I just made a package of this via [Composer](https://getcomposer.org/) for easy access. This may help you in developing libraries for CodeIgniter that does not go through ```index.php```, giving you more flexibility to your desired library or console application. -I used this package as a dependency for [Combustor](https://github.com/rougin/combustor) and [Refinery](https://github.com/rougin/refinery). +Right now, I used this package as a dependency for [Combustor](https://github.com/rougin/combustor) and [Refinery](https://github.com/rougin/refinery) packages. # Getting Started @@ -25,4 +25,7 @@ use Rougin\SparkPlug\Instance; $instance = new Instance(); $codeigniter = $instance->get(); + +// You can now use its instance +$codeigniter->load->model('foo'); ``` \ No newline at end of file diff --git a/src/GetInstance.php b/src/GetInstance.php index b06b9ab..83b6479 100644 --- a/src/GetInstance.php +++ b/src/GetInstance.php @@ -1,5 +1,10 @@ __toString(), 'core' . $slash . 'CodeIgniter.php') !== FALSE) { - $basepath = str_replace('core' . $slash . 'CodeIgniter.php', '', $path->__toString()); + exit($message . PHP_EOL); + } + + // Search for the directory and defined it as the BASEPATH + $directory = new RecursiveDirectoryIterator( + getcwd(), + FilesystemIterator::SKIP_DOTS + ); + + $iterator = new RecursiveIteratorIterator( + $directory, + RecursiveIteratorIterator::SELF_FIRST + ); + + $slash = DIRECTORY_SEPARATOR; + + foreach ($iterator as $path) { + $core = 'core' . $slash . 'CodeIgniter.php'; + + if (strpos($path->__toString(), $core) !== FALSE) { + $basepath = str_replace($core, '', $path->__toString()); define('BASEPATH', $basepath); break; } } - /** - * Load the Common and Base Controller class - */ + if ( ! defined('BASEPATH')) { + $message = 'Oops! We can\'t find the "system" directory!'; + exit($message . PHP_EOL); + } + + // Load the Common and Base Controller class require BASEPATH . 'core/Common.php'; require BASEPATH . 'core/Controller.php'; @@ -67,52 +95,51 @@ public function __construct() require APPPATH . 'config/constants.php'; } - /** - * Important charset-related stuff - */ - + // Important charset-related stuff $charset = strtoupper(config_item('charset')); ini_set('default_charset', $charset); - if (extension_loaded('mbstring')) { - define('MB_ENABLED', TRUE); - // mbstring.internal_encoding is deprecated starting with PHP 5.6 - // and it's usage triggers E_DEPRECATED messages. - @ini_set('mbstring.internal_encoding', $charset); - // This is required for mb_convert_encoding() to strip invalid characters. - // That's utilized by CI_Utf8, but it's also done for consistency with iconv. - mb_substitute_character('none'); - } else { + if ( ! extension_loaded('mbstring')) { define('MB_ENABLED', FALSE); } + if ( ! defined('MB_ENABLED')) { + define('MB_ENABLED', TRUE); + } + + // mbstring.internal_encoding is deprecated starting with PHP 5.6 + // and it's usage triggers E_DEPRECATED messages. + @ini_set('mbstring.internal_encoding', $charset); + + // This is required for mb_convert_encoding() to strip invalid + // characters. That's utilized by CI_Utf8, but it's also done for + // consistency with iconv. + mb_substitute_character('none'); + + if ( ! extension_loaded('iconv')) { + define('ICONV_ENABLED', FALSE); + } + // There's an ICONV_IMPL constant, but the PHP manual says that using // iconv's predefined constants is "strongly discouraged". - if (extension_loaded('iconv')) { + if ( ! defined('ICONV_ENABLED')) { define('ICONV_ENABLED', TRUE); - // iconv.internal_encoding is deprecated starting with PHP 5.6 - // and it's usage triggers E_DEPRECATED messages. - @ini_set('iconv.internal_encoding', $charset); - } else { - define('ICONV_ENABLED', FALSE); } + // iconv.internal_encoding is deprecated starting with PHP 5.6 + // and it's usage triggers E_DEPRECATED messages. + @ini_set('iconv.internal_encoding', $charset); + if (is_php('5.6')) { ini_set('php.internal_encoding', $charset); } - /** - * Set global configurations - */ - + // Set global configurations $GLOBALS['CFG'] = & load_class('Config', 'core'); $GLOBALS['UNI'] = & load_class('Utf8', 'core'); $GLOBALS['SEC'] = & load_class('Security', 'core'); - /** - * Load the CodeIgniter's core classes - */ - + // Load the CodeIgniter's core classes load_class('Loader', 'core'); load_class('Router', 'core'); load_class('Input', 'core'); @@ -120,7 +147,7 @@ public function __construct() } /** - * Get the instance of CodeIgniter + * Gets an instance of CodeIgniter. * * @return CodeIgniter */