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