Symfony 1.4使用php 5.5中的弃用函数

时间:2013-08-06 10:14:09

标签: php preg-replace symfony-1.4 preg-replace-callback

我最近将PHP从5.3.27升级到5.5.0。 在我的Symfony 2.3.2项目中一切正常,我可以享受最新的PHP功能。

现在当我回到我的其他Symfony 1.4.16项目时,我得到一个关于preg_replace被/ e修饰符弃用的PHP错误。

我在论坛中找不到关于此错误的引用:之前有没有人遇到此问题?有什么样的补丁我可以开箱即用吗?升级到Symfony 1.4.20会解决这个问题吗?

错误信息如下:

  

不推荐使用:preg_replace():不推荐使用/ e修饰符,而是在第409行的/myproject/lib/vendor/symfony/lib/response/sfWebResponse.class.php中使用preg_replace_callback

一种方法可能是按照消息和manual中的建议修改代码。 如何将preg_replace表达式更改为preg_replace_callback调用?

非常欢迎任何帮助/提示。

编辑:

到目前为止,还没有针对此的补丁(Symfony 1.4.20没有解决这个问题)。解决方案是将对preg_replace的失败调用替换为在sourche中对preg_replace_callback的相应调用,这很容易在sfWebResponse类中完成(感谢提示Jon)。不幸的是,现在next failing occurrence稍微复杂一点......另一方面,我们可能不得不使用/ e选项grep for preg_replace使用以找出Symfony可能会破坏的位置。这给出了不少结果:o

所以......我的结论是Symfony 1.4用户最好不要将PHP升级到5.5版,直到出现一些严重的补丁。你怎么看 ?还有其他选择吗?

7 个答案:

答案 0 :(得分:37)

除非您在 index.php 中启用了调试,否则错误不会显示在prod中。通过在 settings.yml 中取消设置E_DEPRECATED标志,也可以在dev中删除它们:

dev:
  .settings:
    error_reporting:  <?php echo ((E_ALL | E_STRICT) ^ E_DEPRECATED)."\n" ?>

答案 1 :(得分:13)

基本上你需要做的是从preg_replace调用中取出替换参数并将其分解为正确的PHP表达式,然后使该表达式成为将用作回调函数的函数体。等效preg_replace_callback电话。

在您的情况下,relevant code

return preg_replace('/\-(.)/e', "'-'.strtoupper('\\1')", /* input */)

所以你会这样做

$callback = function($matches) {
    return '-'.strtoupper($matches[1]);
};

return preg_replace_callback('/\-(.)/', $callback, /* input */)

正如您所看到的,回调代码与原始替换表达式相同,唯一的区别是诸如\\1之类的引用被替换为$matches[1]等数组访问。

答案 2 :(得分:10)

总而言之,最好的解决方案是避免将PHP升级到5.5版,因为它与Symfony 1.4不兼容

如果您在开发环境中同时拥有Symfony 2和1.4版本,则可能希望能够切换PHP版本,如here所述。

如果你真的需要,可以同时在同一台Apache服务器上设置两个不同版本的PHP:这需要更多配置,上面的链接也解释了这一点。

替代HOT FIX:

通过Symfony代码中的一些更新,我可以在dev中运行大部分网页。当然,在生产中应用它会很危险,因为“弃用”错误可能随时再次出现,这是由另一个Symfony库引起的。

在第409行的myproject / lib / vendor / symfony / lib / response / sfWebResponse.class.php中,我现在(已注释的代码是原始的Symfony代码):

  protected function normalizeHeaderName($name)
  {
    // return preg_replace('/\-(.)/e', "'-'.strtoupper('\\1')", strtr(ucfirst(strtolower($name)), '_', '-'));    

    return preg_replace_callback(
                  '/\-(.)/', 
                  function ($matches) {
                    return '-'.strtoupper($matches[1]);
                  }, 
                  strtr(ucfirst(strtolower($name)), '_', '-')
        );
  }

