Generator.php 11.9 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Zend\Di\ServiceLocator;

use Zend\Code\Generator\ClassGenerator;
use Zend\Code\Generator\FileGenerator;
use Zend\Code\Generator\MethodGenerator;
use Zend\Code\Generator\ParameterGenerator;
use Zend\Di\Di;
use Zend\Di\Exception;

/**
 * Generator that creates the body of a service locator that can emulate the logic of the given Zend\Di\Di instance
 * without class definitions
 */
class Generator
{
    protected $containerClass = 'ApplicationContext';

    /** @var DependencyInjectorProxy */
    protected $injector;

    /**
     * @var null|string
     */
    protected $namespace;

    /**
     * Constructor
     *
     * Requires a DependencyInjection manager on which to operate.
     *
     * @param Di $injector
     */
    public function __construct(Di $injector)
    {
        $this->injector = new DependencyInjectorProxy($injector);
    }

    /**
     * Set the class name for the generated service locator container
     *
     * @param  string    $name
     * @return Generator
     */
    public function setContainerClass($name)
    {
        $this->containerClass = $name;

        return $this;
    }

    /**
     * Set the namespace to use for the generated class file
     *
     * @param  string    $namespace
     * @return Generator
     */
    public function setNamespace($namespace)
    {
        $this->namespace = $namespace;

        return $this;
    }

    /**
     * Construct, configure, and return a PHP class file code generation object
     *
     * Creates a Zend\Code\Generator\FileGenerator object that has
     * created the specified class and service locator methods.
     *
     * @param  null|string                         $filename
     * @throws \Zend\Di\Exception\RuntimeException
     * @return FileGenerator
     */
    public function getCodeGenerator($filename = null)
    {
        $injector       = $this->injector;
        $im             = $injector->instanceManager();
        $indent         = '    ';
        $aliases        = $this->reduceAliases($im->getAliases());
        $caseStatements = [];
        $getters        = [];
        $definitions    = $injector->definitions();

        $fetched = array_unique(array_merge($definitions->getClasses(), $im->getAliases()));

        foreach ($fetched as $name) {
            $getter = $this->normalizeAlias($name);
            $meta   = $injector->get($name);
            $params = $meta->getParams();

            // Build parameter list for instantiation
            foreach ($params as $key => $param) {
                if (null === $param || is_scalar($param) || is_array($param)) {
                    $string = var_export($param, 1);
                    if (strstr($string, '::__set_state(')) {
                        throw new Exception\RuntimeException('Arguments in definitions may not contain objects');
                    }
                    $params[$key] = $string;
                } elseif ($param instanceof GeneratorInstance) {
                    /* @var $param GeneratorInstance */
                    $params[$key] = sprintf('$this->%s()', $this->normalizeAlias($param->getName()));
                } else {
                    $message = sprintf('Unable to use object arguments when building containers. Encountered with "%s", parameter of type "%s"', $name, get_class($param));
                    throw new Exception\RuntimeException($message);
                }
            }

            // Strip null arguments from the end of the params list
            $reverseParams = array_reverse($params, true);
            foreach ($reverseParams as $key => $param) {
                if ('NULL' === $param) {
                    unset($params[$key]);
                    continue;
                }
                break;
            }

            // Create instantiation code
            $constructor = $meta->getConstructor();
            if ('__construct' != $constructor) {
                // Constructor callback
                $callback = var_export($constructor, 1);
                if (strstr($callback, '::__set_state(')) {
                    throw new Exception\RuntimeException('Unable to build containers that use callbacks requiring object instances');
                }
                if (count($params)) {
                    $creation = sprintf('$object = call_user_func(%s, %s);', $callback, implode(', ', $params));
                } else {
                    $creation = sprintf('$object = call_user_func(%s);', $callback);
                }
            } else {
                // Normal instantiation
                $className = '\\' . ltrim($name, '\\');
                $creation = sprintf('$object = new %s(%s);', $className, implode(', ', $params));
            }

            // Create method call code
            $methods = '';
            foreach ($meta->getMethods() as $methodData) {
                if (!isset($methodData['name']) && !isset($methodData['method'])) {
                    continue;
                }
                $methodName   = isset($methodData['name']) ? $methodData['name'] : $methodData['method'];
                $methodParams = $methodData['params'];

                // Create method parameter representation
                foreach ($methodParams as $key => $param) {
                    if (null === $param || is_scalar($param) || is_array($param)) {
                        $string = var_export($param, 1);
                        if (strstr($string, '::__set_state(')) {
                            throw new Exception\RuntimeException('Arguments in definitions may not contain objects');
                        }
                        $methodParams[$key] = $string;
                    } elseif ($param instanceof GeneratorInstance) {
                        $methodParams[$key] = sprintf('$this->%s()', $this->normalizeAlias($param->getName()));
                    } else {
                        $message = sprintf('Unable to use object arguments when generating method calls. Encountered with class "%s", method "%s", parameter of type "%s"', $name, $methodName, get_class($param));
                        throw new Exception\RuntimeException($message);
                    }
                }

                // Strip null arguments from the end of the params list
                $reverseParams = array_reverse($methodParams, true);
                foreach ($reverseParams as $key => $param) {
                    if ('NULL' === $param) {
                        unset($methodParams[$key]);
                        continue;
                    }
                    break;
                }

                $methods .= sprintf("\$object->%s(%s);\n", $methodName, implode(', ', $methodParams));
            }

            // Generate caching statement
            $storage = '';
            if ($im->hasSharedInstance($name, $params)) {
                $storage = sprintf("\$this->services['%s'] = \$object;\n", $name);
            }

            // Start creating getter
            $getterBody = '';

            // Create fetch of stored service
            if ($im->hasSharedInstance($name, $params)) {
                $getterBody .= sprintf("if (isset(\$this->services['%s'])) {\n", $name);
                $getterBody .= sprintf("%sreturn \$this->services['%s'];\n}\n\n", $indent, $name);
            }

            // Creation and method calls
            $getterBody .= sprintf("%s\n", $creation);
            $getterBody .= $methods;

            // Stored service
            $getterBody .= $storage;

            // End getter body
            $getterBody .= "return \$object;\n";

            $getterDef = new MethodGenerator();
            $getterDef->setName($getter);
            $getterDef->setBody($getterBody);
            $getters[] = $getterDef;

            // Get cases for case statements
            $cases = [$name];
            if (isset($aliases[$name])) {
                $cases = array_merge($aliases[$name], $cases);
            }

            // Build case statement and store
            $statement = '';
            foreach ($cases as $value) {
                $statement .= sprintf("%scase '%s':\n", $indent, $value);
            }
            $statement .= sprintf("%sreturn \$this->%s();\n", str_repeat($indent, 2), $getter);

            $caseStatements[] = $statement;
        }

        // Build switch statement
        $switch  = sprintf("switch (%s) {\n%s\n", '$name', implode("\n", $caseStatements));
        $switch .= sprintf("%sdefault:\n%sreturn parent::get(%s, %s);\n", $indent, str_repeat($indent, 2), '$name', '$params');
        $switch .= "}\n\n";

        // Build get() method
        $nameParam   = new ParameterGenerator();
        $nameParam->setName('name');
        $paramsParam = new ParameterGenerator();
        $paramsParam->setName('params')
                    ->setType('array')
                    ->setDefaultValue([]);

        $get = new MethodGenerator();
        $get->setName('get');
        $get->setParameters([
            $nameParam,
            $paramsParam,
        ]);
        $get->setBody($switch);

        // Create getters for aliases
        $aliasMethods = [];
        foreach ($aliases as $class => $classAliases) {
            foreach ($classAliases as $alias) {
                $aliasMethods[] = $this->getCodeGenMethodFromAlias($alias, $class);
            }
        }

        // Create class code generation object
        $container = new ClassGenerator();
        $container->setName($this->containerClass)
                  ->setExtendedClass('ServiceLocator')
                  ->addMethodFromGenerator($get)
                  ->addMethods($getters)
                  ->addMethods($aliasMethods);

        // Create PHP file code generation object
        $classFile = new FileGenerator();
        $classFile->setUse('Zend\Di\ServiceLocator')
                  ->setClass($container);

        if (null !== $this->namespace) {
            $classFile->setNamespace($this->namespace);
        }

        if (null !== $filename) {
            $classFile->setFilename($filename);
        }

        return $classFile;
    }

