Overview
  • Namespace
  • Class
  • Tree

Namespaces

  • PHP
  • Yiinitializr
    • Cli
    • Clio
    • Composer
    • Helpers

Classes

  • ArrayX
  • Config
  • Initializer
  1 <?php
  2 /**
  3  * Initializer class file.
  4  *
  5  * @author Antonio Ramirez <amigo.cobos@gmail.com>
  6  * @link http://www.ramirezcobos.com/
  7  * @link http://www.2amigos.us/
  8  * @copyright 2013 2amigOS! Consultation Group LLC
  9  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 10  */
 11 namespace Yiinitializr\Helpers;
 12 
 13 use Yiinitializr\Helpers\Config;
 14 use Yiinitializr\Helpers\ArrayX;
 15 use Yiinitializr\Cli\Console;
 16 
 17 /**
 18  * Initializer provides a set of useful functions to initialize a Yii Application development.
 19  *
 20  * @author Antonio Ramirez <amigo.cobos@gmail.com>
 21  * @package Yiinitializr.helpers
 22  * @since 1.0
 23  */
 24 class Initializer
 25 {
 26 
 27     /**
 28      * @param $root
 29      * @param string $configName
 30      * @param mixed $mergeWith
 31      * @return mixed
 32      * @throws Exception
 33      */
 34     public static function create($root, $configName = 'main', $mergeWith = array('common', 'env'))
 35     {
 36         if (($root = realpath($root)) === false)
 37             throw new Exception('could not initialize framework.');
 38 
 39         $config = self::config($configName, $mergeWith);
 40 
 41         if (php_sapi_name() !== 'cli') // aren't we in console?
 42             $app = \Yii::createWebApplication($config); // create web
 43         else
 44         {
 45             defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));
 46             $app = \Yii::createConsoleApplication($config);
 47             $app->commandRunner->addCommands($root . '/cli/commands');
 48             $env = @getenv('YII_CONSOLE_COMMANDS');
 49             if (!empty($env))
 50                 $app->commandRunner->addCommands($env);
 51         }
 52         //  return an app
 53         return $app;
 54     }
 55 
 56     /**
 57      * @param string $configName config name to load (main, test, etc)
 58      * @param null|string $mergeWith
 59      * @return array
 60      * @throws \Exception
 61      */
 62     public static function config($configName = 'main', $mergeWith = null)
 63     {
 64         $files = array($configName);
 65         $directory = Config::value('yiinitializr.app.directories.config.' . $configName);
 66         if (null === $directory)
 67             throw new \Exception("Unable to find 'yiinitializr.app.directories.config.'{$configName} on the settings.");
 68 
 69         if (null !== $mergeWith)
 70         {
 71             if (is_array($mergeWith))
 72             {
 73                 foreach($mergeWith as $file)
 74                     $files[] = $file;
 75             }
 76             else
 77                 $files[] = $mergeWith;
 78         }
 79 
 80         // do we have any other configuration files to merge with?
 81         $mergedSettingFiles = Config::value('yiinitializr.app.files.config.' . $configName);
 82         if (null !== $mergedSettingFiles)
 83         {
 84             if (is_array($mergeWith))
 85             {
 86                 foreach($mergeWith as $file)
 87                     $files[] = $file;
 88             }
 89             else
 90                 $files[] = $mergeWith;
 91         }
 92 
 93         $config = self::build($directory, $files);
 94 
 95         $params = isset($config['params'])
 96             ? $config['params']
 97             : array();
 98 
 99         self::setOptions($params);
100 
101         return $config;
102     }
103 
104     /**
105      * @param $directory
106      * @param $files  array of configuration files to merge
107      * @return array
108      */
109     public static function build($directory, $files)
110     {
111         $result = array();
112         if (!is_array($files))
113             $files = array($files);
114 
115         foreach ($files as $file)
116         {
117             $config = file_exists($file) && is_file($file)
118                 ? require($file)
119                 : (is_string($file) && file_exists($directory . '/' . $file . '.php')
120                     ? require($directory . '/' . $file . '.php')
121                     : array());
122 
123             if (is_array($config))
124                 $result = ArrayX::merge($result, $config);
125         }
126 
127         return $result;
128     }
129 
130     /**
131      * Set php and yii options - some based on the loaded config params
132      * @param array $params The config params being used for the app
133      */
134     protected static function setOptions(array $params)
135     {
136         // yii config
137         defined('YII_DEBUG') or define('YII_DEBUG', isset($params['yii.debug']) ? $params['yii.debug'] : false);
138         defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', isset($params['yii.traceLevel']) ? $params['yii.traceLevel'] : 0);
139         defined('YII_ENABLE_ERROR_HANDLER') or define('YII_ENABLE_ERROR_HANDLER', isset($params['yii.handleErrors']) ? $params['yii.handleErrors'] : true);
140         defined('YII_ENABLE_EXCEPTION_HANDLER') or define('YII_ENABLE_EXCEPTION_HANDLER', YII_ENABLE_ERROR_HANDLER);
141 
142         // php config
143         error_reporting(-1);
144         if(isset($params['php.defaultCharset']))
145             ini_set('default_charset', $params['php.defaultCharset']);
146         if(isset($params['php.timezone']))
147             date_default_timezone_set($params['php.timezone']);
148 
149         date_default_timezone_set($params['php.timezone']);
150 
151         if(!class_exists('YiiBase'))
152             require(Config::value('yii.path').'/yii.php');
153     }
154 
155     /**
156      *  Helper function to build environment files
157      * @param $environment
158      * @throws \Exception
159      */
160     public static function buildEnvironmentFiles($environment = 'dev')
161     {
162         self::output("\n%gBuilding environment files.%n");
163 
164         umask(0);
165         $directories = Config::value('yiinitializr.app.directories.config');
166         if (null === $directories)
167             throw new \Exception("Unable to find 'yiinitializr.app.directories.config' on the settings.");
168 
169         if (!is_array($directories))
170             $directories = array($directories);
171 
172         $environment = strlen($environment)
173             ? $environment
174             : 'dev';
175 
176         foreach ($directories as $directory)
177         {
178             if (file_exists($directory))
179             {
180                 $environment_directory = $directory . '/env';
181                 if (!file_exists($environment_directory))
182                 {
183                     mkdir($environment_directory);
184 
185                     self::output("Your environment directory has been created: %r{$environment_directory}%n.\n");
186                 }
187 
188                 $environment_file = $environment_directory . '/' . $environment . '.php';
189 
190                 if (!file_exists($environment_file))
191                 {
192                     file_put_contents($environment_file, "<?php\n/**\n * {$environment}.php\n */\n\nreturn array(\n);");
193                     @chmod($environment_file, 0644);
194 
195                     self::output("%gEnvironment configuration file has been created: %r{$environment_file}%n.\n");
196                 }
197                 if (!file_exists($directory . '/env.php'))
198                 {
199                     @copy($environment_file, $directory . '/env.php');
200 
201                     self::output("Your environment configuration file has been created on {$directory}.\n");
202                 } else
203                     self::output("'{$directory}/env.php' \n%pfile already exists. No action has been executed.%n");
204             }
205         }
206         Config::createEnvironmentLockFile($environment);
207         self::output("%gEnvironment files creation process finished.%n\n");
208     }
209 
210     /**
211      * @param string $name the name of the runtime folder,
212      * @throws \Exception
213      */
214     public static function createRuntimeFolders($name = 'runtime')
215     {
216         self::output("\n%gBuilding runtime '{$name}' folders.%n");
217         umask(0);
218         $directories = Config::value('yiinitializr.app.directories.' . $name);
219 
220         if (null === $directories)
221             throw new \Exception("Unable to find 'yiinitializr.app.directories.{$name}' on the settings.");
222 
223         if (!is_array($directories))
224             $directories = array($directories);
225 
226         foreach ($directories as $directory)
227         {
228             $runtime = $directory . '/' . $name;
229             if (!file_exists($runtime))
230             {
231                 @mkdir($runtime, 02777);
232                 self::output("Your {$name} folder has been created on {$directory}.");
233             } else
234                 self::output("'{$name}'\n%pfolder already exists. No action has been executed.%n");
235         }
236         self::output("\n%gRuntime '{$name}'' folders creation process finished.%n");
237     }
238 
239     /**
240      * Outputs text only to console
241      * @param $message
242      */
243     protected static function output($message)
244     {
245         if (php_sapi_name() === 'cli')
246             Console::output($message);
247     }
248 }
API documentation generated by ApiGen 2.8.0