nesbert
9/11/2012 - 8:05 PM

Working copy of multiple module Phalcon bootstrap (public/index.php).

Working copy of multiple module Phalcon bootstrap (public/index.php).

<?php
/**
 * Application driver class to initialize Phalcon and
 * other resources.
 */
class Application extends \Phalcon\Mvc\Application
{
    private static $mode = 'development'; // TODO change default to production
    private static $modules = array(
        'portal' => array(
            'className' => 'Knode\Portal\Module',
            'path' => '../apps/portal/Module.php'
        ),
        'admin' => array(
            'className' => 'Knode\Admin\Module',
            'path' => '../apps/admin/Module.php'
        )
    );

    const DEFAULT_MODULE    = 'portal';

    const MODE_PRODUCTION   = 'production';
    const MODE_STAGING      = 'staging';
    const MODE_TEST         = 'test';
    const MODE_DEVELOPMENT  = 'development';

    /**
     * Set application mode and error reporting level.
     */
    public function __construct()
    {
        if (!defined('PHALCON_MODE')) {
            $mode = getenv('PHALCON_MODE');
            $mode = $mode ? $mode : self::$mode;
            define('PHALCON_MODE', $mode);
        }

        switch (self::getMode()) {
            case self::MODE_PRODUCTION:
            case self::MODE_STAGING:
                error_reporting(0);
                break;

            case self::MODE_TEST:
            case self::MODE_DEVELOPMENT:
                error_reporting(E_ALL);
                break;
        }
    }

    /**
     * Register the services here to make them general or register in
     * the ModuleDefinition to make them module-specific.
     */
    protected function _registerServices()
    {
        $loader = new \Phalcon\Loader();

        /**
         * We're a registering a set of directories taken from the configuration file
         */
        $loader->registerDirs(
            array(
                __DIR__ . '/../library/',
                __DIR__ . '/../vendor/',
            )
        )->register();

        // Init a DI
        $di = new \Phalcon\DI\FactoryDefault();

        // Registering a router:
        $defaultModule = self::DEFAULT_MODULE;
        $modules = self::$modules;
        $di->set('router', function() use ($defaultModule, $modules) {

            $router = new \Phalcon\Mvc\Router();

            $router->setDefaultModule($defaultModule);

            foreach ($modules as $moduleName => $module) {

                 // do not route default module
                 if ($defaultModule == $moduleName) continue;

                 $router->add('#^/'.$moduleName.'(|/)$#', array(
                     'module' => $moduleName,
                     'controller' => 'index',
                     'action' => 'index',
                 ));

                 $router->add('#^/'.$moduleName.'/([a-zA-Z0-9\_]+)[/]{0,1}$#', array(
                     'module' => $moduleName,
                     'controller' => 1,
                 ));

                 $router->add('#^/'.$moduleName.'[/]{0,1}([a-zA-Z0-9\_]+)/([a-zA-Z0-9\_]+)(/.*)*$#', array(
                     'module' => $moduleName,
                     'controller' => 1,
                     'action' => 2,
                     'params' => 3,
                 ));
             }

            return $router;
        });

        $this->setDI($di);
    }

    public function main()
    {
        try {

            $this->_registerServices();
            $this->registerModules(self::$modules);
            $this->handle()->send();

        } catch (Exception $e) {

            // TODO log exception

            // remove view contents from buffer
            ob_clean();

            $errorCode = 500;
            $errorView = 'errors/500_error.phtml';

            switch (true) {
                // 401 UNAUTHORIZED
                case $e->getCode() == 401:
                    $errorCode = 401;
                    $errorView = 'errors/401_unathorized.phtml';
                    break;

                // 403 FORBIDDEN
                case $e->getCode() == 403:
                    $errorCode = 403;
                    $errorView = 'errors/403_forbidden.phtml';
                    break;

                // 404 NOT FOUND
                case $e->getCode() == 404:
                case ($e instanceof Phalcon\Mvc\View\Exception):
                case ($e instanceof Phalcon\Mvc\Dispatcher\Exception):
                    $errorCode = 404;
                    $errorView = 'errors/404_not_found.phtml';
                    break;
            }

            // Get error view contents. Since we are including the view
            // file here you can use PHP and local vars inside the error view.
            ob_start();
            include_once $errorView;
            $contents = ob_get_contents();
            ob_end_clean();

            // send view to header
            $response = $this->getDI()->getShared('response');
            $response->resetHeaders()
                ->setStatusCode($errorCode, null)
                ->setContent($contents)
                ->send();
        }
    }

    public static function getMode()
    {
        return self::$mode;
    }
}

// Run application:
$app = new Application();
$app->main();