PageRenderTime 58ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/SystemInformation/src/info.php

https://github.com/Yannix/zetacomponents
PHP | 402 lines | 198 code | 21 blank | 183 comment | 29 complexity | 7ff0ab6633a86b231e47d91592ed1f58 MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the ezcSystemInfo class.
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @package SystemInformation
  23. * @version //autogen//
  24. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  25. */
  26. /**
  27. * Provides access to common system variables.
  28. *
  29. * Variables not available from PHP directly are fetched using readers
  30. * specific for each supported system. Corresponding reader is automatically
  31. * detected, attached and forced to scan system info during initialization.
  32. * An exception is thrown if the reader can't scan the system info.
  33. *
  34. * Available readers are:
  35. * - {@link ezcSystemInfoLinuxReader} reader
  36. * - {@link ezcSystemInfoMacReader} reader
  37. * - {@link ezcSystemInfoFreeBsdReader} reader
  38. * - {@link ezcSystemInfoWindowsReader} reader
  39. *
  40. * Readers for other systems can be added by
  41. * implementing the {@link ezcSystemInfoReader} interface.
  42. *
  43. * The ezcSystemInfo class has the following properties:
  44. *
  45. * Reader independent, these properties are available even if system reader was not initialized.
  46. * @property string $osType
  47. * OS type (e.g 'unix') or null.
  48. * @property string $osName
  49. * OS name (e.g 'Linux') or null.
  50. * @property string $fileSystemType
  51. * Filesystem type (e.g 'linux') or null.
  52. * @property string $lineSeparator
  53. * Symbols which is used for line separators on the current OS.
  54. * @property string $backupFileName
  55. * Backup filename for this platform, '.bak' for win32 and '~' for unix and mac.
  56. * @property array $phpVersion
  57. * Array with PHP version (e.g. array(5,1,1) ).
  58. * @property ezcSystemInfoAccelerator $phpAccelerator
  59. * Structure with PHP accelerator info or null.
  60. * {@link ezcSystemInfoAccelerator}.
  61. * @property bool $isShellExecution
  62. * The flag which indicates if the script was executed over the web or the shell/command line.
  63. *
  64. * Reader dependent, these properties are not available if reader was not initialized and didn't scan OS:
  65. * @property integer $cpuCount
  66. * Number of CPUs in system or null.
  67. * @property string $cpuType
  68. * CPU type string (e.g 'AMD Sempron(tm) Processor 3000+') or null.
  69. * @property float $cpuSpeed
  70. * CPU speed as float (e.g 1808.743) in Mhz or null.
  71. * @property integer $memorySize
  72. * Memory Size in bytes int (e.g. 528424960) or null.
  73. *
  74. * Example:
  75. * <code>
  76. * $info = ezcSystemInfo::getInstance();
  77. * echo 'Processors: ', $info->cpuCount, "\n";
  78. * echo 'CPU Type: ', $info->cpuType, "\n";
  79. * echo 'CPU Speed: ', $info->cpuSpeed, "\n";
  80. * </code>
  81. *
  82. * @package SystemInformation
  83. * @version //autogentag//
  84. * @mainclass
  85. */
  86. class ezcSystemInfo
  87. {
  88. /**
  89. * Instance of the singleton ezcSystemInfo object.
  90. *
  91. * Use the getInstance() method to retrieve the instance.
  92. *
  93. * @var ezcSystemInfo
  94. */
  95. private static $instance = null;
  96. /**
  97. * Contains object that provide info about the underlying OS.
  98. *
  99. * @var ezcSystemInfoReader
  100. */
  101. private $systemInfoReader = null;
  102. /**
  103. * Contains string with the type of the underlying OS
  104. * or empty string if OS can't be detected.
  105. *
  106. * @var string
  107. */
  108. private $osType = null;
  109. /**
  110. * Contains string with the name of the underlying OS
  111. * or empty string if OS can't be detected.
  112. *
  113. * @var string
  114. */
  115. private $osName = null;
  116. /**
  117. * Contains string with the filesystem type of the underlying OS
  118. * or empty string if OS can't be detected.
  119. *
  120. * @var string
  121. */
  122. private $fileSystemType = null;
  123. /**
  124. * Contains string with the line separator of the underlying OS
  125. * or empty string if OS can't be detected.
  126. *
  127. * @var string
  128. */
  129. private $lineSeparator = null;
  130. /**
  131. * Contains string with the backup file name of the underlying OS
  132. * or empty string if OS can't be detected.
  133. *
  134. * @var string
  135. */
  136. private $backupFileName = null;
  137. /**
  138. * Returns the single instance of the ezcSystemInfo class.
  139. *
  140. * @throws ezcSystemInfoReaderCantScanOSException
  141. * If system variables can't be received from OS.
  142. * @return ezcSystemInfo
  143. */
  144. public static function getInstance()
  145. {
  146. if ( is_null( self::$instance ) )
  147. {
  148. self::$instance = new self();
  149. }
  150. return self::$instance;
  151. }
  152. /**
  153. * Constructs ezcSystemInfo object, inits it with corresponding underlying OS data.
  154. *
  155. * @throws ezcSystemInfoReaderCantScanOSException
  156. * If system variables can't be received from OS.
  157. */
  158. private function __construct()
  159. {
  160. $this->init();
  161. }
  162. /**
  163. * Detects underlying system and sets system properties.
  164. *
  165. * @throws ezcSystemInfoReaderCantScanOSException
  166. * If system variables can't be received from OS.
  167. */
  168. private function init()
  169. {
  170. $this->setSystemInfoReader();
  171. }
  172. /**
  173. * Sets the systemInfoReader depending on the OS and fills in the system
  174. * information internally.
  175. *
  176. * Returns true if it was able to set appropriate systemInfoReader
  177. * or false if failed.
  178. *
  179. * @throws ezcSystemInfoReaderCantScanOSException
  180. * If system variables can't be received from OS.
  181. * @return bool
  182. */
  183. private function setSystemInfoReader()
  184. {
  185. // Determine OS
  186. $uname = php_uname( 's' );
  187. if ( substr( $uname, 0, 7 ) == 'Windows' )
  188. {
  189. $this->systemInfoReader = new ezcSystemInfoWindowsReader( $uname );
  190. $this->osType = 'win32';
  191. $this->osName = 'Windows';
  192. $this->fileSystemType = 'win32';
  193. $this->lineSeparator= "\r\n";
  194. $this->backupFileName = '.bak';
  195. }
  196. else if ( substr( $uname, 0, 6 ) == 'Darwin' )
  197. {
  198. $this->systemInfoReader = new ezcSystemInfoMacReader();
  199. $this->osType = 'mac';
  200. $this->osName = 'Mac OS X';
  201. $this->fileSystemType = 'unix';
  202. $this->lineSeparator= "\n";
  203. $this->backupFileName = '~';
  204. }
  205. else
  206. {
  207. $this->osType = 'unix';
  208. if ( strtolower( $uname ) == 'linux' )
  209. {
  210. $this->systemInfoReader = new ezcSystemInfoLinuxReader();
  211. $this->osName = 'Linux';
  212. $this->fileSystemType = 'unix';
  213. $this->lineSeparator= "\n";
  214. $this->backupFileName = '~';
  215. }
  216. else if ( strtolower( substr( $uname, 0, 7 ) ) == 'freebsd' )
  217. {
  218. $this->systemInfoReader = new ezcSystemInfoFreeBsdReader();
  219. $this->osName = 'FreeBSD';
  220. $this->fileSystemType = 'unix';
  221. $this->lineSeparator= "\n";
  222. $this->backupFileName = '~';
  223. }
  224. else
  225. {
  226. $this->systemInfoReader = null;
  227. return false;
  228. }
  229. }
  230. return true;
  231. }
  232. /**
  233. * Detects if a PHP accelerator is running and what type it is.
  234. *
  235. * @return ezcSystemInfoAccelerator or null if no PHP accelerator detected
  236. */
  237. public static function phpAccelerator()
  238. {
  239. $phpAcceleratorInfo = null;
  240. if ( ezcBaseFeatures::hasExtensionSupport( "Turck MMCache" ) )
  241. {
  242. $phpAcceleratorInfo = new ezcSystemInfoAccelerator(
  243. "Turck MMCache", // name
  244. "http://turck-mmcache.sourceforge.net", // url
  245. true, // isEnabled
  246. false, // version int
  247. false // version string
  248. );
  249. }
  250. if ( ezcBaseFeatures::hasExtensionSupport( "eAccelerator" ) )
  251. {
  252. $phpAcceleratorInfo = new ezcSystemInfoAccelerator(
  253. "eAccelerator", // name
  254. "http://sourceforge.net/projects/eaccelerator/", // url
  255. true, // isEnabled
  256. false, // version int
  257. phpversion( 'eAccelerator' ) // version string
  258. );
  259. }
  260. if ( ezcBaseFeatures::hasExtensionSupport( "apc" ) )
  261. {
  262. $phpAcceleratorInfo = new ezcSystemInfoAccelerator(
  263. "APC", // name
  264. "http://pecl.php.net/package/APC", // url
  265. ( ini_get( 'apc.enabled' ) != 0 ), // isEnabled
  266. false, // version int
  267. phpversion( 'apc' ) // version string
  268. );
  269. }
  270. if ( ezcBaseFeatures::hasExtensionSupport( "Zend Performance Suite" ) )
  271. {
  272. $phpAcceleratorInfo = new ezcSystemInfoAccelerator(
  273. "Zend Performance Suite", // name
  274. "http://www.zend.com/en/products/platform/", // url
  275. true, // isEnabled
  276. false, // version int
  277. false // version string
  278. );
  279. }
  280. if ( ezcBaseFeatures::hasExtensionSupport( 'XCache' ) )
  281. {
  282. $phpAcceleratorInfo = new ezcSystemInfoAccelerator(
  283. "XCache", // name
  284. "http://xcache.lighttpd.net/", // url
  285. true, // isEnabled
  286. false, // version int
  287. phpversion( 'XCache' ) // version string
  288. );
  289. }
  290. return $phpAcceleratorInfo;
  291. }
  292. /**
  293. * Determines if the script was executed over the web or the shell/command line.
  294. *
  295. * @return bool
  296. */
  297. public static function isShellExecution()
  298. {
  299. $sapiType = php_sapi_name();
  300. if ( $sapiType == 'cli' )
  301. {
  302. return true;
  303. }
  304. // For CGI we have to check, if the script has been executed over shell.
  305. // Currently it looks like the HTTP_HOST variable is the most reasonable to check.
  306. if ( substr( $sapiType, 0, 3 ) == 'cgi' )
  307. {
  308. if ( !isset( $_SERVER['HTTP_HOST'] ) )
  309. {
  310. return true;
  311. }
  312. else
  313. {
  314. return false;
  315. }
  316. }
  317. return false;
  318. }
  319. /**
  320. * Returns the PHP version as an array with the version elements.
  321. *
  322. * @return array(string)
  323. */
  324. public static function phpVersion()
  325. {
  326. return explode( '.', phpVersion() );
  327. }
  328. /**
  329. * Property read access.
  330. *
  331. * @throws ezcBasePropertyNotFoundException
  332. * If the the desired property is not found.
  333. * @param string $property Name of the property.
  334. * @return mixed Value of the property or null.
  335. * @ignore
  336. */
  337. public function __get( $property )
  338. {
  339. if ( $this->systemInfoReader == null &&
  340. ( $property == 'cpuType' ||
  341. $property == 'cpuCount' ||
  342. $property == 'cpuSpeed' ||
  343. $property == 'memorySize'
  344. )
  345. )
  346. {
  347. return null;
  348. }
  349. switch ( $property )
  350. {
  351. case 'osType':
  352. return $this->osType;
  353. case 'osName':
  354. return $this->osName;
  355. case 'fileSystemType':
  356. return $this->fileSystemType;
  357. case 'cpuCount':
  358. return $this->systemInfoReader->getCpuCount();
  359. case 'cpuType':
  360. return $this->systemInfoReader->cpuType();
  361. case 'cpuSpeed':
  362. return $this->systemInfoReader->cpuSpeed();
  363. case 'memorySize':
  364. return $this->systemInfoReader->memorySize();
  365. case 'lineSeparator':
  366. return $this->lineSeparator;
  367. case 'backupFileName':
  368. return $this->backupFileName;
  369. case 'phpVersion':
  370. return $this->phpVersion();
  371. case 'phpAccelerator':
  372. return $this->phpAccelerator();
  373. case 'isShellExecution':
  374. return $this->isShellExecution();
  375. default:
  376. break;
  377. }
  378. throw new ezcBasePropertyNotFoundException( $property );
  379. }
  380. }
  381. ?>