{"id":263678,"date":"2015-08-17T12:00:02","date_gmt":"2015-08-17T08:00:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=263678"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=263678","title":{"rendered":"Yii environment. \u041d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043d\u0444\u0438\u043d\u043e\u0432"},"content":{"rendered":"<p>             \u0425\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0432\u0430\u043c \u043f\u0440\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0439 \u043e\u043f\u044b\u0442, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u044f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u043d\u0430 \u0441\u0432\u043e\u0435\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u0435. \u041d\u0443\u0436\u043d\u0430 \u0431\u044b\u043b\u0430 \u0433\u0438\u0431\u043a\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0430 Environment-\u043e\u0432. \u041f\u043e\u0441\u043b\u0435 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u043e\u0432 \u044f \u0442\u0430\u043a\u0438 \u0434\u043e\u0431\u0438\u043b\u0441\u044f \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430. \u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u0441\u0440\u0430\u0437\u0443 \u043a \u0434\u0435\u043b\u0443.<\/p>\n<p>  \u0412 protected \u044f \u0441\u043e\u0437\u0434\u0430\u043b environment.php c \u0442\u0430\u043a\u0438\u043c\u0438 \u0432\u043e\u0442 2\u044f \u043a\u043b\u0430\u0441\u0441\u0430\u043c\u0438:<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">environment.php<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"php\">class Environment { \t\/** \t * \t *\/ \tconst PRODUCTION = 10; \t\/** \t * \t *\/ \tconst STAGING = 20; \t\/** \t * \t *\/ \tconst TESTING = 30; \t\/** \t * \t *\/ \tconst DEVELOPMENT = 40;  \t\/** \t * @var int \t *\/ \tprotected static $current;  \t\/** \t * @var \t *\/ \tprivate static $currentObj;  \t\/** \t * \t *\/ \tpublic static function instance() \t{ \t\tif(!self::$currentObj) \t\t{ \t\t\tself::$currentObj = new self(); \t\t} \t\treturn self::$currentObj; \t}  \t\/** \t * @param int $ENV \t * @return bool \t *\/ \tpublic function set($ENV = Environment::DEVELOPMENT) \t{ \t\tif(self::$current) \t\t{ \t\t\treturn false; \t\t}  \t\tif(isset($_GET['DEBUG'])) \t\t{ \t\t\t$this-&gt;setEnvironment($_GET['DEBUG']);  \t\t\t$_SESSION['systemEnvironment'] = $_GET['DEBUG'];  \t\t} \t\telseif(isset($_SESSION['systemEnvironment']) and $_SESSION['systemEnvironment'] !== 'off') \t\t{ \t\t\t$this-&gt;setEnvironment($_SESSION['systemEnvironment']);  \t\t} \t\telse \t\t{ \t\t\tself::$current = $ENV;  \t\t} \t\treturn true; \t}   \t\/** \t * @return int \t *\/ \tpublic static function getCurrent() \t{ \t\treturn self::$current; \t}   \t\/** \t * @param $level \t *\/ \tprivate function setEnvironment($level) \t{ \t\tswitch($level) \t\t{ \t\t\tcase 'PRODUCTION': \t\t\t\tself::$current = self::PRODUCTION; \t\t\t\tbreak; \t\t\tcase 'STAGING': \t\t\t\tself::$current = self::STAGING; \t\t\t\tbreak; \t\t\tcase 'TESTING': \t\t\t\tself::$current = self::TESTING; \t\t\t\tbreak; \t\t\tcase 'DEVELOPMENT': \t\t\t\tself::$current = self::DEVELOPMENT; \t\t\t\tbreak; \t\t\tcase 'off': \t\t\t\tif(isset($_SERVER['ENVIRONMENT'])) \t\t\t\t{ \t\t\t\t\tself::$current = constant('Environment::' . strtoupper($_SERVER['ENVIRONMENT'])); \t\t\t\t} \t\t\t\t$_SESSION['systemEnvironment'] = null; \t\t\t\tbreak;  \t\t} \t}  }   \/**  * Class EnvironmentUtils  *\/ class EnvironmentUtils extends Environment { \t\/** \t * @param $rootDirectory \t * @param $fileName \t * @return string \t *\/ \tpublic static function getConfigFile($rootDirectory, $fileName) \t{ \t\t$fileLink = $rootDirectory . '\/config\/'; \t\tswitch(parent::$current) \t\t{ \t\t\tcase Environment::DEVELOPMENT: \t\t\t\t$fileLink .= 'Development\/'; \t\t\t\tbreak; \t\t\tcase Environment::PRODUCTION: \t\t\t\t$fileLink .= 'Production\/'; \t\t\t\tbreak; \t\t\tcase Environment::STAGING: \t\t\t\t$fileLink .= 'Staging\/'; \t\t\t\tbreak; \t\t\tcase Environment::TESTING: \t\t\t\t$fileLink .= 'Testing\/'; \t\t\t\tbreak;  \t\t} \t\treturn $fileLink . $fileName; \t} } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u043b \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0444\u0430\u0439\u043b\u043e\u0432 \u0432 \u043f\u0430\u043f\u043a\u0435 config:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0412\u043e\u0442 \u0434\u0435\u0440\u0435\u0432\u043e \u0444\u0430\u0439\u043b\u043e\u0432.<\/b><\/p>\n<div class=\"spoiler_text\">\u251c\u2500\u2500 Base<br \/>  \u2502\u00a0\u00a0 \u251c\u2500\u2500 API<br \/>  \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u251c\u2500\u2500 Morpher.php<br \/>  \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u2514\u2500\u2500 XmlRpcClient.php<br \/>  \u2502\u00a0\u00a0 \u251c\u2500\u2500 Common<br \/>  \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u251c\u2500\u2500 Daemon.php<br \/>  \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u251c\u2500\u2500 Email.php<br \/>  \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u2514\u2500\u2500 Filtrator.php<br \/>  \u2502\u00a0\u00a0 \u251c\u2500\u2500 console.php<br \/>  \u2502\u00a0\u00a0 \u251c\u2500\u2500 DB<br \/>  \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u251c\u2500\u2500 1c.php<br \/>  \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u2514\u2500\u2500 BaseConnect.php<br \/>  \u2502\u00a0\u00a0 \u251c\u2500\u2500 main.php<br \/>  \u2502\u00a0\u00a0 \u251c\u2500\u2500 routes.php<br \/>  \u251c\u2500\u2500 Development<br \/>  \u2502\u00a0\u00a0 \u251c\u2500\u2500 cli.php<br \/>  \u2502\u00a0\u00a0 \u251c\u2500\u2500 DB<br \/>  \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u251c\u2500\u2500 1c.php<br \/>  \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u251c\u2500\u2500 BaseConnect.php<br \/>  \u2502\u00a0\u00a0 \u2514\u2500\u2500 Web.php<br \/>  \u251c\u2500\u2500 Production<br \/>  \u2502\u00a0\u00a0 \u251c\u2500\u2500 cli.php<br \/>  \u2502\u00a0\u00a0 \u251c\u2500\u2500 DB<br \/>  \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u251c\u2500\u2500 1c.php<br \/>  \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u251c\u2500\u2500 BaseConnect.php<br \/>  \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u251c\u2500\u2500 Sape.php<br \/>  \u2502\u00a0\u00a0 \u2514\u2500\u2500 Web.php<br \/>  \u251c\u2500\u2500 Staging<br \/>  \u2502\u00a0\u00a0 \u251c\u2500\u2500 cli.php<br \/>  \u2502\u00a0\u00a0 \u251c\u2500\u2500 DB<br \/>  \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u251c\u2500\u2500 1c.php<br \/>  \u2502\u00a0\u00a0 \u2502\u00a0\u00a0 \u251c\u2500\u2500 BaseConnect.php<br \/>  \u2502\u00a0\u00a0 \u2514\u2500\u2500 Web.php<br \/>  \u2514\u2500\u2500 Testing<br \/>   \u251c\u2500\u2500 cli.php<br \/>   \u251c\u2500\u2500 DB<br \/>   \u2502\u00a0\u00a0 \u251c\u2500\u2500 1c.php<br \/>   \u2502\u00a0\u00a0 \u2514\u2500\u2500 BaseConnect.php<br \/>   \u2514\u2500\u2500 Web.php  <\/div>\n<\/div>\n<p>  \u0412 Base\/Web.php \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043e\u043d\u0444\u0438\u0433. \u0418\u0437 \u0441\u0435\u0440\u0438\u0438:  <\/p>\n<pre><code class=\"php\">return [ \t'basePath'          =&gt; PROTECTED_PATH, \t'name'              =&gt; 'MyApp', \t'theme'             =&gt; 'classic', \t'language'          =&gt; 'ru', \t'defaultController' =&gt; 'user\/login', \t\/\/ preloading 'log' component \t\/\/ autoloading model and component classes \t'aliases'           =&gt; [ \t\t'bootstrap' =&gt; PROTECTED_PATH.'extensions\/bootstrap', \t\t\/\/ change this if necessary \t], \t'preload'           =&gt; [ \t\t'log', \t\t'bootstrap' \t], \t'import'            =&gt; [ \t\t'application.models.*',         ] <\/code><\/pre>\n<p>  \u0422\u043e\u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u0432 cli.php. \u0410 \u0432\u043e\u0442 \u0432 Production\/Web.php \u0447\u0442\u043e\u0431 \u043d\u0435 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u043a\u0430\u043a\u043e\u0439 \u0442\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0435\u0441\u043b\u0438 \u043c\u0435\u043d\u044f\u0435\u0448\u044c, \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"php\">return CMap::mergeArray(include(PROTECTED_PATH.'config\/Base\/main.php'), \t[ \t\t'components'=&gt; \t\t[ \t\t\t'db'         =&gt; include(dirname(__FILE__) . '\/DB\/BaseConnect.php'), \t\t\t'db1c'       =&gt; include(dirname(__FILE__) . '\/DB\/1c.php') \t\t] \t] ); <\/code><\/pre>\n<p>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u044f \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u044e \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0431\u0434, \u043d\u0443 \u0438\u043b\u0438 \u0442\u043e, \u0447\u0442\u043e \u043c\u043d\u0435 \u043d\u0443\u0436\u043d\u043e. \u0422\u0435\u043f\u0435\u0440\u044c \u0441\u0430\u043c\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435. \u041f\u043e\u0434\u0433\u0440\u0443\u0437\u043a\u0430. index.php.<\/p>\n<p>  P.S.: \u0423 \u043c\u0435\u043d\u044f \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u044f\u0434\u0440\u043e yii \u0442\u044f\u043d\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 composer:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">index.php<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"php\">define(&quot;PROTECTED_PATH&quot;,realpath(dirname(__FILE__)).'\/protected\/'); if(!file_exists(PROTECTED_PATH.'vendor\/autoload.php')) { \tdie('autoload.php not found. Composer update!'); }  require_once(PROTECTED_PATH.'vendor\/autoload.php'); require_once(PROTECTED_PATH.'environment.php');  if(isset($_SERVER['HTTP_ORIGIN'])) { \tif(in_array($_SERVER['HTTP_ORIGIN'],[....])) \t{ \t      header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']); \t      header('Access-Control-Allow-Methods: GET, POST, OPTIONS, DELETE, PUT'); \t      header('Access-Control-Max-Age: 1728000'); \t      header('Access-Control-Allow-Credentials: true'); \t      header(&quot;Access-Control-Allow-Headers: access-token, expiry, token-type, uid, client&quot;); \t      header(&quot;Access-Control-Expose-Headers: access-token, expiry, token-type, uid, client&quot;); \t}\t }   if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { \theader(&quot;HTTP\/1.0 204 No Content&quot;); \tdie(); }  Environment::instance()-&gt;set(Environment::DEVELOPMENT);  switch(Environment::getCurrent()) { \tcase Environment::DEVELOPMENT:  \t\tdefine('YII_DEBUG', false); \t\tdefine('YII_SKIP_AUTH', true); \t\tdefine('YII_KERNEL_LOG', true); \t\tdefine('DISPLAY_ERROR_TRACE', true); \t\tdefine('YII_TRACE_LEVEL', 0); \t\tdefine('MINIFY_INTERFACE', false);  \t\terror_reporting(E_ALL); \t\tini_set('display_errors','On'); \t\tbreak; \tcase Environment::PRODUCTION:  \t\tdefine('YII_DEBUG', false); \t\tdefine('YII_SKIP_AUTH', false); \t\tdefine('YII_KERNEL_LOG', false); \t\tdefine('DISPLAY_ERROR_TRACE', false); \t\tdefine('YII_TRACE_LEVEL', 3); \t\tdefine('MINIFY_INTERFACE', true);  \t\terror_reporting(0); \t\tini_set('display_errors','Off'); \t\tbreak; \tcase Environment::STAGING:  \t\tdefine('YII_DEBUG', true); \t\tdefine('YII_SKIP_AUTH', false); \t\tdefine('YII_KERNEL_LOG', true); \t\tdefine('DISPLAY_ERROR_TRACE', true); \t\tdefine('YII_TRACE_LEVEL', 0);  \t\terror_reporting(E_ALL); \t\tini_set('display_errors','On'); \t\tbreak; }  \/** @noinspection PhpUndefinedClassInspection *\/ Yii::$enableIncludePath = true; \/\/For init Yii kernel $config = EnvironmentUtils::getConfigFile(PROTECTED_PATH,'Web.php');  \/\/Other constants define('DATE_FORMAT', 'Y\/m\/d'); define('STATUS_NOACTIVE', 0); define('STATUS_ACTIVE', 1); date_default_timezone_set(&quot;Europe\/Kiev&quot;);  \/** @noinspection PhpUndefinedClassInspection *\/ Yii::createWebApplication($config)-&gt;run();  <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041f\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0438 \u0441\u0434\u0435\u043b\u0430\u043d\u043e \u0438 \u0432 cli.php. \u0421\u0438\u0441\u0442\u0435\u043c\u0430 \u0441\u0435\u0431\u044f \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u0437\u0430\u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u043e\u0432\u0430\u043b\u0430.             <\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/264877\/\"> http:\/\/habrahabr.ru\/post\/264877\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>             \u0425\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0432\u0430\u043c \u043f\u0440\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0439 \u043e\u043f\u044b\u0442, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u044f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u043d\u0430 \u0441\u0432\u043e\u0435\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0440\u0430\u0431\u043e\u0442\u0435. \u041d\u0443\u0436\u043d\u0430 \u0431\u044b\u043b\u0430 \u0433\u0438\u0431\u043a\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0430 Environment-\u043e\u0432. \u041f\u043e\u0441\u043b\u0435 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u043e\u0432 \u044f \u0442\u0430\u043a\u0438 \u0434\u043e\u0431\u0438\u043b\u0441\u044f \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430. \u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u0441\u0440\u0430\u0437\u0443 \u043a \u0434\u0435\u043b\u0443.<\/p>\n<p>  \u0412 protected \u044f \u0441\u043e\u0437\u0434\u0430\u043b environment.php c \u0442\u0430\u043a\u0438\u043c\u0438 \u0432\u043e\u0442 2\u044f \u043a\u043b\u0430\u0441\u0441\u0430\u043c\u0438:  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-263678","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/263678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=263678"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/263678\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=263678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=263678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=263678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}