| 1: | <?php
|
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: |
|
| 15: |
|
| 16: | namespace LucidFrame\Core;
|
| 17: |
|
| 18: | |
| 19: | |
| 20: |
|
| 21: | class Seeder
|
| 22: | {
|
| 23: |
|
| 24: | private $dbNamespace;
|
| 25: |
|
| 26: | private $path;
|
| 27: |
|
| 28: | private $data = array();
|
| 29: |
|
| 30: | private static $references = array();
|
| 31: |
|
| 32: | private $tables;
|
| 33: |
|
| 34: | |
| 35: | |
| 36: | |
| 37: |
|
| 38: | public function __construct($namespace = 'default')
|
| 39: | {
|
| 40: | $this->dbNamespace = $namespace;
|
| 41: | $this->path = DB . 'seed' . _DS_;
|
| 42: | }
|
| 43: |
|
| 44: | |
| 45: | |
| 46: | |
| 47: |
|
| 48: | public function setDbNamespace($namespace)
|
| 49: | {
|
| 50: | $this->dbNamespace = $namespace;
|
| 51: | }
|
| 52: |
|
| 53: | |
| 54: | |
| 55: | |
| 56: |
|
| 57: | public function getDbNamespace()
|
| 58: | {
|
| 59: | return $this->dbNamespace;
|
| 60: | }
|
| 61: |
|
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: |
|
| 67: | public static function getReference($key)
|
| 68: | {
|
| 69: | return __CLASS__ . '::' . $key;
|
| 70: | }
|
| 71: |
|
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: |
|
| 77: | public static function getReferenceValue($key)
|
| 78: | {
|
| 79: | return isset(self::$references[$key]) ? self::$references[$key] : null;
|
| 80: | }
|
| 81: |
|
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: |
|
| 87: | public function run(array $entities = array())
|
| 88: | {
|
| 89: | if ($this->load($entities)) {
|
| 90: |
|
| 91: | db_disableForeignKeyCheck();
|
| 92: |
|
| 93: | if (count($entities)) {
|
| 94: | $this->tables = array_filter($entities, function($table) {
|
| 95: | return in_array($table, $this->tables);
|
| 96: | });
|
| 97: | }
|
| 98: |
|
| 99: | foreach ($this->tables as $table) {
|
| 100: | db_truncate($table);
|
| 101: | }
|
| 102: |
|
| 103: | $tableDone = '';
|
| 104: |
|
| 105: |
|
| 106: | foreach ($this->data as $reference => $record) {
|
| 107: | if (!isset($record['__TABLE__'])) {
|
| 108: | continue;
|
| 109: | }
|
| 110: |
|
| 111: | $table = $record['__TABLE__'];
|
| 112: | unset($record['__TABLE__']);
|
| 113: |
|
| 114: | if ($table != $tableDone) {
|
| 115: | if ($tableDone) {
|
| 116: | _writeln('%s is seeded.', $tableDone);
|
| 117: | }
|
| 118: | $tableDone = $table;
|
| 119: | }
|
| 120: |
|
| 121: | $slug = null;
|
| 122: | $data = array();
|
| 123: | foreach ($record as $field => $value) {
|
| 124: | if ($field == 'slug') {
|
| 125: | $slug = $value;
|
| 126: | unset($record['slug']);
|
| 127: | }
|
| 128: |
|
| 129: |
|
| 130: | if (is_string($value) && strpos($value, __CLASS__ . '::') === 0) {
|
| 131: | $refKeyName = explode('::', $value);
|
| 132: | $refKey = end($refKeyName);
|
| 133: | $data[$field] = self::getReferenceValue($refKey);
|
| 134: | } else {
|
| 135: | $data[$field] = $value;
|
| 136: | }
|
| 137: | }
|
| 138: |
|
| 139: |
|
| 140: | if ($slug) {
|
| 141: | $data = array('slug' => $slug) + $data;
|
| 142: | }
|
| 143: |
|
| 144: | if ($insertId = db_insert($table, $data)) {
|
| 145: | self::$references[$reference] = $insertId;
|
| 146: | }
|
| 147: | }
|
| 148: |
|
| 149: | if ($tableDone) {
|
| 150: | _writeln('%s is seeded.', $tableDone);
|
| 151: | }
|
| 152: |
|
| 153: | db_enableForeignKeyCheck();
|
| 154: |
|
| 155: | return true;
|
| 156: | } else {
|
| 157: | return false;
|
| 158: | }
|
| 159: | }
|
| 160: |
|
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: |
|
| 166: | private function load(array $entities = array())
|
| 167: | {
|
| 168: | $_DB = _app('db');
|
| 169: |
|
| 170: | $entities = array_map(function($entity) {
|
| 171: | $entity .= '.php';
|
| 172: | return $entity;
|
| 173: | }, $entities);
|
| 174: |
|
| 175: | $dir = $this->path . $this->dbNamespace;
|
| 176: | if (is_dir($dir) && is_object($_DB)) {
|
| 177: | $seeding = array();
|
| 178: | $files = scandir($dir);
|
| 179: | foreach ($files as $fileName) {
|
| 180: | if (count($entities) && !in_array($fileName, $entities)) {
|
| 181: | continue;
|
| 182: | }
|
| 183: |
|
| 184: | $dir = rtrim(rtrim($dir, '/'), '\\');
|
| 185: | $file = $dir . _DS_ . $fileName;
|
| 186: |
|
| 187: | if ($fileName === '.' || $fileName === '..' || $fileName === '.gitkeep' || !is_file($file)) {
|
| 188: | continue;
|
| 189: | }
|
| 190: |
|
| 191: | $table = substr($fileName, 0, -4);
|
| 192: | if (file_exists($file) && $_DB->schemaManager->hasTable($table)) {
|
| 193: | $data = include($file);
|
| 194: | $order = $data['order'];
|
| 195: | unset($data['order']);
|
| 196: |
|
| 197: |
|
| 198: | array_walk($data, function (&$value, $key, $table) {
|
| 199: | $value = array('__TABLE__' => $table) + $value;
|
| 200: | }, $table);
|
| 201: |
|
| 202: | $seeding[$order] = $data;
|
| 203: | $this->tables[] = $table;
|
| 204: | }
|
| 205: | }
|
| 206: |
|
| 207: | ksort($seeding);
|
| 208: |
|
| 209: | foreach ($seeding as $data) {
|
| 210: | $this->data += $data;
|
| 211: | }
|
| 212: | }
|
| 213: |
|
| 214: | return (bool) count($this->data);
|
| 215: | }
|
| 216: | }
|
| 217: | |