Go\Instrument\Transformer\WeavingTransformer::transform PHP Method

transform() public method

This method may transform the supplied source and return a new replacement for it
public transform ( StreamMetaData $metadata ) : boolean
$metadata StreamMetaData Metadata for source
return boolean Return false if transformation should be stopped
    public function transform(StreamMetaData $metadata)
    {
        $totalTransformations = 0;
        $fileName = $metadata->uri;
        $astTree = ReflectionEngine::parseFile($fileName, $metadata->source);
        $parsedSource = new ReflectionFile($fileName, $astTree);
        // Check if we have some new aspects that weren't loaded yet
        $unloadedAspects = $this->aspectLoader->getUnloadedAspects();
        if (!empty($unloadedAspects)) {
            $this->loadAndRegisterAspects($unloadedAspects);
        }
        $advisors = $this->container->getByTag('advisor');
        $namespaces = $parsedSource->getFileNamespaces();
        $lineOffset = 0;
        foreach ($namespaces as $namespace) {
            $classes = $namespace->getClasses();
            foreach ($classes as $class) {
                // Skip interfaces and aspects
                if ($class->isInterface() || in_array(Aspect::class, $class->getInterfaceNames())) {
                    continue;
                }
                $wasClassProcessed = $this->processSingleClass($advisors, $metadata, $class, $lineOffset);
                $totalTransformations += (int) $wasClassProcessed;
            }
            $wasFunctionsProcessed = $this->processFunctions($advisors, $metadata, $namespace);
            $totalTransformations += (int) $wasFunctionsProcessed;
        }
        // If we return false this will indicate no more transformation for following transformers
        return $totalTransformations > 0;
    }

Usage Example

 /**
  * Testcase for multiple classes (@see https://github.com/lisachenko/go-aop-php/issues/71)
  */
 public function testMultipleClasses()
 {
     $this->metadata->source = $this->loadTest('multiple-classes');
     $this->transformer->transform($this->metadata);
     $actual = $this->normalizeWhitespaces($this->metadata->source);
     $expected = $this->normalizeWhitespaces($this->loadTest('multiple-classes-woven'));
     $this->assertEquals($expected, $actual);
 }