Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / league / container / src / Definition / ClassDefinition.php
diff --git a/vendor/league/container/src/Definition/ClassDefinition.php b/vendor/league/container/src/Definition/ClassDefinition.php
new file mode 100644 (file)
index 0000000..07448a2
--- /dev/null
@@ -0,0 +1,67 @@
+<?php
+
+namespace League\Container\Definition;
+
+use ReflectionClass;
+
+class ClassDefinition extends AbstractDefinition implements ClassDefinitionInterface
+{
+    /**
+     * @var array
+     */
+    protected $methods = [];
+
+    /**
+     * {@inheritdoc}
+     */
+    public function withMethodCall($method, array $args = [])
+    {
+        $this->methods[] = [
+            'method'    => $method,
+            'arguments' => $args
+        ];
+
+        return $this;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function withMethodCalls(array $methods = [])
+    {
+        foreach ($methods as $method => $args) {
+            $this->withMethodCall($method, $args);
+        }
+
+        return $this;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function build(array $args = [])
+    {
+        $args       = (empty($args)) ? $this->arguments : $args;
+        $resolved   = $this->resolveArguments($args);
+        $reflection = new ReflectionClass($this->concrete);
+        $instance   = $reflection->newInstanceArgs($resolved);
+
+        return $this->invokeMethods($instance);
+    }
+
+    /**
+     * Invoke methods on resolved instance.
+     *
+     * @param  object $instance
+     * @return object
+     */
+    protected function invokeMethods($instance)
+    {
+        foreach ($this->methods as $method) {
+            $args = $this->resolveArguments($method['arguments']);
+            call_user_func_array([$instance, $method['method']], $args);
+        }
+
+        return $instance;
+    }
+}