/** * One Line Description * * @command format_php * @argument file Which file? */ function pestle_cli($argv) { define('START', 0); define('PARSE_IF', 1); define('INSIDE_IF_BLOCK', 2); $file = $argv['file']; $tokens = pestle_token_get_all(file_get_contents($file)); //remove whitespace tokens $tokens = array_filter($tokens, function ($token) { return $token->token_name !== 'T_WHITESPACE'; }); $tokens = array_values($tokens); $state = 0; $indent_level = 0; foreach ($tokens as $key => $token) { $before = ''; $after = ''; //state switching if ($token->token_name == 'T_IF') { $state = PARSE_IF; } if ($state == PARSE_IF && $token->token_value === ':') { $indent_level++; $state = INSIDE_IF_BLOCK; } if ($state == INSIDE_IF_BLOCK && $token->token_name === 'T_ENDIF') { $state = START; $indent_level--; } //manipuate extra output tokens if ($token->token_value === '{') { $indent_level++; $after = "\n" . str_repeat(" ", $indent_level); } if ($token->token_value === '}') { $indent_level--; $after = "\n" . str_repeat(" ", $indent_level); } if ($token->token_name === 'T_CLOSE_TAG') { $after = "\n" . str_repeat(" ", $indent_level); } if (tokenIsSemiColonAndNextTokenIsNotTCloseTag($tokens, $key)) { $after = "\n" . str_repeat(" ", $indent_level); } if ($token->token_name === 'T_INLINE_HTML' && !trim($token->token_value)) { continue; } //do output echo $before; echo $token->token_value; echo $after; } }
function getAclRulesFromIsAllowedFunction($string) { $tokens = pestle_token_get_all('<' . '?' . 'php ' . "\n" . $string); $state = STATE_ACLRULE_START; foreach ($tokens as $token) { if ($state === STATE_ACLRULE_START) { if ($token->token_name === 'T_STRING' && $token->token_value === 'isAllowed') { $state = STATE_ACLRULE_FOUND_ISALLOWED; } continue; } if ($state === STATE_ACLRULE_FOUND_ISALLOWED) { if ($token->token_name === 'T_STRING' || $token->token_name === 'T_CONSTANT_ENCAPSED_STRING') { $string = $token->token_value; return trim($string, "'\""); } } } return null; }
function injectDependencyArgumentIntoFile($class, $file, $propName = false) { $di_lines = (object) getDiLinesFromMage2ClassName($class, $propName); $di_lines->parameter = trim(trim($di_lines->parameter, ',')); $indent = getClassIndent(); $contents = file_get_contents($file); $tokens = pestle_token_get_all($contents); $has_constructor = arrayContainsConstructToken($tokens); if (!$has_constructor) { $tokens = insertConstrctorIntoPhpClassFileTokens($tokens); } $state = 0; $c = 0; $new_tokens = []; foreach ($tokens as $token) { $new_tokens[] = $token; if ($state === 0 && $token->token_name === 'T_CLASS') { $state = FOUND_CLASS_KEYWORD; } if ($state === FOUND_CLASS_KEYWORD && $token->token_value === '{') { $state = FOUND_OPENING_CLASS_BRACKET; $tmp = new stdClass(); //$tmp->token_value = "\n" . $indent . '#Property Here' . "\n"; $comment = $indent . '/**' . "\n" . $indent . '* Injected Dependency Description' . "\n" . $indent . '* ' . "\n" . $indent . '* @var \\' . $class . '' . "\n" . $indent . '*/' . "\n"; $tmp->token_value = "\n" . $comment . $indent . $di_lines->property . "\n"; $new_tokens[] = $tmp; } if ($state === FOUND_OPENING_CLASS_BRACKET && $token->token_value === '__construct') { $state = FOUND_CONSTRUCT; } if ($state === FOUND_CONSTRUCT && $token->token_value === ')') { $state = FOUND_CONSTRUCT_CLOSE_PAREN; $tmp = new stdClass(); $tmp->token_value = "\n" . $indent . $indent . $di_lines->parameter; $current_token = array_pop($new_tokens); $new_tokens = trimWhitespaceFromEndOfTokenArray($new_tokens); $new_tokens = addCommaIfSpoolBackwardsRevealsConstructorParam($new_tokens); $new_tokens[] = $tmp; $new_tokens[] = $current_token; } if ($state === FOUND_CONSTRUCT_CLOSE_PAREN && $token->token_value === '{') { $state = FOUND_CONSTRUCT_OPEN_BRACKET; $tmp = new stdClass(); // $tmp->token_value = "\n" . $indent . '#Property Assignment Here' . "\n"; $tmp->token_value = "\n" . $indent . $indent . $di_lines->assignment; $new_tokens[] = $tmp; } $c++; } $contents = implodeTokensIntoContents($new_tokens); output("Injecting {$class} into {$file}"); writeStringToFile($file, $contents); }
/** * This is a test * @command export_module * @argument module_file Which file? */ function pestle_cli($arguments) { $files = getFilesFromArguments($arguments); foreach ($files as $file) { $tokens = pestle_token_get_all(file_get_contents($file)); $tokens = replaceNamespacedFunction($tokens); $tokens = removePestleImports($tokens); $tokens = removePhpTag($tokens); $tokens = turnIntoBlockedNamespace($tokens); //collect names of all functions $string = getTokensAsString($tokens); // output("##PROCESSING: $file"); output($string); // output("##DONE PROCESSING: $file"); } }
function inProgressParsing() { $contents = file_get_contents('/Users/alanstorm/Documents/github_private/infortis-ultimo-magento2/app/code/Infortis/Brands/Block/Logo.php'); $tokens = pestle_token_get_all($contents); $namespace = parseNamespaceFromTokens($tokens); // var_dump($namespace); $uses = parseUsesFromTokens($tokens); // var_dump($uses); $classes = parseClassCodeFromTokens($tokens); }
/** * Injects a dependency into a class constructor * This command modifies a preexisting class, adding the provided * dependency to that class's property list, `__construct` parameters * list, and assignment list. * * pestle.phar generate_di app/code/Pulsestorm/Generate/Command/Example.php 'Magento\Catalog\Model\ProductFactory' * * @command generate_di * @argument file Which PHP class file are we injecting into? * @argument class Which class to inject? [Magento\Catalog\Model\ProductFactory] * */ function pestle_cli($argv) { defineStates(); $file = realpath($argv['file']); if (!$file) { exit("Could not find {$file}.\n"); } $class = $argv['class']; $di_lines = (object) getDiLinesFromMage2ClassName($class); $di_lines->parameter = trim(trim($di_lines->parameter, ',')); $indent = getClassIndent(); $contents = file_get_contents($file); $tokens = pestle_token_get_all($contents); $has_constructor = arrayContainsConstructToken($tokens); if (!$has_constructor) { $tokens = insertConstrctorIntoPhpClassFileTokens($tokens); } $state = 0; $c = 0; $new_tokens = []; foreach ($tokens as $token) { $new_tokens[] = $token; if ($state === 0 && $token->token_name === 'T_CLASS') { $state = FOUND_CLASS_KEYWORD; } if ($state === FOUND_CLASS_KEYWORD && $token->token_value === '{') { $state = FOUND_OPENING_CLASS_BRACKET; $tmp = new stdClass(); //$tmp->token_value = "\n" . $indent . '#Property Here' . "\n"; $tmp->token_value = "\n" . $indent . $di_lines->property . "\n"; $new_tokens[] = $tmp; } if ($state === FOUND_OPENING_CLASS_BRACKET && $token->token_value === '__construct') { $state = FOUND_CONSTRUCT; } if ($state === FOUND_CONSTRUCT && $token->token_value === ')') { $state = FOUND_CONSTRUCT_CLOSE_PAREN; $tmp = new stdClass(); $tmp->token_value = "\n" . $indent . $indent . $di_lines->parameter; $current_token = array_pop($new_tokens); $new_tokens = trimWhitespaceFromEndOfTokenArray($new_tokens); $new_tokens = addCommaIfSpoolBackwardsRevealsConstructorParam($new_tokens); $new_tokens[] = $tmp; $new_tokens[] = $current_token; } if ($state === FOUND_CONSTRUCT_CLOSE_PAREN && $token->token_value === '{') { $state = FOUND_CONSTRUCT_OPEN_BRACKET; $tmp = new stdClass(); // $tmp->token_value = "\n" . $indent . '#Property Assignment Here' . "\n"; $tmp->token_value = "\n" . $indent . $indent . $di_lines->assignment; $new_tokens[] = $tmp; } $c++; } $contents = implodeTokensIntoContents($new_tokens); output("Injecting {$class} into {$file}"); writeStringToFile($file, $contents); }
/** * Test Command * argument foobar @callback exampleOfACallback * @command fix_direct_om * @argument folder Folder to scan * @argument extensions File extensions? [php, phtml] */ function pestle_cli($arguments, $options) { output("TODO: When there's not an existing __construct"); output("TODO: When file doesn't exist"); output("TODO: Flag to ask if you want to replace a file"); output("TODO: Prop Name \\Foo\\Bar\\Splat\\Baz\\Boo ->barBazBoo"); defineStates(); define('TOKEN_BASELINE', 0); define('TOKEN_REMOVING_OM', 1); extract($arguments); $files = getFiles($folder, $extensions); foreach ($files as $file) { // output('.'); if (preg_match('%.bak.php%', $file)) { // output("{$file} looks like a backup, skipping."); continue; } // output($file); $tokensAll = pestle_token_get_all(file_get_contents($file)); $tokens = tokensFilterWhitespace($tokensAll); // getBaseConstructor($file, $tokens); $results = processFile($file, $tokensAll, $tokens); outputResults($results); //do the fixing validateResults($results); #performInjectionAndReplaceObjectManager($results, $tokensAll); } output("Done"); }