Overview

Namespaces

  • Deimos
    • Migrate
  • PHP

Classes

  • Deimos\Migrate\Migrate
  • Deimos\Migrate\Model
  • FilterIterator
  • IteratorIterator
  • RegexIterator

Interfaces

  • Iterator
  • OuterIterator
  • Traversable
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: namespace Deimos\Migrate;
  4: 
  5: use Deimos\Database\Database;
  6: use Deimos\ORM\Builder;
  7: use Deimos\ORM\ORM;
  8: use Deimos\ORM\Transaction;
  9: 
 10: class Migrate
 11: {
 12: 
 13:     /**
 14:      * @var ORM
 15:      */
 16:     protected $orm;
 17: 
 18:     /**
 19:      * @var string
 20:      */
 21:     protected $path;
 22: 
 23:     /**
 24:      * Migrate constructor.
 25:      *
 26:      * @param ORM $orm
 27:      */
 28:     public function __construct(ORM $orm)
 29:     {
 30:         $this->orm = $orm;
 31:         $this->init();
 32:     }
 33: 
 34:     /**
 35:      * init database
 36:      */
 37:     protected function init()
 38:     {
 39:         $this->orm->register('migrate', \Deimos\Migrate\Model::class);
 40:         $table = $this->orm->mapTable('migrate');
 41: 
 42:         $this->orm->database()->rawQuery("CREATE TABLE IF NOT EXISTS `{$table}` (
 43:             `id` INT(11) NOT NULL AUTO_INCREMENT,
 44:             `item` VARCHAR(400) NOT NULL,
 45:             PRIMARY KEY (`id`),
 46:             INDEX `itemIndex` (`item`)
 47:         )
 48:         COLLATE='utf8_general_ci'
 49:         ENGINE=InnoDB;");
 50:     }
 51: 
 52:     /**
 53:      * @param $path
 54:      */
 55:     public function setPath($path)
 56:     {
 57:         $this->path = str_replace('\\', '/', $path);
 58:     }
 59: 
 60:     /**
 61:      * @return \RegexIterator
 62:      */
 63:     public function scan()
 64:     {
 65:         $path = realpath($this->path);
 66: 
 67:         $recursiveDirectoryIterator = new \RecursiveDirectoryIterator($path);
 68: 
 69:         $iterator      = new \RecursiveIteratorIterator($recursiveDirectoryIterator, \RecursiveIteratorIterator::SELF_FIRST);
 70:         $regexIterator = new \RegexIterator($iterator, '/^.+\.sql$/i', \RecursiveRegexIterator::GET_MATCH);
 71: 
 72:         $files = iterator_to_array($regexIterator);
 73: 
 74:         uksort($files, function ($a, $b)
 75:         {
 76:             return strnatcmp($a, $b);
 77:         });
 78: 
 79:         return $files;
 80:     }
 81: 
 82:     /**
 83:      * @param $filename
 84:      *
 85:      * @return mixed
 86:      */
 87:     public function filename($filename)
 88:     {
 89:         $filename = str_replace('\\', '/', $filename);
 90:         $filename = str_replace($this->path, '', $filename);
 91: 
 92:         return trim($filename, '\\/');
 93:     }
 94: 
 95:     /**
 96:      * @param $filename
 97:      *
 98:      * @return bool
 99:      */
100:     public function isMake($filename)
101:     {
102:         return $this->orm->repository('migrate')
103:                 ->where('item', $filename)
104:                 ->count() > 0;
105:     }
106: 
107:     /**
108:      * @return array
109:      */
110:     public function run()
111:     {
112:         $result  = true;
113:         $storage = [];
114: 
115:         foreach ($this->scan() as $item => $value)
116:         {
117:             $path = realpath($item);
118: 
119:             $filename = $this->filename($item);
120: 
121:             if (!$this->isMake($filename))
122:             {
123:                 $transaction = $this->orm->database()->transaction();
124:                 $sql         = file_get_contents($path);
125:                 $result      = false;
126:                 $isSave      = 0;
127: 
128:                 $transaction->call(function (Database $database) use ($sql)
129:                 {
130:                     return $database->exec($sql);
131:                 });
132: 
133:                 if ($transaction->state() === \Deimos\Database\Transaction::STATE_COMMIT)
134:                 {
135:                     $isSave = $model = $this->orm->create(
136:                             'migrate',
137:                             ['item' => $filename]
138:                         )->id() > 0;
139:                 }
140: 
141:                 if (!$isSave)
142:                 {
143:                     $storage[] = $filename . ' -- error';
144:                     break;
145:                 }
146: 
147:                 $storage[] = $filename . ' -- commit';
148:             }
149:         }
150: 
151:         if ($result)
152:         {
153:             $storage[] = 'Already on latest version!';
154:         }
155: 
156:         return $storage;
157:     }
158: 
159: }
160: 
API documentation generated by ApiGen