Pagekit\Installer\Installer::install PHP Метод

install() публичный Метод

public install ( $config = [], $option = [], $user = [] )
    public function install($config = [], $option = [], $user = [])
    {
        $status = $this->check($config);
        $message = $status['message'];
        $status = $status['status'];
        try {
            if ('no-connection' == $status) {
                $this->app->abort(400, __('No database connection.'));
            }
            if ('tables-exist' == $status) {
                $this->app->abort(400, $message);
            }
            $scripts = new PackageScripts($this->app->path() . '/app/system/scripts.php');
            $scripts->install();
            $this->app->db()->insert('@system_user', ['name' => $user['username'], 'username' => $user['username'], 'password' => $this->app->get('auth.password')->hash($user['password']), 'status' => 1, 'email' => $user['email'], 'registered' => date('Y-m-d H:i:s'), 'roles' => '2,3']);
            $option['system']['version'] = $this->app->version();
            foreach ($option as $name => $values) {
                $this->app->config()->set($name, $this->app->config($name)->merge($values));
            }
            $packageManager = new PackageManager(new NullOutput());
            foreach (glob($this->app->get('path.packages') . '/*/*/composer.json') as $package) {
                $package = $this->app->package()->load($package);
                if ($package->get('type') === 'pagekit-extension' || $package->get('type') === 'pagekit-theme') {
                    $packageManager->enable($package);
                }
            }
            if (file_exists(__DIR__ . '/../install.php')) {
                require_once __DIR__ . '/../install.php';
            }
            if (!$this->config) {
                $configuration = new Config();
                $configuration->set('application.debug', false);
                foreach ($config as $key => $value) {
                    $configuration->set($key, $value);
                }
                $configuration->set('system.secret', $this->app->get('auth.random')->generateString(64));
                if (!file_put_contents($this->configFile, $configuration->dump())) {
                    $status = 'write-failed';
                    $this->app->abort(400, __('Can\'t write config.'));
                }
            }
            $this->app->module('system/cache')->clearCache();
            $status = 'success';
        } catch (DBALException $e) {
            $status = 'db-sql-failed';
            $message = __('Database error: %error%', ['%error%' => $e->getMessage()]);
        } catch (\Exception $e) {
            $message = $e->getMessage();
        }
        return ['status' => $status, 'message' => $message];
    }

Usage Example

Пример #1
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     if (!in_array($this->option('db-driver'), ['mysql', 'sqlite'])) {
         $this->error("Unsupported db driver.");
         exit;
     }
     $this->line("Setting up Pagekit installation...");
     $app = $this->container;
     App::module('session')->config['storage'] = 'array';
     $app->boot();
     $app['module']->load('installer');
     $installer = new Installer($app);
     $dbDriver = $this->option('db-driver');
     $config = ['locale' => $this->option('locale'), 'database' => ['default' => $dbDriver, 'connections' => [$dbDriver => ['dbname' => $this->option('db-name'), 'host' => $this->option('db-host'), 'user' => $this->option('db-user'), 'password' => $this->option('db-pass'), 'prefix' => $this->option('db-prefix')]]]];
     $user = ['username' => $this->option('username'), 'password' => $this->option('password'), 'email' => $this->option('mail')];
     $options = ['system' => ['site' => ['locale' => $this->option('locale')], 'admin' => ['locale' => $this->option('locale')]], 'system/site' => ['title' => $this->option('title')]];
     $result = $installer->install($config, $options, $user);
     $status = $result['status'];
     $message = $result['message'];
     if ($status == 'success') {
         $this->line("Done");
     } else {
         $this->error($message);
     }
 }