在第362行的myproject / lib / vendor / symfony / lib / util / sfToolkit.class.php中,我们得到:

  public static function pregtr($search, $replacePairs)
  {
    // return preg_replace(array_keys($replacePairs), array_values($replacePairs), $search);
    foreach($replacePairs as $pattern => $replacement)
        $search = preg_replace_callback(
                    $pattern, 
                    function ($matches) use ($replacement){
                        if(array_key_exists(1, $matches)){ $replacement = str_replace("\\1", $matches[1], $replacement);}
                        if(array_key_exists(2, $matches)){ $replacement = str_replace("\\2", $matches[2], $replacement);}
                        return $replacement;
                    }, 
                    $search
                );
    return $search;
  }

使用风险自负:)

答案 3 :(得分:10)

第407行的/lib/vendor/symfony/lib/response/sfWebResponse.class.php中的normalizeHeaderName方法的FIX

protected function normalizeHeaderName($name)
{
  //return preg_replace('/\-(.)/e', "'-'.strtoupper('\\1')", 
  strtr(ucfirst(strtolower($name)), '_', '-');
  return str_replace(array('\'$1$3\'','\'$2$4\'','\'$1\'', '\'$2\'', '$1', '$2'),array('$matches[1].$matches[3]','$matches[2].$matches[4]','$matches[1]','$matches[2]','$matches[1]','$matches[2]'),
$name);
}

第360行/lib/vendor/symfony/lib/util/sfToolkit.class.php中的pregtr方法的FIX

public static function pregtr($search, $replacePairs){
  // return preg_replace(array_keys($replacePairs), array_values($replacePairs), $search);
  foreach($replacePairs as $pattern => $replacement)
  {
    if (preg_match('/(.*)e$/', $pattern, $matches))
    {
      $pattern = $matches[1];
      $search = preg_replace_callback($pattern, function ($matches) use ($replacement) {
        preg_match("/('::'\.)?([a-z]*)\('\\\\([0-9]{1})'\)/", $replacement, $match);
        return ($match[1]==''?'':'::').call_user_func($match[2], $matches[$match[3]]);
      }, $search);
    }
    else
    {
      $search = preg_replace($pattern, $replacement, $search);
    }
  }
  return $search;
}

答案 4 :(得分:8)

Symfony的社区版本维护和修补旧代码:

https://github.com/LExpress/symfony1

答案 5 :(得分:4)

第360行/ lib / vendor / symfony / lib / util / sfToolkit.class.php pregtr 方法的替代FIX

public static function pregtr($search, $replacePairs)
{
  // return preg_replace(array_keys($replacePairs), array_values($replacePairs), $search);
  foreach($replacePairs as $pattern => $replacement)
  {
    if (preg_match('/(.*)e$/', $pattern, $matches))
    {
      $pattern = $matches[1];
      $search = preg_replace_callback($pattern, function ($matches) use ($replacement) {
        preg_match("/('::'\.)?([a-z]*)\('\\\\([0-9]{1})'\)/", $replacement, $match);
        return ($match[1]==''?'':'::').call_user_func($match[2], $matches[$match[3]]);
      }, $search);
    }
    else
    {
      $search = preg_replace($pattern, $replacement, $search);
    }
  }
  return $search;
}

答案 6 :(得分:0)

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in lib/vendor/symfony/…This changelog will solve the problem for all symfony 1.4.x. Tested on Symfony 1.4.20
---
 lib/vendor/symfony/lib/command/sfCommandManager.class.php     |  4 +++-
 lib/vendor/symfony/lib/form/addon/sfFormObject.class.php      |  2 +-
 lib/vendor/symfony/plugins/sfDoctrinePlugin/lib/form/sfFormFilterDoctrine.class.php  |  2 +-
 lib/vendor/symfony/plugins/sfPropelPlugin/lib/form/sfFormFilterPropel.class.php      |  2 +-
 lib/vendor/symfony/lib/response/sfWebResponse.class.php       |  2 +-
 lib/vendor/symfony/lib/util/sfInflector.class.php             |  5 +----
 lib/vendor/symfony/lib/util/sfToolkit.class.php               | 11 +++++++++++
 7 files changed, 19 insertions(+), 9 deletions(-)

