/** * Compile built classes * * @param $source Reflection_Source * @param $compiler PHP\Compiler * @return boolean */ public function compile(Reflection_Source $source, PHP\Compiler $compiler = null) { $builder = Builder::current(); $builder->build = false; $compiled = false; foreach ($source->getClasses() as $class) { $replacement = $builder->getComposition($class->name); if (is_array($replacement)) { foreach (Class_Builder::build($class->name, $replacement, true) as $built_source) { $compiler->addSource((new Reflection_Source())->setSource('<?php' . LF . $built_source)); $compiled = true; } } } $builder->build = true; return $compiled; }
/** * @param $source Reflection_Source the PHP source file object * @param $compiler PHP\Compiler the main compiler * @return boolean true if compilation process did something, else false */ public function compile(Reflection_Source $source, PHP\Compiler $compiler = null) { $compiled = false; foreach ($source->getClasses() as $class) { // replace extends with the built replacement class if (!Builder::isBuilt($class->name)) { $parent_class_name = $class->getParentName(); if ($parent_class_name) { $replacement_class_name = Builder::className($parent_class_name); if ($replacement_class_name != $parent_class_name) { $class_exists = class_exists($replacement_class_name, false); if ($class_exists && is_a($replacement_class_name, $parent_class_name) || !$class_exists && Reflection_Source::of($replacement_class_name)->getClass($replacement_class_name)->isA($class->name)) { $replacement_class_name = $parent_class_name; } } if (is_array($replacement_class_name)) { trigger_error("Replacement classes should all be compiled", E_USER_ERROR); $compiler->addSource($source); } elseif ($replacement_class_name !== $parent_class_name && Builder::isBuilt($replacement_class_name)) { $extended = null; $buffer = $source->getSource(); $short_class_name = Namespaces::shortClassName($class->name); $buffer = preg_replace_callback('%(\\s+class\\s+' . $short_class_name . '\\s+extends\\s+)([\\\\\\w]+)(\\s+)%', function ($match) use(&$extended, $replacement_class_name) { $extended = $match[2]; return $match[1] . BS . $replacement_class_name . $match[3]; }, $buffer); if ($extended) { $buffer = preg_replace_callback('%(\\n\\s+\\*\\s+@link\\s+)(' . str_replace(BS, BS . BS, $extended) . ')(\\s+)%', function ($match) use($replacement_class_name) { return $match[1] . BS . $replacement_class_name . $match[3]; }, $buffer); } $source->setSource($buffer); $compiled = true; } } } } return $compiled; }