Пример #1
0
 private function setUrl()
 {
     $url = new Url();
     if (!$this->application->debug) {
         $url->setBaseUri($this->application->production->baseUri);
         $url->setStaticBaseUri($this->application->production->staticBaseUri);
     } else {
         $url->setBaseUri($this->application->development->baseUri);
         $url->setStaticBaseUri($this->application->development->staticBaseUri);
     }
     return $url;
 }
 public function onBoot()
 {
     // TODO: Implement onBoot() method.
     $this->getDI()->setShared('url', function () {
         $protocol = stripos(server('SERVER_PROTOCOL'), 'https') === true ? 'https://' : 'http://';
         $hostname = server('HTTP_HOST');
         $url = new Url();
         $url->setStaticBaseUri(env('static_url', "{$protocol}{$hostname}/"));
         $url->setBaseUri(env('base_url', '/'));
         return $url;
     });
 }
Пример #3
0
 /**
  * Set url service when module start
  * @param Di $di
  * @param Config $config
  * @param string $name Module name
  * @return \Phalex\Events\Listener\Application
  */
 private function setUrlService(Di $di, Config $config, $name)
 {
     $base = $static = '/';
     if (isset($config['url'])) {
         $default = isset($config['url']['default']) ? $config['url']['default'] : '/';
         if (isset($config['url'][$name])) {
             $base = isset($config['url'][$name]['uri']) ? $config['url'][$name]['uri'] : $default;
             $static = isset($config['url'][$name]['static']) ? $config['url'][$name]['static'] : $default;
         }
     }
     $url = new UrlService();
     $url->setBaseUri($base);
     $url->setStaticBaseUri($static);
     $di->set('url', $url, true);
     return $this;
 }
//Github API mock
$di->set('github', function () use($config) {
    $api = m::mock("api");
    $api->shouldReceive("show")->with("simple-helpers", "php-file-mover", 8)->andReturn(json_decode(file_get_contents(__DIR__ . "/../_data/8"), true));
    $client = m::mock("Github");
    $client->shouldReceive("api")->with('pull_request')->andReturn($api);
    return $client;
}, true);
/**
 * The URL component is used to generate all kind of urls in the application
 */
$di->set('url', function () use($config) {
    $url = new UrlResolver();
    if (!$config->application->debug) {
        $url->setBaseUri($config->application->production->baseUri);
        $url->setStaticBaseUri($config->application->production->staticBaseUri);
    } else {
        $url->setBaseUri($config->application->development->baseUri);
        $url->setStaticBaseUri($config->application->development->staticBaseUri);
    }
    return $url;
}, true);
/**
 * Database connection is created based in the parameters defined in the configuration file
 */
