Exemplo n.º 1
0
 /**
  * @param Patch $patch
  * @param string $code
  * @return string
  */
 private function applyPatch(Patch $patch, $code)
 {
     $statements = $this->parser->parse($code);
     foreach ($patch->getInsertions() as $insertion) {
         try {
             $codeToInsert = $insertion->getCode();
             $codeToInsert = sprintf('<?php %s', preg_replace('/^\\s*<\\?php/', '', $codeToInsert));
             $additionalStatements = $this->parser->parse($codeToInsert);
         } catch (Error $e) {
             //we should probably log this and have a dev mode or something
             continue;
         }
         switch ($insertion->getType()) {
             case CodeInsertion::TYPE_BEFORE:
                 array_unshift($statements, ...$additionalStatements);
                 break;
             case CodeInsertion::TYPE_AFTER:
                 array_push($statements, ...$additionalStatements);
                 break;
         }
     }
     foreach ($patch->getTransformers() as $transformer) {
         $statements = $transformer($statements);
     }
     return $this->printer->prettyPrintFile($statements);
 }
Exemplo n.º 2
0
 public function testWithTransformer()
 {
     $patch = new Patch();
     $transformer = function (array $statements) {
         return $statements;
     };
     $new = $patch->withTransformer($transformer);
     $this->assertNotSame($patch, $new);
     $this->assertEmpty($patch->getTransformers());
     $this->assertEquals([$transformer], $new->getTransformers());
 }
Exemplo n.º 3
0
 /**
  * @param Patch $patch
  * @param string $code
  * @return string
  */
 private function applyPatch(Patch $patch, $code)
 {
     $statements = $this->parser->parse($code);
     foreach ($patch->getModifiers() as $modifier) {
         if ($modifier instanceof CodeInsertion) {
             $statements = $this->applyCodeInsertion($modifier, $statements);
             continue;
         }
         if (is_callable($modifier)) {
             $statements = $modifier($statements);
             continue;
         }
     }
     return $this->printer->prettyPrintFile($statements);
 }