I am using the Zend Framework, and I use the .htaccess
for some settings. I am now writing command line scripts for scheduling (e.g. cron). Command line scripts don't look at the .htaccess
file because they're not served up by Apache. I would like to parse the .htaccess
with my script to retrieve some settings. Here are the lines I'm specifically interested in:
我正在使用Zend Framework,我使用.htaccess进行一些设置。我现在正在编写用于调度的命令行脚本(例如cron)。命令行脚本不查看.htaccess文件,因为它们不是由Apache提供的。我想用我的脚本解析.htaccess来检索一些设置。以下是我特别感兴趣的专栏:
SetEnv APPLICATION_ENV development
php_value date.timezone America/New_York
I noticed the PEAR File_HtAccess package, but it seems to only address authentication portions of the .htaccess
file.
我注意到了PEAR File_HtAccess包,但似乎只解决了.htaccess文件的身份验证部分。
SOLUTION: (with credit due to Bamieater)
解决方案:(由于Bamieater的信用)
echo
statements for debug output, removed from working code.
调试输出的echo语句,从工作代码中删除。
$htaccess = file(realpath(dirname(__FILE__)) . '/.htaccess');
echo '<pre>';
foreach ($htaccess as $line) {
if (preg_match('/^\s*SetEnv\s+APPLICATION_ENV\s+(.*?)\s*$/', trim($line), $matches)) {
defined('APPLICATION_ENV') || define('APPLICATION_ENV', $matches[1]);
echo APPLICATION_ENV . PHP_EOL;
} elseif (preg_match('/^\s*php_(?:admin_)?value\s+date\.timezone\s+(.*?)\s*$/', $line, $matches)) {
date_default_timezone_set($matches[1]);
echo date_default_timezone_get() . PHP_EOL;
}
}
echo '</pre>';
4 个解决方案
#1
6
Read the .htaccess file per line and use a regular expression to access the data.
每行读取.htaccess文件并使用正则表达式访问数据。
For example something like this:
例如这样的事情:
$line = "php_value date.timezone America/New_York";
$pattern = "@^php_value date.timezone (.*)$@";
if(preg_match($pattern,$line,$matches))
{
print_r($matches);
}
#2
2
Borrowing a bit from Bamieater, here's a more complete function you can drop into the beginning of your cron job file that sets the environment variables and includes the php_prepend_file.
借用Bamieater中的一点,这里有一个更完整的函数,你可以放入你的cron作业文件的开头,它设置环境变量并包含php_prepend_file。
function isCLI() {
return (PHP_SAPI == 'cli');
}
function loadEnvironmentFromHtaccess($file) {
$line = file_get_contents($file);
$pattern = '@SetEnv (?P<env>[^ ]*) (?P<value>[^ \n]*)@';
preg_match_all($pattern, $line, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$_SERVER[$match['env']] = $match['value'];
}
$pattern = '@php_value auto_prepend_file (?P<value>[^ \n]*)@';
if (preg_match($pattern, $line, $matches)) {
require $matches['value'];
}
}
if (isCLI()) {
loadEnvironmentFromHtaccess(dirname(__FILE__).'/../../.htaccess');
}
#3
1
Parsing .htaccess
, despite it is easy to do, is not the best solution in my opinion.
解析.htaccess,尽管很容易做到,但在我看来并不是最好的解决方案。
The environment variable may be set in different places, so you'd have to check all the files (eg. main Apache configuration, other .htaccesses).
环境变量可以设置在不同的位置,因此您必须检查所有文件(例如主Apache配置,其他.htaccesses)。
I'd recommend setting separate environment variable for your shell scripts,
我建议为shell脚本设置单独的环境变量,
export APPLICATION_ENV=staging
I keep those settings in global properties of the server (apache config and .bashrc), so automatically all the apps know where they are without changing the files upon deployment.
我将这些设置保存在服务器的全局属性中(apache config和.bashrc),因此所有应用程序都会自动知道它们的位置,而无需在部署时更改文件。
#4
1
A late answer but it might interest you (or others that come into this thread).
一个迟到的答案,但你可能会感兴趣(或其他人进入这个线程)。
I wrote a little .htaccess
parser, in PHP, that enables one to manipulate the file as an ArrayObject.
我在PHP中编写了一个小的.htaccess解析器,它使人们能够将文件作为ArrayObject进行操作。
You can check the project here (GitHub).
你可以在这里查看项目(GitHub)。
#1
6
Read the .htaccess file per line and use a regular expression to access the data.
每行读取.htaccess文件并使用正则表达式访问数据。
For example something like this:
例如这样的事情:
$line = "php_value date.timezone America/New_York";
$pattern = "@^php_value date.timezone (.*)$@";
if(preg_match($pattern,$line,$matches))
{
print_r($matches);
}
#2
2
Borrowing a bit from Bamieater, here's a more complete function you can drop into the beginning of your cron job file that sets the environment variables and includes the php_prepend_file.
借用Bamieater中的一点,这里有一个更完整的函数,你可以放入你的cron作业文件的开头,它设置环境变量并包含php_prepend_file。
function isCLI() {
return (PHP_SAPI == 'cli');
}
function loadEnvironmentFromHtaccess($file) {
$line = file_get_contents($file);
$pattern = '@SetEnv (?P<env>[^ ]*) (?P<value>[^ \n]*)@';
preg_match_all($pattern, $line, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$_SERVER[$match['env']] = $match['value'];
}
$pattern = '@php_value auto_prepend_file (?P<value>[^ \n]*)@';
if (preg_match($pattern, $line, $matches)) {
require $matches['value'];
}
}
if (isCLI()) {
loadEnvironmentFromHtaccess(dirname(__FILE__).'/../../.htaccess');
}
#3
1
Parsing .htaccess
, despite it is easy to do, is not the best solution in my opinion.
解析.htaccess,尽管很容易做到,但在我看来并不是最好的解决方案。
The environment variable may be set in different places, so you'd have to check all the files (eg. main Apache configuration, other .htaccesses).
环境变量可以设置在不同的位置,因此您必须检查所有文件(例如主Apache配置,其他.htaccesses)。
I'd recommend setting separate environment variable for your shell scripts,
我建议为shell脚本设置单独的环境变量,
export APPLICATION_ENV=staging
I keep those settings in global properties of the server (apache config and .bashrc), so automatically all the apps know where they are without changing the files upon deployment.
我将这些设置保存在服务器的全局属性中(apache config和.bashrc),因此所有应用程序都会自动知道它们的位置,而无需在部署时更改文件。
#4
1
A late answer but it might interest you (or others that come into this thread).
一个迟到的答案,但你可能会感兴趣(或其他人进入这个线程)。
I wrote a little .htaccess
parser, in PHP, that enables one to manipulate the file as an ArrayObject.
我在PHP中编写了一个小的.htaccess解析器,它使人们能够将文件作为ArrayObject进行操作。
You can check the project here (GitHub).
你可以在这里查看项目(GitHub)。