Last active
August 29, 2015 14:02
-
-
Save emamut/a0bf4a88eefd826989bb to your computer and use it in GitHub Desktop.
Codeigniter environment setter - CI Library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class Environment_setter | |
{ | |
protected $environment_file; | |
protected $previous_environment; | |
public function __construct() | |
{ | |
$this->environment_file = dirname(__FILE__)."/../../index.php"; | |
} | |
public function set_development_environment() | |
{ | |
$this->previous_environment = $this->get_current_environment(); | |
$file_contents = file_get_contents($this->environment_file); | |
$new_content = str_replace("define('ENVIRONMENT', '$this->previous_environment');", "define('ENVIRONMENT', 'development');", $file_contents); | |
file_put_contents($this->environment_file, $new_content); | |
} | |
public function set_production_environment() | |
{ | |
$this->previous_environment = $this->get_current_environment(); | |
$file_contents = file_get_contents($this->environment_file); | |
$new_content = str_replace("define('ENVIRONMENT', '$this->previous_environment');", "define('ENVIRONMENT', 'production');", $file_contents); | |
file_put_contents($this->environment_file, $new_content); | |
} | |
public function set_testing_environment() | |
{ | |
$this->previous_environment = $this->get_current_environment(); | |
$file_contents = file_get_contents($this->environment_file); | |
$new_content = str_replace("define('ENVIRONMENT', '$this->previous_environment');", "define('ENVIRONMENT', 'testing');", $file_contents); | |
file_put_contents($this->environment_file, $new_content); | |
} | |
public function set_previous_environment() | |
{ | |
$file_contents = file_get_contents($this->environment_file); | |
$new_content = str_replace("define('ENVIRONMENT', 'development');", "define('ENVIRONMENT', '$this->previous_environment');", $file_contents); | |
file_put_contents($this->environment_file, $new_content); | |
$file_contents = file_get_contents($this->environment_file); | |
$new_content = str_replace("define('ENVIRONMENT', 'production');", "define('ENVIRONMENT', '$this->previous_environment');", $file_contents); | |
file_put_contents($this->environment_file, $new_content); | |
$file_contents = file_get_contents($this->environment_file); | |
$new_content = str_replace("define('ENVIRONMENT', 'testing');", "define('ENVIRONMENT', '$this->previous_environment');", $file_contents); | |
file_put_contents($this->environment_file, $new_content); | |
} | |
protected function get_current_environment() | |
{ | |
if(strpos(file_get_contents($this->environment_file), "define('ENVIRONMENT', 'production');")) | |
return 'production'; | |
if(strpos(file_get_contents($this->environment_file), "define('ENVIRONMENT', 'development');")) | |
return 'development'; | |
if(strpos(file_get_contents($this->environment_file), "define('ENVIRONMENT', 'testing');")) | |
return 'testing'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment