forked from appserver-io/appserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.php
executable file
·144 lines (119 loc) · 5.57 KB
/
server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/opt/appserver/bin/php
<?php
/**
* server.php
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* PHP version 5
*
* @category Server
* @package Appserver
* @author Tim Wagner <[email protected]>
* @copyright 2014 TechDivision GmbH <[email protected]>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link http://github.com/appserver-io/appserver
* @link http://www.appserver.io
*/
namespace AppserverIo\Appserver\Core;
use AppserverIo\Storage\GenericStackable;
use AppserverIo\Appserver\Naming\NamingDirectory;
use AppserverIo\Appserver\Core\Utilities\DirectoryKeys;
declare (ticks = 1);
error_reporting(~E_NOTICE);
set_time_limit(0);
// set the session timeout to unlimited
ini_set('session.gc_maxlifetime', 0);
ini_set('zend.enable_gc', 0);
ini_set('max_execution_time', 0);
// query whether the sockets extension is available or not
if (extension_loaded('sockets') === false) {
throw new \Exception('Extension sockets has to be loaded');
}
// query whether the pthreads extension is available or not
if (extension_loaded('pthreads') === false) {
throw new \Exception('Extension pthreads (https://github.com/appserver-io-php/pthreads) > 2.0.10 has to be loaded');
}
// query whether the appserver extension is available or not
if (extension_loaded('appserver') === false) {
throw new \Exception('Extension appserver (https://github.com/appserver-io-php/php-ext-appserver) > 1.0.1 has to be loaded');
}
// define a all constants appserver base directory
define('APPSERVER_BP', __DIR__);
// bootstrap the application
require __DIR__ . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'scripts' . DIRECTORY_SEPARATOR . 'bootstrap.php';
// define the available options
$setup = 's';
$watch = 'w'; // compatibility mode for old server version
$config = 'c';
$bootstrap = 'b';
$configTest = 't';
// check if server.php has been started with -s, -w, -c or -b option
$arguments = getopt("$setup:$configTest::$watch::$config::$bootstrap::");
// query whether a configuration file has been specified or not
if (array_key_exists($config, $arguments) && file_exists($arguments[$config])) {
// set the file passed as parameter
$filename = $arguments[$config];
} elseif (file_exists(sprintf('%s/etc/appserver/appserver.xml', APPSERVER_BP))) {
// try to load the default configuration file
$filename = sprintf('%s/etc/appserver/appserver.xml', APPSERVER_BP);
} else {
// throw an exception if we don't have a configuration file
throw new \Exception('Can\'t find a configuration file');
}
// query whether a bootstrap file has been specified or not
if (array_key_exists($bootstrap, $arguments) && file_exists($arguments[$bootstrap])) {
// set the file passed as parameter
$bootstrapFilename = $arguments[$bootstrap];
} elseif ((array_key_exists($setup, $arguments) || array_key_exists($configTest, $arguments)) && file_exists(sprintf('%s/etc/appserver/conf.d/bootstrap-commands.xml', APPSERVER_BP))) {
// set the default commands boostrap file
$bootstrapFilename = sprintf('%s/etc/appserver/conf.d/bootstrap-commands.xml', APPSERVER_BP);
} elseif (array_key_exists($watch, $arguments) && file_exists(sprintf('%s/etc/appserver/conf.d/bootstrap-watcher.xml', APPSERVER_BP))) {
// set the default watcher boostrap file
$bootstrapFilename = sprintf('%s/etc/appserver/conf.d/bootstrap-watcher.xml', APPSERVER_BP);
} elseif (file_exists(sprintf('%s/etc/appserver/conf.d/bootstrap.xml', APPSERVER_BP))) {
// try to load the default bootstrap file
$bootstrapFilename = sprintf('%s/etc/appserver/conf.d/bootstrap.xml', APPSERVER_BP);
} else {
// throw an exception if we don't have a bootstrap file
throw new \Exception('Can\'t find a bootstrap file');
}
// create and initialize the naming directory
$namingDirectory = new NamingDirectory();
$namingDirectory->setScheme('php');
// create a directory for the services
$namingDirectory->createSubdirectory('php:env');
$namingDirectory->createSubdirectory('php:env/args');
$namingDirectory->createSubdirectory('php:global');
$namingDirectory->createSubdirectory('php:global/log');
$namingDirectory->createSubdirectory('php:services');
// create the default subdirectories
foreach (array_keys(ApplicationServer::$runlevels) as $runlevel) {
$namingDirectory->createSubdirectory(sprintf('php:services/%s', $runlevel));
}
// bind the command line arguments to the naming directory
foreach ($arguments as $name => $value) {
$namingDirectory->bind(sprintf('php:env/args/%s', $name), empty($value) ? true : $value);
}
// bind the current user to the naming directory
$namingDirectory->bind('php:env/currentUser', isset($_SERVER['SUDO_USER']) ? $_SERVER['SUDO_USER'] : get_current_user());
// bind the path to the default configuration and bootstrap filenames
$namingDirectory->bind('php:env/configurationFilename', DirectoryKeys::realpath($filename));
$namingDirectory->bind('php:env/bootstrapConfigurationFilename', DirectoryKeys::realpath($bootstrapFilename));
// add the storeage containers for the runlevels
$runlevels = new GenericStackable();
foreach (ApplicationServer::$runlevels as $runlevel) {
$runlevels[$runlevel] = new GenericStackable();
}
// initialize and start the application server
$applicationServer = ApplicationServer::singleton($namingDirectory, $runlevels);
// we've to wait for shutdown
while ($applicationServer->keepRunning()) {
sleep(1);
}
// wait until all threads have been stopped
$applicationServer->join();