示例#1
0
    public static function generate(array $addOn, DevHelper_Config_Base $config)
    {
        $className = self::getClassName($addOn, $config);
        $tables = array();
        $dataClasses = $config->getDataClasses();
        foreach ($dataClasses as $dataClass) {
            $table = array();
            $table['createQuery'] = DevHelper_Generator_Db::createTable($config, $dataClass);
            $table['dropQuery'] = false;
            $tables[$dataClass['name']] = $table;
        }
        $tables = DevHelper_Generator_File::varExport($tables);
        $patches = array();
        $dataPatches = $config->getDataPatches();
        foreach ($dataPatches as $table => $tablePatches) {
            foreach ($tablePatches as $dataPatch) {
                $patch = array();
                $patch['table'] = $table;
                $patch['field'] = $dataPatch['name'];
                $patch['showColumnsQuery'] = DevHelper_Generator_Db::showColumns($config, $table, $dataPatch);
                $patch['alterTableAddColumnQuery'] = DevHelper_Generator_Db::alterTableAddColumn($config, $table, $dataPatch);
                $patch['alterTableDropColumnQuery'] = false;
                $patches[] = $patch;
            }
        }
        $patches = DevHelper_Generator_File::varExport($patches);
        $commentAutoGeneratedStart = DevHelper_Generator_File::COMMENT_AUTO_GENERATED_START;
        $commentAutoGeneratedEnd = DevHelper_Generator_File::COMMENT_AUTO_GENERATED_END;
        $contents = <<<EOF
<?php
class {$className} {

\t{$commentAutoGeneratedStart}

\tprotected static \$_tables = {$tables};
\tprotected static \$_patches = {$patches};

\tpublic static function install() {
\t\t\$db = XenForo_Application::get('db');

\t\tforeach (self::\$_tables as \$table) {
\t\t\t\$db->query(\$table['createQuery']);
\t\t}
\t\t
\t\tforeach (self::\$_patches as \$patch) {
\t\t\t\$existed = \$db->fetchOne(\$patch['showColumnsQuery']);
\t\t\tif (empty(\$existed)) {
\t\t\t\t\$db->query(\$patch['alterTableAddColumnQuery']);
\t\t\t}
\t\t}
\t}
\t
\tpublic static function uninstall() {
\t\t// TODO
\t}

\t{$commentAutoGeneratedStart}
\t
}
EOF;
        return array($className, $contents);
    }
示例#2
0
    public static function generate(array $addOn, DevHelper_Config_Base $config)
    {
        $className = self::getClassName($addOn, $config);
        $tables = array();
        $dataClasses = $config->getDataClasses();
        foreach ($dataClasses as $dataClass) {
            $table = array();
            $table['createQuery'] = DevHelper_Generator_Db::createTable($config, $dataClass);
            $table['dropQuery'] = DevHelper_Generator_Db::dropTable($config, $dataClass);
            $tables[$dataClass['name']] = $table;
        }
        $tables = DevHelper_Generator_File::varExport($tables);
        $patches = array();
        $dataPatches = $config->getDataPatches();
        foreach ($dataPatches as $table => $tablePatches) {
            foreach ($tablePatches as $dataPatch) {
                $patch = array();
                $patch['table'] = $table;
                $patch['tableCheckQuery'] = DevHelper_Generator_Db::showTables($config, $table);
                if (!empty($dataPatch['index'])) {
                    $patch['index'] = $dataPatch['name'];
                    $patch['checkQuery'] = DevHelper_Generator_Db::showIndexes($config, $table, $dataPatch);
                    $patch['addQuery'] = DevHelper_Generator_Db::alterTableAddIndex($config, $table, $dataPatch);
                    $patch['dropQuery'] = DevHelper_Generator_Db::alterTableDropIndex($config, $table, $dataPatch);
                } else {
                    $patch['field'] = $dataPatch['name'];
                    $patch['checkQuery'] = DevHelper_Generator_Db::showColumns($config, $table, $dataPatch);
                    $patch['addQuery'] = DevHelper_Generator_Db::alterTableAddColumn($config, $table, $dataPatch);
                    $patch['dropQuery'] = DevHelper_Generator_Db::alterTableDropColumn($config, $table, $dataPatch);
                }
                $patches[] = $patch;
            }
        }
        $patches = DevHelper_Generator_File::varExport($patches);
        $commentAutoGeneratedStart = DevHelper_Generator_File::COMMENT_AUTO_GENERATED_START;
        $commentAutoGeneratedEnd = DevHelper_Generator_File::COMMENT_AUTO_GENERATED_END;
        $contents = <<<EOF
<?php

class {$className}
{
    {$commentAutoGeneratedStart}

    protected static \$_tables = {$tables};
    protected static \$_patches = {$patches};

    public static function install(\$existingAddOn, \$addOnData)
    {
        \$db = XenForo_Application::get('db');

        foreach (self::\$_tables as \$table) {
            \$db->query(\$table['createQuery']);
        }

        foreach (self::\$_patches as \$patch) {
            \$tableExisted = \$db->fetchOne(\$patch['tableCheckQuery']);
            if (empty(\$tableExisted)) {
                continue;
            }

            \$existed = \$db->fetchOne(\$patch['checkQuery']);
            if (empty(\$existed)) {
                \$db->query(\$patch['addQuery']);
            }
        }

        self::installCustomized(\$existingAddOn, \$addOnData);
    }

    public static function uninstall()
    {
        \$db = XenForo_Application::get('db');

        foreach (self::\$_patches as \$patch) {
            \$tableExisted = \$db->fetchOne(\$patch['tableCheckQuery']);
            if (empty(\$tableExisted)) {
                continue;
            }

            \$existed = \$db->fetchOne(\$patch['checkQuery']);
            if (!empty(\$existed)) {
                \$db->query(\$patch['dropQuery']);
            }
        }

        foreach (self::\$_tables as \$table) {
            \$db->query(\$table['dropQuery']);
        }

        self::uninstallCustomized();
    }

    {$commentAutoGeneratedEnd}

    public static function installCustomized(\$existingAddOn, \$addOnData)
    {
        // customized install script goes here
    }

    public static function uninstallCustomized()
    {
        // customized uninstall script goes here
    }

}
EOF;
        return array($className, $contents);
    }