---
title: "symfony的环境检测代码"
author: "#rethink#"
date: "2010-07-20T15:14:00Z"
tags: []
slug: "symfony"
draft: false
lastmod: "2010-07-20T15:14:00Z"
id: 803733
url: "https://rethink2.iteye.com/blog/803733"
---

&lt;!--&lt;br/ /&gt;&lt;br/ /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br/ /&gt;http://www.CodeHighlighter.com/&lt;br/ /&gt;&lt;br/ /&gt;--&gt;&lt;?php

function is\_cli()  
{  
  return !isset($\_SERVER\['HTTP\_HOST']);  
}

/\**  
 * Checks a configuration.  
 \*/  
function check($boolean, $message, $help = '', $fatal = false)  
{  
  echo $boolean ? "  OK        " : sprintf("\[\[%s]] ", $fatal ? ' ERROR ' : 'WARNING');  
  echo sprintf("$message%s\\n", $boolean ? '' : ': FAILED');

  if (!$boolean)  
  {  
    echo "            \*\** $help \*\*\*\\n";  
    if ($fatal)  
    {  
      die("You must fix this problem before resuming the check.\\n");  
    }  
  }  
}

/\**  
 * Gets the php.ini path used by the current PHP interpretor.  
 *  
 * @return string the php.ini path  
 \*/  
function get\_ini\_path()  
{  
  if ($path = get\_cfg\_var('cfg\_file\_path'))  
  {  
    return $path;  
  }

  return 'WARNING: not using a php.ini file';  
}

if (!is\_cli())  
{  
  echo '&lt;html&gt;&lt;body&gt;&lt;pre&gt;';  
}

echo "\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\\n";  
echo "*                              \*\\n";  
echo "*  symfony requirements check  \*\\n";  
echo "*                              \*\\n";  
echo "\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\\n\\n";

echo sprintf("php.ini used by PHP: %s\\n\\n", get\_ini\_path());

if (is\_cli())  
{  
  echo "\** WARNING \*\*\\n";  
  echo "*  The PHP CLI can use a different php.ini file\\n";  
  echo "*  than the one used with your web server.\\n";  
  if ('\\\\' == DIRECTORY\_SEPARATOR)  
  {  
    echo "*  (especially on the Windows platform)\\n";  
  }  
  echo "*  If this is the case, please launch this\\n";  
  echo "*  utility from your web server.\\n";  
  echo "\** WARNING \*\*\\n";  
}

// mandatory  
echo "\\n\** Mandatory requirements \*\*\\n\\n";  
check(version\_compare(phpversion(), '5.2.4', '&gt;='), sprintf('PHP version is at least 5.2.4 (%s)', phpversion()), 'Current version is '.phpversion(), true);

// warnings  
echo "\\n\** Optional checks \*\*\\n\\n";  
check(class\_exists('PDO'), 'PDO is installed', 'Install PDO (mandatory for Propel and Doctrine)', false);  
if (class\_exists('PDO'))  
{  
  $drivers = PDO::getAvailableDrivers();  
  check(count($drivers), 'PDO has some drivers installed: '.implode(', ', $drivers), 'Install PDO drivers (mandatory for Propel and Doctrine)');  
}  
check(class\_exists('DomDocument'), 'PHP-XML module is installed', 'Install and enable the php-xml module (required by Propel)', false);  
check(class\_exists('XSLTProcessor'), 'XSL module is installed', 'Install and enable the XSL module (recommended for Propel)', false);  
check(function\_exists('token\_get\_all'), 'The token\_get\_all() function is available', 'Install and enable the Tokenizer extension (highly recommended)', false);  
check(function\_exists('mb\_strlen'), 'The mb\_strlen() function is available', 'Install and enable the mbstring extension', false);  
check(function\_exists('iconv'), 'The iconv() function is available', 'Install and enable the iconv extension', false);  
check(function\_exists('utf8\_decode'), 'The utf8\_decode() is available', 'Install and enable the XML extension', false);  
check(function\_exists('posix\_isatty'), 'The posix\_isatty() is available', 'Install and enable the php\_posix extension (used to colorized the CLI output)', false);

$accelerator =   
  (function\_exists('apc\_store') && ini\_get('apc.enabled'))  
  ||  
  function\_exists('eaccelerator\_put') && ini\_get('eaccelerator.enable')  
  ||  
  function\_exists('xcache\_set')  
;  
check($accelerator, 'A PHP accelerator is installed', 'Install a PHP accelerator like APC (highly recommended)', false);

check(!ini\_get('short\_open\_tag'), 'php.ini has short\_open\_tag set to off', 'Set it to off in php.ini', false);  
check(!ini\_get('magic\_quotes\_gpc'), 'php.ini has magic\_quotes\_gpc set to off', 'Set it to off in php.ini', false);  
check(!ini\_get('register\_globals'), 'php.ini has register\_globals set to off', 'Set it to off in php.ini', false);  
check(!ini\_get('session.auto\_start'), 'php.ini has session.auto\_start set to off', 'Set it to off in php.ini', false);

check(version\_compare(phpversion(), '5.2.9', '!='), 'PHP version is not 5.2.9', 'PHP 5.2.9 broke array\_unique() and sfToolkit::arrayDeepMerge(). Use 5.2.10 instead \[Ticket #6211]', false);

if (!is\_cli())  
{  
  echo '&lt;/pre&gt;&lt;/body&gt;&lt;/html&gt;';  
}