Skip to content

Commit 170d620

Browse files
committed
Config data is now cached in a global var so the file system is not hit so often. You can also set config data temporarily for the script if you are using your own external script.
git-svn-id: http://svn.php.net/repository/pear/packages/PHP_CodeSniffer/trunk@248462 c90b9560-bf6c-de11-be94-00142212c4b1
1 parent ae66426 commit 170d620

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

CodeSniffer.php

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,21 +1495,26 @@ public static function getConfigData($key)
14951495
* @param string|null $value The value to set. If null, the config
14961496
* entry is deleted, reverting it to the
14971497
* default value.
1498+
* @param boolean $temp Set this config data temporarily for this
1499+
* script run. This will not write the config
1500+
* data to the config file.
14981501
*
14991502
* @return boolean
15001503
* @see getConfigData()
15011504
* @throws PHP_CodeSniffer_Exception If the config file can not be written.
15021505
*/
1503-
public static function setConfigData($key, $value)
1506+
public static function setConfigData($key, $value, $temp=false)
15041507
{
1505-
$configFile = dirname(__FILE__).'/CodeSniffer.conf';
1506-
if (is_file($configFile) === false) {
1507-
$configFile = '@data_dir@/PHP_CodeSniffer/CodeSniffer.conf';
1508-
}
1508+
if ($temp === false) {
1509+
$configFile = dirname(__FILE__).'/CodeSniffer.conf';
1510+
if (is_file($configFile) === false) {
1511+
$configFile = '@data_dir@/PHP_CodeSniffer/CodeSniffer.conf';
1512+
}
15091513

1510-
if (is_file($configFile) === true && is_writable($configFile) === false) {
1511-
$error = "Config file $configFile is not writable";
1512-
throw new PHP_CodeSniffer_Exception($error);
1514+
if (is_file($configFile) === true && is_writable($configFile) === false) {
1515+
$error = "Config file $configFile is not writable";
1516+
throw new PHP_CodeSniffer_Exception($error);
1517+
}
15131518
}
15141519

15151520
$phpCodeSnifferConfig = self::getAllConfigData();
@@ -1522,14 +1527,18 @@ public static function setConfigData($key, $value)
15221527
$phpCodeSnifferConfig[$key] = $value;
15231528
}
15241529

1525-
$output = '<'.'?php'."\n".' $phpCodeSnifferConfig = ';
1526-
$output .= var_export($phpCodeSnifferConfig, true);
1527-
$output .= "\n?".'>';
1530+
if ($temp === false) {
1531+
$output = '<'.'?php'."\n".' $phpCodeSnifferConfig = ';
1532+
$output .= var_export($phpCodeSnifferConfig, true);
1533+
$output .= "\n?".'>';
15281534

1529-
if (file_put_contents($configFile, $output) === false) {
1530-
return false;
1535+
if (file_put_contents($configFile, $output) === false) {
1536+
return false;
1537+
}
15311538
}
15321539

1540+
$GLOBALS['PHP_CODESNIFFER_CONFIG_DATA'] = $phpCodeSnifferConfig;
1541+
15331542
return true;
15341543

15351544
}//end setConfigData()
@@ -1543,6 +1552,10 @@ public static function setConfigData($key, $value)
15431552
*/
15441553
public static function getAllConfigData()
15451554
{
1555+
if (isset($GLOBALS['PHP_CODESNIFFER_CONFIG_DATA']) === true) {
1556+
return $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA'];
1557+
}
1558+
15461559
$configFile = dirname(__FILE__).'/CodeSniffer.conf';
15471560
if (is_file($configFile) === false) {
15481561
$configFile = '@data_dir@/PHP_CodeSniffer/CodeSniffer.conf';
@@ -1553,7 +1566,8 @@ public static function getAllConfigData()
15531566
}
15541567

15551568
include $configFile;
1556-
return $phpCodeSnifferConfig;
1569+
$GLOBALS['PHP_CODESNIFFER_CONFIG_DATA'] = $phpCodeSnifferConfig;
1570+
return $GLOBALS['PHP_CODESNIFFER_CONFIG_DATA'];
15571571

15581572
}//end getAllConfigData()
15591573

package.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
4141
-- Other tokens (like T_FUNCTION) still stop after 3 lines for performance
4242
- You can now esacpe commas in ignore patterns so they can be matched in file names
4343
-- Thanks to Carsten Wiedmann for the patch
44+
- Config data is now cached in a global var so the file system is not hit so often
45+
-- You can also set config data temporarily for the script if you are using your own external script
46+
-- Pass TRUE as the third argument to PHP_CodeSniffer::setConfigData()
4447
- PEAR ClassDeclarationSniff no longer throws errors for multi-line class declarations
4548
- Squiz ClassDeclarationSniff now ensures there is one blank line after a class closing brace
4649
- Squiz ClassDeclarationSniff now throws errors for a missing end PHP tag after the end class tag

0 commit comments

Comments
 (0)