UpdateModel::runStructure PHP Method

runStructure() public method

The structure runs the addons in priority order so that higher priority addons override lower priority ones.
public runStructure ( boolean $captureOnly = false )
$captureOnly boolean Run the structure or just capture the SQL changes.
    public function runStructure($captureOnly = false)
    {
        $addons = array_reverse(Gdn::addonManager()->getEnabled());
        // These variables are required for included structure files.
        $Database = Gdn::database();
        $SQL = $this->SQL;
        $SQL->CaptureModifications = $captureOnly;
        $Structure = Gdn::structure();
        $Structure->CaptureOnly = $captureOnly;
        /* @var Addon $addon */
        foreach ($addons as $addon) {
            // Look for a structure file.
            if ($structure = $addon->getSpecial('structure')) {
                Logger::event('addon_structure', Logger::INFO, "Executing structure for {addonKey}.", ['addonKey' => $addon->getKey(), 'structureType' => 'file']);
                try {
                    include $addon->path($structure);
                } catch (\Exception $ex) {
                    if (debug()) {
                        throw $ex;
                    }
                }
            }
            // Look for a structure method on the plugin.
            if ($addon->getPluginClass()) {
                $plugin = Gdn::pluginManager()->getPluginInstance($addon->getPluginClass(), Gdn_PluginManager::ACCESS_CLASSNAME);
                if (is_object($plugin) && method_exists($plugin, 'structure')) {
                    Logger::event('addon_structure', Logger::INFO, "Executing structure for {addonKey}.", ['addonKey' => $addon->getKey(), 'structureType' => 'method']);
                    try {
                        call_user_func([$plugin, 'structure']);
                    } catch (\Exception $ex) {
                        if (debug()) {
                            throw $ex;
                        }
                    }
                }
            }
            // Register permissions.
            $permissions = $addon->getInfoValue('registerPermissions');
            if (!empty($permissions)) {
                Logger::event('addon_permissions', Logger::INFO, "Defining permissions for {addonKey}.", ['addonKey' => $addon->getKey(), 'permissions' => $permissions]);
                Gdn::permissionModel()->define($permissions);
            }
        }
        $this->fireEvent('AfterStructure');
        if ($captureOnly && property_exists($Structure->Database, 'CapturedSql')) {
            return $Structure->Database->CapturedSql;
        }
        return [];
    }

Usage Example

 /**
  * Run a structure update on the database.
  *
  * It should always be possible to call this method, even if no database tables exist yet.
  * A working forum database should be built from scratch where none exists. Therefore,
  * it can have no reliance on existing data calls, or they must be able to fail gracefully.
  *
  * @since 2.0.?
  * @access public
  */
 public function update()
 {
     // Check for permission or flood control.
     // These settings are loaded/saved to the database because we don't want the config file storing non/config information.
     $Now = time();
     $LastTime = 0;
     $Count = 0;
     try {
         $LastTime = Gdn::get('Garden.Update.LastTimestamp', 0);
     } catch (Exception $Ex) {
         // We don't have a GDN_UserMeta table yet. Sit quietly and one will appear.
     }
     if ($LastTime + 60 * 60 * 24 > $Now) {
         // Check for flood control.
         try {
             $Count = Gdn::get('Garden.Update.Count', 0) + 1;
         } catch (Exception $Ex) {
             // Once more we sit, watching the breath.
         }
         if ($Count > 5) {
             if (!Gdn::session()->checkPermission('Garden.Settings.Manage')) {
                 // We are only allowing an update of 5 times every 24 hours.
                 throw permissionException();
             }
         }
     } else {
         $Count = 1;
     }
     try {
         Gdn::set('Garden.Update.LastTimestamp', $Now);
         Gdn::set('Garden.Update.Count', $Count);
     } catch (Exception $Ex) {
         // What is a GDN_UserMeta table, really? Suffering.
     }
     try {
         // Run the structure.
         $UpdateModel = new UpdateModel();
         $UpdateModel->runStructure();
         $this->setData('Success', true);
     } catch (Exception $Ex) {
         $this->setData('Success', false);
         $this->setData('Error', $Ex->getMessage());
         if (Debug()) {
             throw $Ex;
         }
     }
     if (Gdn::session()->checkPermission('Garden.Settings.Manage')) {
         saveToConfig('Garden.Version', APPLICATION_VERSION);
     }
     if ($Target = $this->Request->get('Target')) {
         safeRedirect($Target);
     }
     $this->fireEvent('AfterUpdate');
     $this->MasterView = 'empty';
     $this->CssClass = 'Home';
     $this->render();
 }
All Usage Examples Of UpdateModel::runStructure