예제 #1
0
 /** @inheritdoc */
 public static function tearDownAfterClass()
 {
     parent::tearDownAfterClass();
     //  Clean up: remove directory created for testing
     try {
         Disk::deleteTree(Disk::path([dirname(dirname(__DIR__)), 'storage', 'mount-test']));
     } catch (\Exception $_ex) {
         //  Ignored
     }
 }
 /** @inheritdoc */
 public function import($request)
 {
     $_from = null;
     $_instance = $request->getInstance();
     $_mount = $_instance->getStorageMount();
     $this->info('[provisioning:storage:import] instance "' . $_instance->instance_id_text . '" begin');
     //  Grab the target (zip archive) and pull out the target of the import
     $_zip = $request->getTarget();
     /** @var \ZipArchive $_archive */
     /** @noinspection PhpUndefinedMethodInspection */
     $_archive = $_zip->getAdapter()->getArchive();
     $_path = null;
     foreach ($_zip->listContents() as $_file) {
         if ('dir' != $_file['type'] && false !== strpos($_file['path'], '.storage.zip')) {
             $_from = Disk::segment([sys_get_temp_dir(), 'dfe', 'import', sha1($_file['path'])], true);
             if (!$_archive->extractTo($_from, $_file['path'])) {
                 throw new \RuntimeException('Unable to unzip archive file "' . $_file['path'] . '" from snapshot.');
             }
             $_path = Disk::path([$_from, $_file['path']], false);
             if (!$_path || !file_exists($_path)) {
                 throw new \InvalidArgumentException('$from file "' . $_file['path'] . '" missing or unreadable.');
             }
             $_from = new Filesystem(new ZipArchiveAdapter($_path));
             break;
         }
     }
     if (!$_mount instanceof Filesystem) {
         $_mount = new Filesystem(new ZipArchiveAdapter($_mount));
     }
     //  If "clean" == true, storage is wiped clean before restore
     if (true === $request->get('clean', false)) {
         $_mount->deleteDir('./');
     }
     //  Extract the files
     $_restored = [];
     /** @type Filesystem $_archive */
     foreach ($_from->listContents() as $_file) {
         $_filename = $_file['path'];
         if ('dir' == array_get($_file, 'type')) {
             $_mount->createDir($_filename);
         } else {
             $_mount->writeStream($_filename, $_archive->readStream($_filename));
         }
         $_restored[] = $_file;
     }
     unset($_from);
     $_path && is_dir(dirname($_path)) && Disk::deleteTree(dirname($_path));
     //  Fire off a "storage.imported" event...
     \Event::fire('dfe.storage.imported', [$this, $request]);
     $this->info('[provisioning:storage:import] instance "' . $_instance->instance_id_text . '" complete');
     return $_restored;
 }