Factory.php 7.0 KB

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