Example #1
0
 /**
  * Update database schema according to models metadata.
  *
  * @param string|null $model   Model name to update. Example: \Test\Model\Class.
  * @param bool        $cleanup Cleanup database? Drop not related tables.
  *
  * @return void
  */
 public function updateAction($model = null, $cleanup = false)
 {
     $schema = new Schema($this->getDI());
     if ($model) {
         if (!class_exists($model)) {
             print ConsoleUtil::error('Model with class "' . $model . '" doesn\'t exists.') . PHP_EOL;
             return;
         }
         $count = current($schema->updateTable($model));
         if ($count) {
             print ConsoleUtil::headLine('Table update for model: ' . $model);
             print ConsoleUtil::commandLine('Executed queries:', $count, ConsoleUtil::FG_CYAN);
         } else {
             print ConsoleUtil::success('Table is up to date');
         }
         print PHP_EOL;
     } else {
         $queriesCount = $schema->updateDatabase($cleanup);
         if (!empty($queriesCount)) {
             print ConsoleUtil::headLine('Database update:');
             foreach ($queriesCount as $model => $count) {
                 print ConsoleUtil::commandLine($model . ':', $count, ConsoleUtil::FG_CYAN);
             }
         } else {
             print ConsoleUtil::success('Database is up to date');
         }
         print PHP_EOL;
     }
 }
Example #2
0
 /**
  * Database installation step.
  *
  * @return mixed
  *
  * @Route("/database", methods={"GET", "POST"}, name="install-database")
  */
 public function databaseAction()
 {
     if (!$this->_isPassed('requirementsAction') || $this->_isPassed('databaseAction')) {
         return $this->_selectAction();
     }
     $form = new DatabaseForm();
     if ($this->request->isPost() && $form->isValid($this->request->getPost())) {
         $data = $form->getValues();
         try {
             $connectionSettings = ["adapter" => $data['adapter'], "host" => $data['host'], "port" => $data['port'], "username" => $data['username'], "password" => $data['password'], "dbname" => $data['dbname']];
             $this->_setupDatabase($connectionSettings);
             // Install schema.
             $schema = new Schema($this->di);
             $schema->updateDatabase();
             // Run modules installation scripts.
             $packageManager = new PackageManager([], $this->di);
             foreach ($this->di->get('registry')->modules as $moduleName) {
                 $packageManager->runInstallScript(new Config(['name' => $moduleName, 'type' => PackageManager::PACKAGE_TYPE_MODULE, 'currentVersion' => '0', 'isUpdate' => false]));
             }
             $this->config->save('database');
             $this->_setPassed(__FUNCTION__, true);
         } catch (\Exception $ex) {
             $form->addError($ex->getMessage());
         }
         if ($this->_isPassed(__FUNCTION__)) {
             return $this->_selectAction();
         }
     }
     $this->view->form = $form;
 }
 /**
  * Uninstall package.
  *
  * @param string $type   Package type.
  * @param string $name   Package name.
  * @param string $return Return to.
  *
  * @return mixed
  *
  * @Route(
  * "/uninstall/{type:[a-zA-Z0-9_-]+}/{name:[a-zA-Z0-9_-]+}/{return:[a-zA-Z0-9_-]+}",
  * methods={"GET"},
  * name="admin-packages-uninstall"
  * )
  */
 public function uninstallAction($type, $name, $return)
 {
     $this->view->disable();
     $package = $this->_getPackage($type, $name);
     if ($package) {
         if ($this->_hasDependencies($package)) {
             return $this->response->redirect(['for' => $return]);
         }
         try {
             if ($package->type == Manager::PACKAGE_TYPE_MODULE) {
                 $installerClass = ucfirst($name) . '\\Installer';
                 if (class_exists($installerClass)) {
                     $packageInstaller = new $installerClass($this->di, $name);
                     if (method_exists($packageInstaller, 'remove')) {
                         $packageInstaller->remove();
                     }
                 }
             }
             $packageManager = new Manager();
             $packageManager->removePackage($package);
             $package->delete();
             $this->_removePackageConfig($package);
             $this->_updateMetadata();
             $this->_clearCache();
             // Update database.
             $schema = new Schema($this->getDI());
             $schema->updateDatabase(true);
             $this->flashSession->success('Package "' . $name . '" removed!');
         } catch (PackageException $e) {
             $package->delete();
             $this->flashSession->notice('Failed to remove package directory, check logs...');
         }
     } else {
         $this->flashSession->notice('Package not found...');
     }
     return $this->response->redirect(['for' => $return]);
 }