1. Vladimir Mihailenco
  2. zf-form-builder

Source

zf-form-builder / www / system / library / ZendY / Tool / Provider / Modules.php

<?php
require_once 'ZendY/Tool/Provider/AbstractProvider.php';

class ZendY_Tool_Provider_Modules extends ZendY_Tool_Provider_AbstractProvider
{
    public function publish($modules = '')
    {
        self::initialize();

        $modules = $this->getModules($modules);
        foreach ($modules as $moduleName) {
            $webDir = MODULES_DIR . "/{$moduleName}/web";
            if (!file_exists($webDir)) {
                continue;
            }

             $it = new DirectoryIterator($webDir);
             foreach ($it as $dir) {
                 if ($dir->isDot()) {
                     continue;
                 }

                 $targetDir = WEB_DIR . '/' . $dir->getFilename() . '/' . $moduleName;
                 IO_File::copyDir($dir->getPathname(), $targetDir);
             }
        }

        return $this;
    }

    public function build()
    {
        return $this
                ->dropCreate()
                ->buildModels()
                ->createTables()
                ->loadData();
    }

    public function dropCreate()
    {
        return $this
                ->dropDb()
                ->createDb();
    }

    public function dropDb()
    {
        $this->initialize();

        try {
            Doctrine_Core::dropDatabases();
        } catch (Doctrine_Connection_Exception $e) {}

        return $this;
    }

    public function createDb()
    {
        $this->initialize();

        $manager = Doctrine_Manager::getInstance();
        foreach ($manager as $name => $connection) {
            $connection->createDatabase();
        }

        return $this;
    }

    public function buildModels($modules = '')
    {
        self::initialize();

        $modelDir = IO_File::emptyDir(CACHE_DIR . '/model');
        $configDir = IO_File::emptyDir(CACHE_DIR . '/config');
        $modules = $this->getModules($modules);

        // collect all configs in one dir
        foreach ($modules as $moduleName) {
            $moduleConfigDir = MODULES_DIR . "/$moduleName/configs/doctrine";
            if (!file_exists($moduleConfigDir)) {
                continue;
            }

            $it = new DirectoryIterator($moduleConfigDir);
            foreach ($it as $file) {
                if (!$file->isFile()) {
                    continue;
                }

                IO_File::copy(
                    $file->getPathname(),
                    $configDir . '/' . $moduleName . '_' . $file->getFilename()
                );
            }
        }

        Doctrine_Core::generateModelsFromYaml($configDir, $modelDir, array(
            'pearStyle' => true,
            'classPrefixFiles' => false,
            'baseClassesDirectory' => null,
            'baseClassPrefix' => 'Base',
            'baseClassName' => 'DoctrineX_Record',
        ));

        foreach ($modules as $moduleName) {
            $modulePrefix = ucfirst($moduleName);
            $moduleModelDir = $modelDir . "/{$modulePrefix}";
            $moduleBaseModelDir = $modelDir . "/Base{$modulePrefix}";

            if (file_exists($moduleModelDir) && file_exists($moduleBaseModelDir)) {
                $targetDir = MODULES_DIR . "/{$moduleName}/library";
                $it = new DirectoryIterator($moduleModelDir);
                foreach ($it as $file) {
                    if (!$file->isFile()) {
                        continue;
                    }

                    $targetFile = $targetDir . '/' . $file->getFilename();
                    if (!file_exists($targetFile)) {
                        $contents = IO_File::get($file->getPathname());
                        $contents = $this->processFileContents($contents, $modulePrefix);
                        IO_File::set(
                            $targetFile,
                            $contents
                        );
                    }
                }

                $targetDir = IO_File::emptyDir(MODULES_DIR . "/{$moduleName}/library/Base");
                $it = new DirectoryIterator($moduleBaseModelDir);
                foreach ($it as $file) {
                    if (!$file->isFile()) {
                        continue;
                    }

                    $contents = IO_File::get($file->getPathname());
                    $contents = $this->processFileContents($contents, $modulePrefix);
                    IO_File::set(
                        $targetDir . '/' . $file->getFilename(),
                        $contents
                    );
                }
            }
        }

        return $this;
    }

    public function createTables($modules = '')
    {
        self::initialize();

        $modelDir = IO_File::emptyDir(CACHE_DIR . '/model');

        $modules = $this->getModules($modules);
        foreach ($modules as $moduleName) {
            $moduleModelDir = MODULES_DIR . "/{$moduleName}/library";
            if (!file_exists($moduleModelDir)) {
                continue;
            }

            $modulePrefix = ucfirst($moduleName);
            IO_File::copyDir($moduleModelDir, $modelDir . "/{$modulePrefix}", true);
        }

        Doctrine_Core::createTablesFromModels($modelDir);

        return $this;
    }

    public function loadData($modules = '', $truncate = true)
    {
        self::initialize();

        $fixtureDir = IO_File::createDir(CACHE_DIR . '/fixtures');
        IO_File::emptyDir($fixtureDir);

        $modules = $this->getModules($modules);
        foreach ($modules as $moduleName) {
            $moduleFixtureDir = MODULES_DIR . "/{$moduleName}/fixtures";
            if (!file_exists($moduleFixtureDir)) {
                continue;
            }

            $modulePrefix = ucfirst($moduleName);
            IO_File::copyDir($moduleFixtureDir, $fixtureDir . "/{$modulePrefix}");
        }

        Doctrine_Core::loadData($fixtureDir, !$truncate);

        return $this;
    }

    protected function processFileContents($contents, $modulePrefix)
    {
        $contents = str_replace(
            'Base' . $modulePrefix,
            $modulePrefix . '_Base',
            $contents
        );
        $contents = preg_replace('#\/\*.+?\*\/#s', '', $contents);
        return $contents;
    }

    protected function getModules($modules)
    {
        if ('' === $modules) {
            return Zend_Registry::get('appConfig')->modules;
        } else {
            return explode(', ', $modules);
        }
    }
}