1 <?php
2 /**
3 * Callback class file
4 *
5 * @author Antonio Ramirez <amigo.cobos@gmail.com>
6 * @link http://2amigos.us
7 * @copyright 2013 2amigOS! Consultation Group LLC
8 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
9 *
10 * Credits to Phundament... Tobias, thanks for introducing me the wonders of composer
11 *
12 * @author Tobias Munk <schmunk@usrbin.de>
13 * @link http://www.phundament.com/
14 * @copyright Copyright © 2012 diemeisterei GmbH
15 * @license http://www.phundament.com/license
16 */
17 namespace Yiinitializr\Composer;
18
19 use Composer\Script\Event;
20 use Yiinitializr\Helpers\Initializer;
21 use Yiinitializr\Helpers\Config;
22 use Yiinitializr\Cli\Console;
23 /**
24 *
25 * Callback provides composer hooks
26 *
27 * Totally inspired by the ComposerCallback of Phundament3 and adapted for its use with Yiinitialzr
28 *
29 * This setup class triggers `./yiic migrate` at post-install and post-update.
30 * For a package the class triggers `./yiic <vendor/<packageName>-<action>` at post-package-install and
31 * post-package-update.
32 *
33 * You can also create new commands to be called within your boilerplate configuration.
34 *
35 * See composer manual (http://getcomposer.org/doc/articles/scripts.md)
36 *
37 * Usage example
38 * <pre>
39 * config.php
40 * 'params' => array(
41 * 'composer.callbacks' => array(
42 * 'post-update' => array('yiic', 'migrate'),
43 * 'post-install' => array('yiic', 'migrate'),
44 * 'yiisoft/yii-install' => array('yiic', 'webapp', realpath(dirname(__FILE__))),
45 * ),
46 * ))
47 *
48 * composer.json
49 * "scripts": {
50 * "pre-install-cmd": "Yiinitialzr\\Composer\\Callback::preInstall",
51 * "post-install-cmd": "Yiinitialzr\\Composer\\Callback::postInstall",
52 * "pre-update-cmd": "Yiinitialzr\\Composer\\Callback::preUpdate",
53 * "post-update-cmd": "Yiinitialzr\\Composer\\Callback::postUpdate",
54 * "post-package-install": [
55 * "Yiinitialzr\\Composer\\Callback::postPackageInstall"
56 * ],
57 * "post-package-update": [
58 * "Yiinitialzr\\Composer\\Callback::postPackageUpdate"
59 * ]
60 * }
61 * </pre>
62 *
63 * @author Antonio Ramirez <amigo.cobos@gmail.com>
64 * @package Yiinitializr.helpers
65 * @since 1.0
66 */
67 class Callback
68 {
69 /**
70 * Displays welcome message
71 * @static
72 * @param \Composer\Script\Event $event
73 */
74 public static function preInstall(Event $event)
75 {
76 Console::output("\n%BYiinitialzr 1.0.1%n\n");
77 Console::output("* download packages specified in composer.json");
78 Console::output("* configures your runtime folders");
79 Console::output("* triggers composer callbacks (yiic commands)\n");
80
81 if (Console::confirm("Start Installation?"))
82 self::runHook('pre-install');
83 else
84 exit("\n%RInstallation aborted%n.\n");
85 }
86
87 /**
88 * Executes a post-install callback
89 * @static
90 * @param \Composer\Script\Event $event
91 */
92 public static function postInstall(Event $event)
93 {
94 self::runHook('post-install');
95 Console::output("\n%GInstallation completed!%n\n");
96 }
97
98 /**
99 * Displays updating message
100 *
101 * @static
102 * @param \Composer\Script\Event $event
103 */
104 public static function preUpdate(Event $event)
105 {
106 Console::output("Updating your application to the latest available packages...");
107 self::runHook('pre-update');
108 }
109
110 /**
111 * Executes post-update message
112 *
113 * @static
114 * @param \Composer\Script\Event $event
115 */
116 public static function postUpdate(Event $event)
117 {
118 self::runHook('post-update');
119 Console::output("%GUpdate completed.%n");
120 }
121
122 /**
123 * Executes ./yiic <vendor/<packageName>-<action>
124 *
125 * @static
126 * @param \Composer\Script\Event $event
127 */
128 public static function postPackageInstall(Event $event)
129 {
130 $installedPackage = $event->getOperation()->getPackage();
131 $hookName = $installedPackage->getPrettyName() . '-install';
132 self::runHook($hookName);
133 }
134
135 /**
136 * Executes ./yiic <vendor/<packageName>-<action>
137 *
138 * @static
139 * @param \Composer\Script\Event $event
140 */
141 public static function postPackageUpdate(Event $event)
142 {
143 $installedPackage = $event->getOperation()->getTargetPackage();
144 $commandName = $installedPackage->getPrettyName() . '-update';
145 self::runHook($commandName);
146 }
147
148 /**
149 * Runs Yii command, if available (defined in config.php)
150 */
151 private static function runHook($name)
152 {
153 $app = self::getYiiApplication();
154 if ($app === null) return;
155
156 if (isset($app->params['composer.callbacks'][$name]))
157 {
158 $args = $app->params['composer.callbacks'][$name];
159 $app->commandRunner->addCommands(\Yii::getPathOfAlias('system.cli.commands'));
160 $app->commandRunner->run($args);
161 }
162 }
163
164 /**
165 * Creates console application, if Yii is available
166 */
167 private static function getYiiApplication()
168 {
169 if (!is_file(Config::value('yii.path') . '/yii.php'))
170 {
171 // nothing yet installed, return
172 return null;
173 }
174
175 require_once(Config::value('yii.path') . '/yii.php');
176
177 spl_autoload_register(array('YiiBase', 'autoload'));
178
179 if (\Yii::app() === null)
180 {
181
182 if (!Config::value('envlock'))
183 {
184 $env = Console::prompt('Please, enter your environment -ie. "dev | prod | stage": ', array('default' => 'dev'));
185 Initializer::buildEnvironmentFiles($env);
186 } else
187 {
188 Console::output("\n%Benv.lock%n file found. No environment request required.\n");
189 Console::output("Note: if you wish to re-do enviroment setting merging, please remove the %Benv.lock%n file " .
190 "from the Yiinitializr %Bconfig%n folder.");
191 }
192
193 Initializer::createRuntimeFolders();
194
195 Initializer::createRuntimeFolders('assets');
196
197 if (is_file(Config::value('yiinitializr.config.console')))
198 $app = \Yii::createConsoleApplication(Config::value('yiinitializr.config.console'));
199 else
200 throw new \Exception("'yiinitializr.config.console' setting not found");
201
202 } else
203 {
204 $app = \Yii::app();
205 }
206 return $app;
207 }
208
209 }