    /**
     * Reduces aliases
     *
     * Takes alias list and reduces it to a 2-dimensional array of
     * class names pointing to an array of aliases that resolve to
     * it.
     *
     * @param  array $aliasList
     * @return array
     */
    protected function reduceAliases(array $aliasList)
    {
        $reduced = [];
        $aliases = array_keys($aliasList);
        foreach ($aliasList as $alias => $service) {
            if (in_array($service, $aliases)) {
                do {
                    $service = $aliasList[$service];
                } while (in_array($service, $aliases));
            }
            if (!isset($reduced[$service])) {
                $reduced[$service] = [];
            }
            $reduced[$service][] = $alias;
        }

        return $reduced;
    }

    /**
     * Create a PhpMethod code generation object named after a given alias
     *
     * @param  string          $alias
     * @param  string          $class Class to which alias refers
     * @return MethodGenerator
     */
    protected function getCodeGenMethodFromAlias($alias, $class)
    {
        $alias = $this->normalizeAlias($alias);
        $method = new MethodGenerator();
        $method->setName($alias);
        $method->setBody(sprintf('return $this->get(\'%s\');', $class));

        return $method;
    }

    /**
     * Normalize an alias to a getter method name
     *
     * @param  string $alias
     * @return string
     */
    protected function normalizeAlias($alias)
    {
        $normalized = preg_replace('/[^a-zA-Z0-9]/', ' ', $alias);
        $normalized = 'get' . str_replace(' ', '', ucwords($normalized));

        return $normalized;
    }
}