Skip to content

Commit 7cbbe6b

Browse files
committed
Optimized sfCompileConfigHandler and added unit tests
1 parent c4b4ca6 commit 7cbbe6b

File tree

3 files changed

+44
-39
lines changed

3 files changed

+44
-39
lines changed

lib/config/sfCompileConfigHandler.class.php

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -48,56 +48,32 @@ public function execute($configFiles)
4848
throw new sfParseException(sprintf('Configuration file "%s" specifies nonexistent or unreadable file "%s".', $configFiles[0], $file));
4949
}
5050

51-
$contents = file_get_contents($file);
51+
if (sfConfig::get('sf_debug'))
52+
{
53+
$contents = file_get_contents($file);
5254

53-
// strip comments (not in debug mode)
54-
if (!sfConfig::get('sf_debug'))
55+
// replace windows and mac format with unix format
56+
$contents = str_replace("\r", PHP_EOL, $contents);
57+
}
58+
else
5559
{
56-
$contents = sfToolkit::stripComments($contents);
60+
// strip whitespaces and comments
61+
$contents = php_strip_whitespace($file);
5762
}
5863

59-
// insert configuration files
60-
/* $contents = preg_replace_callback(array('#(require|include)(_once)?\((sfContext::getInstance\(\)\->getConfigCache\(\)|\$configCache)->checkConfig\(\'config/([^\']+)\'\)\);#m',
61-
'#()()(sfContext::getInstance\(\)\->getConfigCache\(\)|\$configCache)->import\(\'config/([^\']+)\'(, false)?\);#m'),
62-
array($this, 'insertConfigFileCallback'), $contents);
63-
*/
6464
// strip php tags
65-
$contents = sfToolkit::pregtr($contents, array('/^\s*<\?(php)?/m' => '',
66-
'/^\s*\?>/m' => ''));
67-
68-
// replace windows and mac format with unix format
69-
$contents = str_replace("\r", "\n", $contents);
70-
71-
// replace multiple new lines with a single newline
72-
$contents = preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $contents);
65+
$contents = sfToolkit::pregtr($contents, array('/^\s*<\?(php)?/m' => '', '/^\s*\?>/m' => ''));
7366

7467
// append file data
75-
$data .= "\n".$contents;
68+
$data .= PHP_EOL.$contents;
7669
}
7770

7871
// compile data
79-
$retval = sprintf("<?php\n".
80-
"// auto-generated by sfCompileConfigHandler\n".
81-
"// date: %s\n%s\n",
72+
return sprintf('<?php'.PHP_EOL.
73+
'// auto-generated by sfCompileConfigHandler'.PHP_EOL.
74+
'// date: %s'.PHP_EOL.
75+
'%s'.PHP_EOL,
8276
date('Y/m/d H:i:s'), $data);
83-
84-
return $retval;
85-
}
86-
87-
/**
88-
* Callback for configuration file insertion in the cache.
89-
*
90-
*/
91-
protected function insertConfigFileCallback($matches)
92-
{
93-
$configFile = 'config/'.$matches[4];
94-
95-
$configCache = sfContext::getInstance()->getConfigCache();
96-
$configCache->checkConfig($configFile);
97-
98-
$config = "// '$configFile' config file\n".file_get_contents($configCache->getCacheName($configFile));
99-
100-
return $config;
10177
}
10278

10379
/**
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- %SF_SYMFONY_LIB_DIR%/util/sfInflector.class.php
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the symfony package.
5+
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
require_once(dirname(__FILE__).'/../../bootstrap/unit.php');
12+
13+
$t = new lime_test(2);
14+
15+
$handler = new sfCompileConfigHandler();
16+
$handler->initialize();
17+
18+
$dir = dirname(__FILE__).DIRECTORY_SEPARATOR.'fixtures'.DIRECTORY_SEPARATOR.'sfCompileConfigHandler'.DIRECTORY_SEPARATOR;
19+
20+
$t->diag('execute');
21+
22+
sfConfig::set('sf_debug', true);
23+
$data = $handler->execute(array($dir.'simple.yml'));
24+
$t->ok(false !== strpos($data, 'class sfInflector'.PHP_EOL.'{'.PHP_EOL.' /**'), '->execute() return complete classe codes');
25+
26+
sfConfig::set('sf_debug', false);
27+
$data = $handler->execute(array($dir.'simple.yml'));
28+
$t->ok(false !== strpos($data, 'class sfInflector { public'), '->execute() return minified classe codes');

0 commit comments

Comments
 (0)