LFT
  • Namespace
  • Class
  • Tree

Namespaces

  • Docoflow
    • Contracts
    • Entity
    • Facades
    • Models
    • Traits
  • Docolight
    • Agno
      • Traits
    • Container
    • Http
      • Contracts
    • Rest
      • Handler
      • Http
    • Support
      • Debug
      • Traits
  • Docotory
    • Traits
  • None

Classes

  • Docoflow\Docoflow
  • Docoflow\Entity\Group
  • Docoflow\Entity\Step
  • Docoflow\Entity\Verificator
  • Docoflow\Facades\Action
  • Docoflow\Facades\Activity
  • Docoflow\Facades\Flo
  • Docoflow\Facades\StateActivity
  • Docoflow\Flo
  • Docoflow\Models\Workflow
  • Docoflow\Models\WorkflowAction
  • Docoflow\Models\WorkflowActivity
  • Docoflow\Models\WorkflowGroups
  • Docoflow\Models\WorkflowNotification
  • Docoflow\Models\WorkflowState
  • Docoflow\Models\WorkflowStateActivity
  • Docoflow\Models\WorkflowStep
  • Docoflow\Models\WorkflowVerificator
  • Docolight
  • Docolight\Agno\AgnoModule
  • Docolight\Container\Container
  • Docolight\Http\Headers
  • Docolight\Http\JsonResponse
  • Docolight\Http\MimeResponse
  • Docolight\Http\Response
  • Docolight\Http\ResponseFactory
  • Docolight\Rest\Handler\RestfulErrorHandler
  • Docolight\Rest\Http\RestFulController
  • Docolight\Support\ActiveRecordWrapper
  • Docolight\Support\Arr
  • Docolight\Support\Carbonate
  • Docolight\Support\ClassLoader
  • Docolight\Support\Collection
  • Docolight\Support\CollectionDataProvider
  • Docolight\Support\Debug\Dumper
  • Docolight\Support\Debug\HtmlDumper
  • Docolight\Support\Facade
  • Docolight\Support\Factory
  • Docolight\Support\Fluent
  • Docolight\Support\Html
  • Docolight\Support\IterablePager
  • Docolight\Support\Repository
  • Docolight\Support\Set
  • Docolight\Support\Str
  • Docotory\Factory

Interfaces

  • Docoflow\Contracts\DocoflowContract
  • Docoflow\Contracts\ValidationStatus
  • Docolight\Http\Contracts\Arrayable

Traits

  • Docoflow\Traits\BulkValidator
  • Docoflow\Traits\Entity
  • Docoflow\Traits\HasMutator
  • Docoflow\Traits\Validable
  • Docolight\Agno\Traits\HasAssetsUrl
  • Docolight\Agno\Traits\HasAutoload
  • Docolight\Support\Traits\Macroable
  • Docotory\Traits\HasFactories

Exceptions

  • Docolight\Container\BindingResolutionException
  • Docotory\ResolvingTypeException

