Factory.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. namespace Illuminate\Database\Eloquent;
  3. use ArrayAccess;
  4. use Faker\Generator as Faker;
  5. use Symfony\Component\Finder\Finder;
  6. class Factory implements ArrayAccess
  7. {
  8. /**
  9. * The model definitions in the container.
  10. *
  11. * @var array
  12. */
  13. protected $definitions = [];
  14. /**
  15. * The registered model states.
  16. *
  17. * @var array
  18. */
  19. protected $states = [];
  20. /**
  21. * The registered after making callbacks.
  22. *
  23. * @var array
  24. */
  25. protected $afterMaking = [];
  26. /**
  27. * The registered after creating callbacks.
  28. *
  29. * @var array
  30. */
  31. protected $afterCreating = [];
  32. /**
  33. * The Faker instance for the builder.
  34. *
  35. * @var \Faker\Generator
  36. */
  37. protected $faker;
  38. /**
  39. * Create a new factory instance.
  40. *
  41. * @param \Faker\Generator $faker
  42. * @return void
  43. */
  44. public function __construct(Faker $faker)
  45. {
  46. $this->faker = $faker;
  47. }
  48. /**
  49. * Create a new factory container.
  50. *
  51. * @param \Faker\Generator $faker
  52. * @param string|null $pathToFactories
  53. * @return static
  54. */
  55. public static function construct(Faker $faker, $pathToFactories = null)
  56. {
  57. $pathToFactories = $pathToFactories ?: database_path('factories');
  58. return (new static($faker))->load($pathToFactories);
  59. }
  60. /**
  61. * Define a class with a given short-name.
  62. *
  63. * @param string $class
  64. * @param string $name
  65. * @param callable $attributes
  66. * @return $this
  67. */
  68. public function defineAs($class, $name, callable $attributes)
  69. {
  70. return $this->define($class, $attributes, $name);
  71. }
  72. /**
  73. * Define a class with a given set of attributes.
  74. *
  75. * @param string $class
  76. * @param callable $attributes
  77. * @param string $name
  78. * @return $this
  79. */
  80. public function define($class, callable $attributes, $name = 'default')
  81. {
  82. $this->definitions[$class][$name] = $attributes;
  83. return $this;
  84. }
  85. /**
  86. * Define a state with a given set of attributes.
  87. *
  88. * @param string $class
  89. * @param string $state
  90. * @param callable|array $attributes
  91. * @return $this
  92. */
  93. public function state($class, $state, $attributes)
  94. {
  95. $this->states[$class][$state] = $attributes;
  96. return $this;
  97. }
  98. /**
  99. * Define a callback to run after making a model.
  100. *
  101. * @param string $class
  102. * @param callable $callback
  103. * @param string $name
  104. * @return $this
  105. */
  106. public function afterMaking($class, callable $callback, $name = 'default')
  107. {
  108. $this->afterMaking[$class][$name][] = $callback;
  109. return $this;
  110. }
  111. /**
  112. * Define a callback to run after making a model with given state.
  113. *
  114. * @param string $class
  115. * @param string $state
  116. * @param callable $callback
  117. * @return $this
  118. */
  119. public function afterMakingState($class, $state, callable $callback)
  120. {
  121. return $this->afterMaking($class, $callback, $state);
  122. }
  123. /**
  124. * Define a callback to run after creating a model.
  125. *
  126. * @param string $class
  127. * @param callable $callback
  128. * @param string $name
  129. * @return $this
  130. */
  131. public function afterCreating($class, callable $callback, $name = 'default')
  132. {
  133. $this->afterCreating[$class][$name][] = $callback;
  134. return $this;
  135. }
  136. /**
  137. * Define a callback to run after creating a model with given state.
  138. *
  139. * @param string $class
  140. * @param string $state
  141. * @param callable $callback
  142. * @return $this
  143. */
  144. public function afterCreatingState($class, $state, callable $callback)
  145. {
  146. return $this->afterCreating($class, $callback, $state);
  147. }
  148. /**
  149. * Create an instance of the given model and persist it to the database.
  150. *
  151. * @param string $class
  152. * @param array $attributes
  153. * @return mixed
  154. */
  155. public function create($class, array $attributes = [])
  156. {
  157. return $this->of($class)->create($attributes);
  158. }
  159. /**
  160. * Create an instance of the given model and type and persist it to the database.
  161. *
  162. * @param string $class
  163. * @param string $name
  164. * @param array $attributes
  165. * @return mixed
  166. */
  167. public function createAs($class, $name, array $attributes = [])
  168. {
  169. return $this->of($class, $name)->create($attributes);
  170. }
  171. /**
  172. * Create an instance of the given model.
  173. *
  174. * @param string $class
  175. * @param array $attributes
  176. * @return mixed
  177. */
  178. public function make($class, array $attributes = [])
  179. {
  180. return $this->of($class)->make($attributes);
  181. }
  182. /**
  183. * Create an instance of the given model and type.
  184. *
  185. * @param string $class
  186. * @param string $name
  187. * @param array $attributes
  188. * @return mixed
  189. */
  190. public function makeAs($class, $name, array $attributes = [])
  191. {
  192. return $this->of($class, $name)->make($attributes);
  193. }
  194. /**
  195. * Get the raw attribute array for a given named model.
  196. *
  197. * @param string $class
  198. * @param string $name
  199. * @param array $attributes
  200. * @return array
  201. */
  202. public function rawOf($class, $name, array $attributes = [])
  203. {
  204. return $this->raw($class, $attributes, $name);
  205. }
  206. /**
  207. * Get the raw attribute array for a given model.
  208. *
  209. * @param string $class
  210. * @param array $attributes
  211. * @param string $name
  212. * @return array
  213. */
  214. public function raw($class, array $attributes = [], $name = 'default')
  215. {
  216. return array_merge(
  217. call_user_func($this->definitions[$class][$name], $this->faker), $attributes
  218. );
  219. }
  220. /**
  221. * Create a builder for the given model.
  222. *
  223. * @param string $class
  224. * @param string $name
  225. * @return \Illuminate\Database\Eloquent\FactoryBuilder
  226. */
  227. public function of($class, $name = 'default')
  228. {
  229. return new FactoryBuilder(
  230. $class, $name, $this->definitions, $this->states,
  231. $this->afterMaking, $this->afterCreating, $this->faker
  232. );
  233. }
  234. /**
  235. * Load factories from path.
  236. *
  237. * @param string $path
  238. * @return $this
  239. */
  240. public function load($path)
  241. {
  242. $factory = $this;
  243. if (is_dir($path)) {
  244. foreach (Finder::create()->files()->name('*.php')->in($path) as $file) {
  245. require $file->getRealPath();
  246. }
  247. }
  248. return $factory;
  249. }
  250. /**
  251. * Determine if the given offset exists.
  252. *
  253. * @param string $offset
  254. * @return bool
  255. */
  256. public function offsetExists($offset)
  257. {
  258. return isset($this->definitions[$offset]);
  259. }
  260. /**
  261. * Get the value of the given offset.
  262. *
  263. * @param string $offset
  264. * @return mixed
  265. */
  266. public function offsetGet($offset)
  267. {
  268. return $this->make($offset);
  269. }
  270. /**
  271. * Set the given offset to the given value.
  272. *
  273. * @param string $offset
  274. * @param callable $value
  275. * @return void
  276. */
  277. public function offsetSet($offset, $value)
  278. {
  279. return $this->define($offset, $value);
  280. }
  281. /**
  282. * Unset the value at the given offset.
  283. *
  284. * @param string $offset
  285. * @return void
  286. */
  287. public function offsetUnset($offset)
  288. {
  289. unset($this->definitions[$offset]);
  290. }
  291. }