$di->set('db', function () use($config) {
    $connection = new DatabaseConnection($config->database->toArray());
    $debug = $config->application->debug;
    if ($debug) {
        $eventsManager = new EventsManager();
        $logger = new FileLogger(APP_PATH . "/app/logs/db.log");
Пример #5
0
 public function setStaticBaseUri($staticBaseUri)
 {
     return parent::setStaticBaseUri($staticBaseUri);
 }
Пример #6
0
use Phalcon\Session\Adapter\Files as SessionAdapter;
/**
 * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
 */
$di = new FactoryDefault();
/**
 * Set router for rewrite url
 */
$di->setShared('router', include __DIR__ . '/routers.php');
/**
 * The URL component is used to generate all kind of urls in the application
 */
$di->setShared('url', function () use($config) {
    $url = new UrlResolver();
    $url->setBaseUri($config->application->baseUri);
    $url->setStaticBaseUri($config->application->staticUri);
    return $url;
});
/**
 * Setting up the view component
 */
$di->setShared('view', function () use($config) {
    $view = new View();
    $view->setViewsDir($config->application->viewsDir);
    $view->registerEngines(array('.volt' => function ($view, $di) use($config) {
        $volt = new VoltEngine($view, $di);
        $volt->setOptions(array('compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '.'));
        return $volt;
    }, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
    return $view;
});
Пример #7
0
 /**
  * Register the services here to make them general
  * or register in the ModuleDefinition to make them module-specific
  */
 public function registerServices(DiInterface $di)
 {
     //Read configuration
     $config = (include __DIR__ . "/config/config.php");
     $configGlobal = $di->getConfig();
     $di->set('url', function () use($config, $configGlobal) {
         $url = new Url();
         if (APPLICATION_ENV == 'production') {
             $url->setStaticBaseUri($configGlobal->application->production->staticBaseUri);
         } else {
             $url->setStaticBaseUri($configGlobal->application->development->staticBaseUri);
         }
         $url->setBaseUri($config->application->baseUri);
         return $url;
     });
     //Registering a dispatcher
     $di->set('dispatcher', function () {
         $eventsManager = $this->getEventsManager();
         $eventsManager->attach("dispatch", function ($event, $dispatcher, $exception) {
             //controller or action doesn't exist
             if ($event->getType() == 'beforeException') {
                 $message = $exception->getMessage();
                 $response = $this->getResponse();
                 switch ($exception->getCode()) {
                     case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                         $response->redirect();
                         return false;
                     case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                         $response->redirect('action-not-found?msg=' . $message);
                         return false;
                     case Dispatcher::EXCEPTION_CYCLIC_ROUTING:
                         $response->redirect('cyclic-routing?msg=' . $message);
                         return false;
                 }
             }
         });
         $dispatcher = new Dispatcher();
         $dispatcher->setDefaultNamespace("Phanbook\\Frontend\\Controllers");
         $dispatcher->setEventsManager($eventsManager);
         return $dispatcher;
     });
     /**
      * Setting up the view component
      */
     $di->set('view', function () use($di) {
         $config = $di->get('config');
         $view = new View();
         $view->setViewsDir(ROOT_DIR . 'content/themes/' . $config->theme);
         $view->disableLevel([View::LEVEL_MAIN_LAYOUT => true, View::LEVEL_LAYOUT => true]);
         $view->registerEngines(['.volt' => 'volt']);
         // Create an event manager
         $eventsManager = $this->getEventsManager();
         $eventsManager->attach('view', function ($event, $view) {
             if ($event->getType() == 'notFoundView') {
                 throw new \Exception('View not found!!! (' . $view->getActiveRenderPath() . ')');
             }
         });
         // Bind the eventsManager to the view component
         $view->setEventsManager($eventsManager);
         return $view;
     });
 }
Пример #8
0
 /**
  * Register the services here to make them general
  * or register in the ModuleDefinition to make them module-specific
  */
 public function registerServices(DiInterface $di)
 {
     //Read configuration
     $config = (include __DIR__ . "/config/config.php");
     $configGlobal = $di->getConfig();
     // The URL component is used to generate all kind of urls in the application
     $di->set('url', function () use($config, $configGlobal) {
         $url = new Url();
         if (APPLICATION_ENV == 'production') {
             $url->setStaticBaseUri($configGlobal->application->production->staticBaseUri);
         } else {
             $url->setStaticBaseUri($configGlobal->application->development->staticBaseUri);
         }
         $url->setBaseUri($config->application->baseUri);
         return $url;
     });
     //Registering a dispatcher
     $di->set('dispatcher', function () use($di) {
         //Create/Get an EventManager
         $eventsManager = new EventsManager();
         //Attach a listener
         $eventsManager->attach('dispatch', function ($event, $dispatcher, $exception) use($di) {
             //controller or action doesn't exist
             if ($event->getType() == 'beforeException') {
                 $message = $exception->getMessage();
                 $response = $di->getResponse();
                 switch ($exception->getCode()) {
                     case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                         $response->redirect();
                         return false;
                     case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                         $response->redirect('action-not-found?msg=' . $message);
                         return false;
                     case Dispatcher::EXCEPTION_CYCLIC_ROUTING:
                         $response->redirect('cyclic-routing?msg=' . $message);
                         return false;
                 }
             }
         });
         $dispatcher = new Dispatcher();
         $dispatcher->setDefaultNamespace("Phanbook\\Backend\\Controllers");
         $dispatcher->setEventsManager($eventsManager);
         return $dispatcher;
     });
     /**
      * Setting up the view component
      */
     $di->set('view', function () use($config) {
         $view = new View();
         $view->setViewsDir($config->application->viewsDir);
         $view->disableLevel([View::LEVEL_MAIN_LAYOUT => true, View::LEVEL_LAYOUT => true]);
         $view->registerEngines(['.volt' => 'volt']);
         // Create an event manager
         $eventsManager = new EventsManager();
         // Attach a listener for type 'view'
         $eventsManager->attach('view', function ($event, $view) {
             if ($event->getType() == 'notFoundView') {
                 throw new \Exception('View not found!!! (' . $view->getActiveRenderPath() . ')');
             }
         });
         // Bind the eventsManager to the view component
         $view->setEventsManager($eventsManager);
         return $view;
     });
     $configMenu = (include __DIR__ . "/config/config.menu.php");
     $di->setShared('menuStruct', function () use($configMenu) {
         // if structure received from db table instead getting from $config
         // we need to store it to cache for reducing db connections
         $struct = $configMenu->get('menuStruct')->toArray();
         return $struct;
     });
 }
Пример #9
0
 /**
  * Initialize the Url service.
  *
  * The URL component is used to generate all kind of urls in the application.
  *
  * @param DiInterface   $di     Dependency Injector
  * @param Config        $config App config
  * @param EventsManager $em     Events Manager
  *
  * @return void
  */
 protected function initUrl(DiInterface $di, Config $config, EventsManager $em)
 {
     $di->setShared('url', function () use($config) {
         $url = new UrlResolver();
         if (ENV_PRODUCTION === APPLICATION_ENV) {
             $url->setBaseUri($config->get('application')->production->baseUri);
             $url->setStaticBaseUri($config->get('application')->production->staticBaseUri);
         } else {
             $url->setBaseUri($config->get('application')->development->baseUri);
             $url->setStaticBaseUri($config->get('application')->development->staticBaseUri);
         }
         return $url;
     });
 }
Пример #10
0
 /**
  * Initializes the baseUrl
  *
  * @param array $options
  */
 public function initUrl($options = [])
 {
     $config = $this->di['config'];
     $request = $this->di['request'];
     /**
      * The URL component is used to generate all kind of urls in the
      * application
      */
     $this->di->setShared('url', function () use($config, $request) {
         $url = new PhUrl();
         if (SUBDOMAIN == 'm') {
             $config->app_baseUri = 'm.' . $config->app_baseUri;
         }
         $url->setBaseUri($request->getScheme() . '://' . $config->app_baseUri . '/');
         $url->setStaticBaseUri($request->getScheme() . '://' . $config->app_resourceUri . '/');
         return $url;
     });
 }
Пример #11
0
 /**
  * Init environment.
  *
  * @param DI     $di     Dependency Injection.
  * @param Config $config Config object.
  *
  * @return Url
  */
 protected function _initEnvironment($di, $config)
 {
     set_error_handler(function ($errorCode, $errorMessage, $errorFile, $errorLine) {
         throw new \ErrorException($errorMessage, $errorCode, 1, $errorFile, $errorLine);
     });
     set_exception_handler(function ($e) use($di) {
         /**
          * Write to log when app in production mode.
          */
         if (ENV == ENV_PRODUCTION) {
             $errorId = EnException::logException($e);
         }
         if ($di->get('app')->isConsole()) {
             echo 'Error <' . $errorId . '>: ' . $e->getMessage();
             return true;
         }
         if (ENV == ENV_DEVELOPMENT) {
             $p = new PrettyExceptions($di);
             $p->setBaseUri('/plugins/pretty-exceptions/');
             return $p->handleException($e);
         }
         return true;
     });
     if ($config->global->profiler) {
         $profiler = new EnProfiler();
         $di->set('profiler', $profiler);
     }
     /**
      * The URL component is used to generate all kind of urls in the
      * application
      */
     $url = new PhUrl();
     $url->setBaseUri($config->global->baseUrl);
     $url->setStaticBaseUri($config->global->staticUrl);
     $di->set('url', $url);
     return $url;
 }
Пример #12
0
$loader = new Loader();
// Register the Library namespace as well as the common module
// since it needs to always be available
$loader->registerNamespaces(['Phalcon\\Test\\Models' => $config->get('application')->modelsDir, 'Phalcon\\Test\\Collections' => $config->get('application')->collectionsDir, 'Phalcon\\Test\\Modules\\Frontend\\Controllers' => $config->get('application')->modulesDir . 'frontend/controllers/', 'Phalcon\\Test\\Modules\\Backend\\Controllers' => $config->get('application')->modulesDir . 'backend/controllers/']);
$loader->registerDirs([$config->get('application')->controllersDir, $config->get('application')->tasksDir, $config->get('application')->microDir]);
$loader->register();
$di->setShared('loader', $loader);
/**
 * The URL component is used to generate all kind of urls in the
 * application
 */
$di->setShared('url', function () use($di) {
    $config = $di['config'];
    $config = $config->get('application');
    $url = new Url();
    $url->setStaticBaseUri($config->staticUri);
    $url->setBaseUri($config->baseUri);
    return $url;
});
/**
 * Router
 */
$di->setShared('router', function () {
    return new Router(false);
});
/**
 * Dispatcher
 */
$di->set('dispatcher', function () use($di) {
    return new Dispatcher();
});
Пример #13
0
 protected function initUrl()
 {
     $config = $this->di['config'];
     $this->di->set('url', function () use($config) {
         $url = new UrlResolver();
         $url->setBaseUri($config->paths->baseUri);
         $url->setStaticBaseUri($config->paths->assetUri);
         return $url;
     }, TRUE);
 }
Пример #14
0
 /**
  * Initialize the Url service.
  *
  * The URL component is used to generate all kind of urls in the application.
  */
 protected function initUrl()
 {
     $this->di->setShared('url', function () {
         /** @var DiInterface $this */
         $config = $this->getShared('config');
         $url = new UrlResolver();
         if (ENV_PRODUCTION === APPLICATION_ENV) {
             $url->setBaseUri($config->get('application')->production->baseUri);
             $url->setStaticBaseUri($config->get('application')->production->staticBaseUri);
         } else {
             $url->setBaseUri($config->get('application')->development->baseUri);
             $url->setStaticBaseUri($config->get('application')->development->staticBaseUri);
         }
         return $url;
     });
 }