Skip to content

Commit 9e6d598

Browse files
author
epriestley
committedDec 25, 2012
Consolidate environmental initialization
Summary: We have a bunch of code duplication now between __init_script__.php and webroot/index.php. Consoldiate these methods and move them into PhabricatorEnv. Merge PhabricatorRequestOverseer into PhabricatorStartup. Test Plan: Loaded page, ran script. Wiped PHABRICATOR_ENV; loaded page, ran script; got errors. Reviewers: btrahan, vrana Reviewed By: btrahan CC: aran Maniphest Tasks: T2223 Differential Revision: https://secure.phabricator.com/D4283
1 parent ed58f6c commit 9e6d598

File tree

6 files changed

+246
-257
lines changed

6 files changed

+246
-257
lines changed
 

‎scripts/__init_script__.php

+20-68
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,24 @@
11
<?php
22

3-
error_reporting(E_ALL | E_STRICT);
4-
ini_set('display_errors', 1);
5-
6-
$include_path = ini_get('include_path');
7-
ini_set(
8-
'include_path',
9-
$include_path.PATH_SEPARATOR.dirname(__FILE__).'/../../');
10-
@include_once 'libphutil/scripts/__init_script__.php';
11-
if (!@constant('__LIBPHUTIL__')) {
12-
echo "ERROR: Unable to load libphutil. Update your PHP 'include_path' to ".
13-
"include the parent directory of libphutil/.\n";
14-
exit(1);
15-
}
16-
17-
phutil_load_library(dirname(__FILE__).'/../src/');
18-
19-
// NOTE: This is dangerous in general, but we know we're in a script context and
20-
// are not vulnerable to CSRF.
21-
AphrontWriteGuard::allowDangerousUnguardedWrites(true);
22-
23-
require_once dirname(dirname(__FILE__)).'/conf/__init_conf__.php';
24-
25-
$env = isset($_SERVER['PHABRICATOR_ENV'])
26-
? $_SERVER['PHABRICATOR_ENV']
27-
: getenv('PHABRICATOR_ENV');
28-
if (!$env) {
29-
echo phutil_console_wrap(
30-
phutil_console_format(
31-
"**ERROR**: PHABRICATOR_ENV Not Set\n\n".
32-
"Define the __PHABRICATOR_ENV__ environment variable before running ".
33-
"this script. You can do it on the command line like this:\n\n".
34-
" $ PHABRICATOR_ENV=__custom/myconfig__ %s ...\n\n".
35-
"Replace __custom/myconfig__ with the path to your configuration file. ".
36-
"For more information, see the 'Configuration Guide' in the ".
37-
"Phabricator documentation.\n\n",
38-
$argv[0]));
39-
exit(1);
40-
}
41-
42-
$conf = phabricator_read_config_file($env);
43-
$conf['phabricator.env'] = $env;
44-
45-
PhabricatorEnv::setEnvConfig($conf);
46-
47-
phutil_load_library('arcanist/src');
48-
49-
foreach (PhabricatorEnv::getEnvConfig('load-libraries') as $library) {
50-
phutil_load_library($library);
51-
}
52-
53-
PhutilErrorHandler::initialize();
54-
PhabricatorEventEngine::initialize();
55-
56-
$tz = PhabricatorEnv::getEnvConfig('phabricator.timezone');
57-
if ($tz) {
58-
date_default_timezone_set($tz);
3+
function init_phabricator_script() {
4+
error_reporting(E_ALL | E_STRICT);
5+
ini_set('display_errors', 1);
6+
7+
$include_path = ini_get('include_path');
8+
ini_set(
9+
'include_path',
10+
$include_path.PATH_SEPARATOR.dirname(__FILE__).'/../../');
11+
@include_once 'libphutil/scripts/__init_script__.php';
12+
if (!@constant('__LIBPHUTIL__')) {
13+
echo "ERROR: Unable to load libphutil. Update your PHP 'include_path' to ".
14+
"include the parent directory of libphutil/.\n";
15+
exit(1);
16+
}
17+
18+
phutil_load_library('arcanist/src');
19+
phutil_load_library(dirname(__FILE__).'/../src/');
20+
21+
PhabricatorEnv::initializeScriptEnvironment();
5922
}
6023

61-
$translation = PhabricatorEnv::newObjectFromConfig('translation.provider');
62-
PhutilTranslator::getInstance()
63-
->setLanguage($translation->getLanguage())
64-
->addTranslations($translation->getTranslations());
65-
66-
// Append any paths to $PATH if we need to.
67-
$paths = PhabricatorEnv::getEnvConfig('environment.append-paths');
68-
if (!empty($paths)) {
69-
$current_env_path = getenv('PATH');
70-
$new_env_paths = implode(PATH_SEPARATOR, $paths);
71-
putenv('PATH='.$current_env_path.PATH_SEPARATOR.$new_env_paths);
72-
}
24+
init_phabricator_script();

‎src/__phutil_library_map__.php

-1
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,6 @@
10801080
'PhabricatorRepositorySymbol' => 'applications/repository/storage/PhabricatorRepositorySymbol.php',
10811081
'PhabricatorRepositoryTestCase' => 'applications/repository/storage/__tests__/PhabricatorRepositoryTestCase.php',
10821082
'PhabricatorRepositoryType' => 'applications/repository/constants/PhabricatorRepositoryType.php',
1083-
'PhabricatorRequestOverseer' => 'infrastructure/PhabricatorRequestOverseer.php',
10841083
'PhabricatorS3FileStorageEngine' => 'applications/files/engine/PhabricatorS3FileStorageEngine.php',
10851084
'PhabricatorSQLPatchList' => 'infrastructure/storage/patch/PhabricatorSQLPatchList.php',
10861085
'PhabricatorSSHWorkflow' => 'infrastructure/ssh/PhabricatorSSHWorkflow.php',

‎src/infrastructure/PhabricatorEnv.php

+96
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,102 @@ final class PhabricatorEnv {
5353
private static $env;
5454
private static $stack = array();
5555

56+
/**
57+
* @phutil-external-symbol class PhabricatorStartup
58+
*/
59+
public static function initializeWebEnvironment() {
60+
$env = self::getSelectedEnvironmentName();
61+
if (!$env) {
62+
PhabricatorStartup::didFatal(
63+
"The 'PHABRICATOR_ENV' environmental variable is not defined. Modify ".
64+
"your httpd.conf to include 'SetEnv PHABRICATOR_ENV <env>', where ".
65+
"'<env>' is one of 'development', 'production', or a custom ".
66+
"environment.");
67+
}
68+
69+
self::initializeCommonEnvironment();
70+
}
71+
72+
public static function initializeScriptEnvironment() {
73+
$env = self::getSelectedEnvironmentName();
74+
if (!$env) {
75+
echo phutil_console_wrap(
76+
phutil_console_format(
77+
"**ERROR**: PHABRICATOR_ENV Not Set\n\n".
78+
"Define the __PHABRICATOR_ENV__ environment variable before ".
79+
"running this script. You can do it on the command line like ".
80+
"this:\n\n".
81+
" $ PHABRICATOR_ENV=__custom/myconfig__ %s ...\n\n".
82+
"Replace __custom/myconfig__ with the path to your configuration ".
83+
"file. For more information, see the 'Configuration Guide' in the ".
84+
"Phabricator documentation.\n\n",
85+
$GLOBALS['argv'][0]));
86+
exit(1);
87+
}
88+
89+
self::initializeCommonEnvironment();
90+
91+
// NOTE: This is dangerous in general, but we know we're in a script context
92+
// and are not vulnerable to CSRF.
93+
AphrontWriteGuard::allowDangerousUnguardedWrites(true);
94+
}
95+
96+
/**
97+
* @phutil-external-symbol function phabricator_read_config_file
98+
*/
99+
private static function initializeCommonEnvironment() {
100+
$env = self::getSelectedEnvironmentName();
101+
102+
$root = dirname(phutil_get_library_root('phabricator'));
103+
require_once $root.'/conf/__init_conf__.php';
104+
$conf = phabricator_read_config_file($env);
105+
$conf['phabricator.env'] = $env;
106+
107+
PhabricatorEnv::setEnvConfig($conf);
108+
109+
PhutilErrorHandler::initialize();
110+
111+
$tz = PhabricatorEnv::getEnvConfig('phabricator.timezone');
112+
if ($tz) {
113+
date_default_timezone_set($tz);
114+
}
115+
116+
// Append any paths to $PATH if we need to.
117+
$paths = PhabricatorEnv::getEnvConfig('environment.append-paths');
118+
if (!empty($paths)) {
119+
$current_env_path = getenv('PATH');
120+
$new_env_paths = implode(PATH_SEPARATOR, $paths);
121+
putenv('PATH='.$current_env_path.PATH_SEPARATOR.$new_env_paths);
122+
}
123+
124+
foreach (PhabricatorEnv::getEnvConfig('load-libraries') as $library) {
125+
phutil_load_library($library);
126+
}
127+
128+
PhabricatorEventEngine::initialize();
129+
130+
$translation = PhabricatorEnv::newObjectFromConfig('translation.provider');
131+
PhutilTranslator::getInstance()
132+
->setLanguage($translation->getLanguage())
133+
->addTranslations($translation->getTranslations());
134+
}
135+
136+
public static function getSelectedEnvironmentName() {
137+
$env_var = 'PHABRICATOR_ENV';
138+
139+
$env = idx($_SERVER, $env_var);
140+
141+
if (!$env) {
142+
$env = getenv($env_var);
143+
}
144+
145+
if (!$env) {
146+
$env = idx($_ENV, $env_var);
147+
}
148+
149+
return $env;
150+
}
151+
56152

57153
/* -( Reading Configuration )---------------------------------------------- */
58154

‎src/infrastructure/PhabricatorRequestOverseer.php

-98
This file was deleted.

0 commit comments

Comments
 (0)
Failed to load comments.