Functions

  • array_add
  • array_build
  • array_collapse
  • array_divide
  • array_dot
  • array_except
  • array_first
  • array_flatten
  • array_forget
  • array_get
  • array_has
  • array_last
  • array_only
  • array_pluck
  • array_pull
  • array_replace_value
  • array_set
  • array_sort
  • array_sort_recursive
  • array_where
  • cache
  • camel_case
  • class_basename
  • class_uses_recursive
  • collect
  • container
  • data_get
  • dd
  • def
  • dump
  • e
  • ends_with
  • fluent
  • get
  • head
  • input
  • last
  • object_get
  • preg_replace_sub
  • request
  • response
  • session
  • snake_case
  • starts_with
  • str_contains
  • str_finish
  • str_is
  • str_limit
  • str_random
  • str_replace_array
  • str_slug
  • studly_case
  • title_case
  • trait_uses_recursive
  • transaction
  • trimtolower
  • value
  • with
  1 <?php
  2 
  3 namespace Docolight\Support;
  4 
  5 use Closure;
  6 use InvalidArgumentException;
  7 use Docolight\Container\Container;
  8 
  9 /**
 10  * In object-oriented programming, a factory is an object for creating other objects – formally a factory is simply an object that returns an object from some method call, which is assumed to be "new". More broadly, a subroutine that returns a "new" object may be referred to as a "factory", as in factory method or factory function. This is a basic concept in OOP, and forms the basis for a number of related software design patterns.
 11  *
 12  * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
 13  *
 14  * @link https://en.wikipedia.org/wiki/Factory_(object-oriented_programming) Factory (object-oriented programming)
 15  */
 16 abstract class Factory
 17 {
 18     /**
 19      * The container instance.
 20      *
 21      * @var
 22      */
 23     protected $container;
 24 
 25     /**
 26      * The registered custom product creators.
 27      *
 28      * @var array
 29      */
 30     protected $customCreators = [];
 31 
 32     /**
 33      * The array of created "products".
 34      *
 35      * @var array
 36      */
 37     protected $products = [];
 38 
 39     /**
 40      * Create a new manager instance.
 41      *
 42      * @param  Container $container
 43      * @return void
 44      */
 45     public function __construct(Container $container)
 46     {
 47         $this->container = $container;
 48     }
 49 
 50     /**
 51      * Get the default product name.
 52      *
 53      * @return string
 54      */
 55     abstract public function getDefaultProduct();
 56 
 57     /**
 58      * Get a product instance.
 59      *
 60      * @param  string  $product
 61      * @return mixed
 62      */
 63     public function produce($product = null)
 64     {
 65         $product = $product ?: $this->getDefaultProduct();
 66         $identifier = camel_case($product);
 67 
 68         // If the given product has not been created before, we will create the instances
 69         // here and cache it so we can return it next time very quickly. If there is
 70         // already a product created by this name, we'll just return that instance.
 71         if (!isset($this->products[$identifier])) {
 72             $this->products[$identifier] = $this->createProduct($product);
 73         }
 74 
 75         return $this->products[$identifier];
 76     }
 77 
 78     /**
 79      * Create a new product instance.
 80      *
 81      * @param  string  $product
 82      * @return mixed
 83      *
 84      * @throws \InvalidArgumentException
 85      */
 86     protected function createProduct($product)
 87     {
 88         $method = $this->getMethod($product);
 89 
 90         // We'll check to see if a creator method exists for the given product. If not we
 91         // will check for a custom product creator, which allows developers to create
 92         // products using their own customized product creator Closure to create it.
 93         if (isset($this->customCreators[$product])) {
 94             return $this->callCustomCreator($product);
 95         } elseif (method_exists($this, $method)) {
 96             return $this->$method();
 97         }
 98 
 99         throw new InvalidArgumentException("Product [$product] not supported.");
100     }
101 
102     /**
103      * Get method to create product.
104      *
105      * @param string $product
106      *
107      * @return string
108      */
109     protected function getMethod($product)
110     {
111         return 'create'.ucfirst($product).'Product';
112     }
113 
114     /**
115      * Call a custom product creator.
116      *
117      * @param  string  $product
118      * @return mixed
119      */
120     protected function callCustomCreator($product)
121     {
122         $productClosure = $this->customCreators[$product];
123 
124         return $productClosure($this->container);
125     }
126 
127     /**
128      * Register a custom product creator Closure.
129      *
130      * @param  string    $product
131      * @param  \Closure  $callback
132      * @return $this
133      */
134     public function extend($product, Closure $callback)
135     {
136         $this->customCreators[$product] = $callback;
137 
138         return $this;
139     }
140 
141     /**
142      * Dynamically call the default product instance.
143      *
144      * @param  string  $method
145      * @param  array   $parameters
146      * @return mixed
147      */
148     public function __call($method, $parameters)
149     {
150         return call_user_func_array(array($this->produce(), $method), $parameters);
151     }
152 }
153 
LFT API documentation generated by ApiGen