Example #1
0
/**
* Short Description
* Long
* Description
* @command convert_class
*/
function pestle_cli($argv)
{
    $type = input("Which type (model, helper, block)?", 'model');
    $alias = input("Which alias?", 'pulsestorm_helloworld/observer_newsletter');
    $path_config = input("Which config.xml?", 'app/code/community/Pulsestorm/Helloworld/etc/config.xml');
    $config = simplexml_load_file($path_config);
    $class = resolveAlias($alias, $config, $type);
    // output($class);
    $mage_1_path = getMage1ClassPathFromConfigPathAndMage2ClassName($path_config, $class);
    $mage_2_path = str_replace(['/core', '/community', '/local'], '', $mage_1_path);
    output('');
    output("New Class Path");
    output('-----------------------');
    output($mage_2_path);
    output('');
    output("New Class Content");
    output('-----------------------');
    output(convertMageOneClassIntoNamespacedClass($mage_1_path));
    output('');
    output("DI Lines");
    output('-----------------------');
    output(implode("\n", getDiLinesFromMage2ClassName($class)));
    output('');
}
Example #2
0
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);
}
Example #3
0
/**
* 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);
}