Factory.php 6.9 KB

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