lib/vendor/symfony/lib/command/sfCommandManager.class.php
@@ -108,7 +108,9 @@ class sfCommandManager
     else if (!is_array($arguments))
     {
       // hack to split arguments with spaces : --test="with some spaces"
-      $arguments = preg_replace('/(\'|")(.+?)\\1/e', "str_replace(' ', '=PLACEHOLDER=', '\\2')", $arguments);
+      $arguments = preg_replace_callback('/(\'|")(.+?)\\1/', function($matches) {
+         return str_replace(' ', '=PLACEHOLDER=', $matches[2]);
+     }, $arguments);
       $arguments = preg_split('/\s+/', $arguments);
       $arguments = str_replace('=PLACEHOLDER=', ' ', $arguments);
     }

lib/vendor/symfony/lib/form/addon/sfFormObject.class.php
@@ -278,6 +278,6 @@ abstract class sfFormObject extends BaseForm

   protected function camelize($text)
   {
-    return preg_replace(array('#/(.?)#e', '/(^|_|-)+(.)/e'), array("'::'.strtoupper('\\1')", "strtoupper('\\2')"), $text);
+    return sfToolkit::camelize($text);
   }
 }

lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/form/sfFormFilterDoctrine.class.php
@@ -323,7 +323,7 @@ abstract class sfFormFilterDoctrine extends sfFormFilter

   protected function camelize($text)
   {
-    return sfToolkit::pregtr($text, array('#/(.?)#e' => "'::'.strtoupper('\\1')", '/(^|_|-)+(.)/e' => "strtoupper('\\2')"));
+    return sfToolkit::camelize($text);
   }

   protected function getTable()

lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/form/sfFormFilterPropel.class.php
@@ -263,6 +263,6 @@ abstract class sfFormFilterPropel extends sfFormFilter

   protected function camelize($text)
   {
-    return sfToolkit::pregtr($text, array('#/(.?)#e' => "'::'.strtoupper('\\1')", '/(^|_|-)+(.)/e' => "strtoupper('\\2')"));
+       return sfToolkit::camelize($text);
   }
 }

lib/vendor/symfony/lib/response/sfWebResponse.class.php
@@ -406,7 +406,7 @@ class sfWebResponse extends sfResponse
    */
   protected function normalizeHeaderName($name)
   {
-    return preg_replace('/\-(.)/e', "'-'.strtoupper('\\1')", strtr(ucfirst(strtolower($name)), '_', '-'));
+    return preg_replace_callback('/\-(.)/', function ($matches) { return '-'.strtoupper($matches[1]); }, strtr(ucfirst(strtolower($name)), '_', '-'));
   }

   /**

lib/vendor/symfony/lib/util/sfInflector.class.php
@@ -28,10 +28,7 @@ class sfInflector
   public static function camelize($lower_case_and_underscored_word)
   {
     $tmp = $lower_case_and_underscored_word;
-    $tmp = sfToolkit::pregtr($tmp, array('#/(.?)#e'    => "'::'.strtoupper('\\1')",
-                                         '/(^|_|-)+(.)/e' => "strtoupper('\\2')"));
-
-    return $tmp;
+    return sfToolkit::camelize($tmp);;
   }

   /**

lib/vendor/symfony/lib/util/sfToolkit.class.php
@@ -608,4 +608,15 @@ class sfToolkit

     return set_include_path(join(PATH_SEPARATOR, $paths));
   }
+
+   public static function camelize($text)
+   {
+       if (preg_match('#/(.?)#', $text, $matches)) {
+           $text = str_replace($matches[0], '::'.strtoupper($matches[1]), $text);
+       }
+       if (preg_match('/(^|_|-)+(.)/', $text, $matches)) {
+           $text = str_replace($matches[0], strtoupper($matches[2]), $text);
+       }
+       return $text;
+   }
 }
--