Skip to content

Commit

Permalink
Updated functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Sep 16, 2015
1 parent 0e36a13 commit 252a330
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 52 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand All @@ -25,4 +25,7 @@ use Rougin\SparkPlug\Instance;

$instance = new Instance();
$codeigniter = $instance->get();

// You can now use its instance
$codeigniter->load->model('foo');
```
5 changes: 5 additions & 0 deletions src/GetInstance.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

/**
* Gets an instance of CodeIgniter.
*
* @return CI_Controller
*/
function &get_instance()
{
return \CI_Controller::get_instance();
Expand Down
125 changes: 76 additions & 49 deletions src/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Rougin\SparkPlug;

use CI_Controller;
use RecursiveDirectoryIterator;
use FilesystemIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

/**
Expand All @@ -18,42 +18,70 @@
class Instance
{
/**
* Set some definitions and load required classes
* Defines constants and load required classes
*
* @return void
*/
public function __construct()
{
/**
* Define the APPPATH, VENDOR, and BASEPATH paths
*/

// Define the APPPATH, VENDOR, and BASEPATH paths
if ( ! defined('VENDOR')) {
define('VENDOR', realpath('vendor') . '/');
define('VENDOR', realpath('vendor') . '/');
}

define('APPPATH', realpath('application') . '/');
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
define('VIEWPATH', APPPATH . '/views/');
if ( ! defined('APPPATH')) {
define('APPPATH', realpath('application') . '/');
}

/**
* Search for the directory and defined it as the BASEPATH
*/
if ( ! defined('ENVIRONMENT')) {
$environment = isset($_SERVER['CI_ENV'])
? $_SERVER['CI_ENV']
: 'development';

define('ENVIRONMENT', $environment);
}

if ( ! defined('VIEWPATH')) {
define('VIEWPATH', APPPATH . '/views/');
}

$directory = new RecursiveDirectoryIterator(getcwd(), FilesystemIterator::SKIP_DOTS);
$slash = (strpos(PHP_OS, 'WIN') !== FALSE) ? '\\' : '/';
if ( ! file_exists(APPPATH)) {
$message = 'Oops! We can\'t find the "application" directory!';

foreach (new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST) as $path) {
if (strpos($path->__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';

Expand All @@ -67,60 +95,59 @@ 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');
load_class('Lang', 'core');
}

/**
* Get the instance of CodeIgniter
* Gets an instance of CodeIgniter.
*
* @return CodeIgniter
*/
Expand Down

0 comments on commit 252a330

Please sign in to comment.