From b38735eec565e3dbb28bea8ec8de3009b0fee85b Mon Sep 17 00:00:00 2001 From: wakaba Date: Wed, 25 Jan 2017 14:14:32 +0900 Subject: [PATCH] add feature : directory access support --- convert.php | 166 ++++++++++++++++++++++++++++------------------------ 1 file changed, 91 insertions(+), 75 deletions(-) diff --git a/convert.php b/convert.php index 2255c11..d66b16b 100755 --- a/convert.php +++ b/convert.php @@ -6,25 +6,25 @@ * * Command-line script to convert PHP's "array()" syntax to PHP 5.4's * short array syntax "[]" using PHP's built-in tokenizer. - * + * * This script is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License (LGPL) as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. - * + * * This script is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - * + * * @link https://github.com/thomasbachem/php-short-array-syntax-converter * * @link http://php.net/manual/en/language.types.array.php - * + * * @license http://www.gnu.org/licenses/lgpl.html * @author Thomas Bachem */ @@ -36,7 +36,7 @@ $saveFile = false; if ($argc > 3) { - file_put_contents('php://stderr', 'Usage: php convert.php [-w] ' . "\n"); + file_put_contents('php://stderr', 'Usage: php convert.php [-w] ' . "\n"); exit(1); } for ($i = 1; $i < $argc; ++$i) { @@ -56,98 +56,114 @@ } -// - - - - - READ ORIGINAL CODE - - - - - +// - - - - - SEARCH TARGET PATH - - - - - -$code = file_get_contents($filePath); -$tokens = token_get_all($code); +$filePathList = []; +if (is_dir($filePath)) { + foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($filePath)) as $fileinfo) { + if ($fileinfo->isFile()) { + $filePathList[] = $fileinfo->getPathname(); + } + } +} else { + $filePathList[] = $filePath; +} -// - - - - - PARSE CODE - - - - - + // - - - - - READ ORIGINAL CODE - - - - - -$replacements = array(); -$offset = 0; -for ($i = 0; $i < count($tokens); ++$i) { - // Keep track of the current byte offset in the source code - $offset += strlen(is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i]); +foreach ($filePathList as $filePath) { + $code = file_get_contents($filePath); + $tokens = token_get_all($code); - // T_ARRAY could either mean the "array(...)" syntax we're looking for - // or a type hinting statement ("function(array $foo) { ... }") - if (is_array($tokens[$i]) && $tokens[$i][0] === T_ARRAY) { - // Look for a subsequent opening bracket ("(") to be sure we're actually - // looking at an "array(...)" statement - $isArraySyntax = false; - $subOffset = $offset; - for ($j = $i + 1; $j < count($tokens); ++$j) { - $subOffset += strlen(is_array($tokens[$j]) ? $tokens[$j][1] : $tokens[$j]); - if (is_string($tokens[$j]) && $tokens[$j] == '(') { - $isArraySyntax = true; - break; - } elseif (!is_array($tokens[$j]) || $tokens[$j][0] !== T_WHITESPACE) { - $isArraySyntax = false; - break; - } - } + // - - - - - PARSE CODE - - - - - - if ($isArraySyntax) { - // Replace "array" and the opening bracket (including preceeding whitespace) with "[" - $replacements[] = array( - 'start' => $offset - strlen($tokens[$i][1]), - 'end' => $subOffset, - 'string' => '[', - ); + $replacements = array(); + $offset = 0; + for ($i = 0; $i < count($tokens); ++$i) { + // Keep track of the current byte offset in the source code + $offset += strlen(is_array($tokens[$i]) ? $tokens[$i][1] : $tokens[$i]); - // Look for matching closing bracket (")") + // T_ARRAY could either mean the "array(...)" syntax we're looking for + // or a type hinting statement ("function(array $foo) { ... }") + if (is_array($tokens[$i]) && $tokens[$i][0] === T_ARRAY) { + // Look for a subsequent opening bracket ("(") to be sure we're actually + // looking at an "array(...)" statement + $isArraySyntax = false; $subOffset = $offset; - $openBracketsCount = 0; for ($j = $i + 1; $j < count($tokens); ++$j) { $subOffset += strlen(is_array($tokens[$j]) ? $tokens[$j][1] : $tokens[$j]); if (is_string($tokens[$j]) && $tokens[$j] == '(') { - ++$openBracketsCount; - } elseif (is_string($tokens[$j]) && $tokens[$j] == ')') { - --$openBracketsCount; - - if ($openBracketsCount == 0) { - // Replace ")" with "]" - $replacements[] = array( - 'start' => $subOffset - 1, - 'end' => $subOffset, - 'string' => ']', - ); - break; + $isArraySyntax = true; + break; + } elseif (!is_array($tokens[$j]) || $tokens[$j][0] !== T_WHITESPACE) { + $isArraySyntax = false; + break; + } + } + + if ($isArraySyntax) { + // Replace "array" and the opening bracket (including preceeding whitespace) with "[" + $replacements[] = array( + 'start' => $offset - strlen($tokens[$i][1]), + 'end' => $subOffset, + 'string' => '[', + ); + + // Look for matching closing bracket (")") + $subOffset = $offset; + $openBracketsCount = 0; + for ($j = $i + 1; $j < count($tokens); ++$j) { + $subOffset += strlen(is_array($tokens[$j]) ? $tokens[$j][1] : $tokens[$j]); + + if (is_string($tokens[$j]) && $tokens[$j] == '(') { + ++$openBracketsCount; + } elseif (is_string($tokens[$j]) && $tokens[$j] == ')') { + --$openBracketsCount; + + if ($openBracketsCount == 0) { + // Replace ")" with "]" + $replacements[] = array( + 'start' => $subOffset - 1, + 'end' => $subOffset, + 'string' => ']', + ); + break; + } } } } } } -} -// - - - - - UPDATE CODE - - - - - + // - - - - - UPDATE CODE - - - - - -// Apply the replacements to the source code -$offsetChange = 0; -foreach ($replacements as $replacement) { - $code = substr_replace( - $code, - $replacement['string'], - $replacement['start'] + $offsetChange, - $replacement['end'] - $replacement['start'] - ); - $offsetChange += strlen($replacement['string']) - ($replacement['end'] - $replacement['start']); -} + // Apply the replacements to the source code + $offsetChange = 0; + foreach ($replacements as $replacement) { + $code = substr_replace( + $code, + $replacement['string'], + $replacement['start'] + $offsetChange, + $replacement['end'] - $replacement['start'] + ); + $offsetChange += strlen($replacement['string']) - ($replacement['end'] - $replacement['start']); + } -// - - - - - OUTPUT/WRITE NEW CODE - - - - - + // - - - - - OUTPUT/WRITE NEW CODE - - - - - -if ($saveFile) { - if ($replacements) { - file_put_contents($filePath, $code); - print count($replacements) . ' replacements.' . "\n"; + if ($saveFile) { + if ($replacements) { + file_put_contents($filePath, $code); + print count($replacements) . ' replacements.' . "\n"; + } else { + print 'No replacements.' . "\n"; + } } else { - print 'No replacements.' . "\n"; + print $code; } -} else { - print $code; -} \ No newline at end of file +}