2017-06-05 88 views
1

我需要一个代码嗅探器转换所有变量在我所有的PHP文件从snake_case驼峰代码嗅探器转换snake_case到驼峰

我不需要在PHP中对字符串进行操作的函数,我想将我的PHP文件中的变量转换为camelCase。

我很欣赏你的先进。

回答

1

我找到了一个办法。 我整合了代码cakePHP(有ctp扩展名的文件和变量传递给查看设置功能)。

保存下面的代码为tocamelcase.php

php tocamelcase.php the/path/of/your/app 

文件内容:

<?php 
function snakeToCamel($val) { 
    preg_match('#^_*#', $val, $underscores); 
    $underscores = current($underscores); 
    $camel = str_replace('||||', '', ucwords(str_replace('_', '||||', $val), '||||')); 
    $camel = strtolower(substr($camel, 0, 1)).substr($camel, 1); 

    return $underscores.$camel; 
} 

function convert($str) { 
    global $j; 
    $name = '/(\$[a-zA-Z0-9]+_[a-zA-Z0-9_]+)|'. 
      '(->[a-zA-Z0-9]+_[a-zA-Z0-9_]+)|'. 
      '(::[a-zA-Z0-9]+_[a-zA-Z0-9_]+)|'. 
      '(\sfunction\s+[a-zA-Z0-9]+_[a-zA-Z0-9_]+)|'. 
      '(\$this->set\(\'[a-zA-Z0-9]+_[a-zA-Z0-9_]+\'\,)|'. 
      '(\$this->set\(\"[a-zA-Z0-9]+_[a-zA-Z0-9_]+\"\,)'. 
      '/i'; 
    $str = preg_replace_callback($name, function($matches){ 
     return snakeToCamel($matches[0]); 
    },$str); 
    return $str; 
} 

$path = $argv[1]; 
$Iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); 
$i=1; 
foreach($Iterator as $file){ 
    if(substr($file,-4) !== '.php' && substr($file,-4) !== '.ctp') 
     continue; 
    echo($i.": ".$file."\n"); 
    $i++; 
    $out = convert(file_get_contents($file)); 
    file_put_contents($file, $out); 
} 
+0

链接无效。你能看看吗? –