Esempio n. 1
0
 public function modulesClosure(IntegrationTester $I)
 {
     $I->wantTo('handle request and get content by using single modules strategy (closure)');
     Di::reset();
     $_GET['_url'] = '/login';
     $di = new FactoryDefault();
     $di->set('router', function () {
         $router = new Router(false);
         $router->add('/index', ['controller' => 'index', 'module' => 'frontend', 'namespace' => 'Phalcon\\Test\\Modules\\Frontend\\Controllers']);
         $router->add('/login', ['controller' => 'login', 'module' => 'backend', 'namespace' => 'Phalcon\\Test\\Modules\\Backend\\Controllers']);
         return $router;
     });
     $application = new Application();
     $view = new View();
     $application->registerModules(['frontend' => function ($di) use($view) {
         /** @var \Phalcon\DiInterface $di */
         $di->set('view', function () use($view) {
             $view->setViewsDir(PATH_DATA . 'modules/frontend/views/');
             return $view;
         });
     }, 'backend' => function ($di) use($view) {
         /** @var \Phalcon\DiInterface $di */
         $di->set('view', function () use($view) {
             $view->setViewsDir(PATH_DATA . 'modules/backend/views/');
             return $view;
         });
     }]);
     $application->setDI($di);
     $I->assertEquals('<html>here</html>' . PHP_EOL, $application->handle()->getContent());
 }
Esempio n. 2
0
 private function setServices()
 {
     $di = new FactoryDefault();
     $di->set('config', $this->setConfig());
     $this->setAutoloaders();
     $di->set('url', $this->setUrl(), true);
     $di->set('router', $this->setRouter());
     $this->setDI($di);
     $di->set('view', $this->setView(), true);
     $di->set('db', $this->setDb());
     $di->set('modelsMetadata', $this->setModelsMetadata());
     $di->set('session', $this->setSession());
     $this->setDI($di);
 }
Esempio n. 3
0
 public static function setUpBeforeClass()
 {
     $basedir = realpath(__DIR__ . '/../../');
     $testdir = $basedir . '/tests';
     self::$viewsdir = realpath($testdir . '/views/') . '/';
     include_once $basedir . "/vendor/autoload.php";
     $di = new DI();
     $di->set('form', "Logikos\\Forms\\Form");
     $di->set('url', function () {
         $url = new \Phalcon\Mvc\Url();
         $url->setBaseUri('/');
         return $url;
     });
     static::$di = $di;
 }
Esempio n. 4
0
 /**
  * Constructor.
  */
 public function __construct()
 {
     /**
      * Create default DI.
      */
     $di = new DI\FactoryDefault();
     /**
      * Get config.
      */
     $this->_config = Config::factory();
     if (!$this->_config->installed) {
         define('CHECK_REQUIREMENTS', true);
         require_once PUBLIC_PATH . '/requirements.php';
     }
     /**
      * Setup Registry.
      */
     $registry = new Registry();
     $registry->modules = array_merge([self::SYSTEM_DEFAULT_MODULE, 'user'], $this->_config->modules->toArray());
     $registry->widgets = $this->_config->widgets->toArray();
     $registry->directories = (object) ['engine' => ROOT_PATH . '/app/engine/', 'modules' => ROOT_PATH . '/app/modules/', 'plugins' => ROOT_PATH . '/app/plugins/', 'widgets' => ROOT_PATH . '/app/widgets/', 'libraries' => ROOT_PATH . '/app/libraries/'];
     $di->set('registry', $registry);
     // Store config in the DI container.
     $di->setShared('config', $this->_config);
     parent::__construct($di);
 }
Esempio n. 5
0
 public function setUp()
 {
     $this->task = new ImportDmmTask();
     $di = new Di\FactoryDefault();
     $db = function () {
         return new Mysql(array("host" => $GLOBALS['db_host'], "username" => $GLOBALS['db_username'], "password" => $GLOBALS['db_password'], "dbname" => 'yinxing'));
     };
     $di->set('dbSlave', $db);
     $di->set('dbMaster', $db);
     $di->set('moduleManager', function () {
         return new ModuleManager();
     });
     /** @var Mysql $mysql */
     $mysql = $di->get('dbMaster');
     $mysql->query(file_get_contents(__DIR__ . '/../../sql/evamovie_2015-10-20.sql'));
 }
Esempio n. 6
0
 /**
  * Register the services here to make them general or register in the ModuleDefinition to make them module-specific
  */
 public function registerServices()
 {
     $di = new FactoryDefault();
     $loader = new Loader();
     $namespaces = [];
     $map = (require_once __DIR__ . '/../autoload_namespaces.php');
     foreach ($map as $k => $values) {
         $k = trim($k, '\\');
         if (!isset($namespaces[$k])) {
             $dir = '/' . str_replace('\\', '/', $k) . '/';
             $namespaces[$k] = implode($dir . ';', $values) . $dir;
         }
     }
     $loader->registerNamespaces($namespaces);
     $loader->register();
     /**
      * Register a router
      */
     $di->set('router', function () {
         $router = new Router();
         $router->setDefaultModule('frontend');
         //set frontend routes
         $router->mount(new FrontendRoutes());
         //
         return $router;
     });
     $this->setDI($di);
 }
 /**
  * @dataProvider additionEmptyDataProvider
  * @expectedException     \SMSFactory\Exceptions\BaseException
  * @expectedExceptionCode 500
  */
 public function testSendCatchExceptionsForNotConfiguredProviders($provider)
 {
     $this->di->set('config', function () {
         return new Config(require './phpunit/data/empty.php');
     });
     $callInstance = new Sender($this->di);
     $this->assertInstanceOf('SMSFactory\\Sender', $callInstance, "[-] Provider instance error");
     $callInstance->call($provider)->setRecipient($this->phone)->send($this->message);
 }
 /**
  * Registers the services in di container.
  *
  * @return void
  */
 private function registerServices()
 {
     $di = new FactoryDefault();
     $di->set('config', function () {
         ob_start();
         $config = (include APPLICATION_ENV . '.php');
         ob_end_clean();
         return new Config($config);
     });
     $di->set('dispatcher', function () {
         $dispatcher = new Dispatcher();
         $dispatcher->setDefaultNamespace('Application\\Controllers\\');
         return $dispatcher;
     });
     $di->set('view', function () {
         $view = new View();
         $view->setViewsDir(ROOT_PATH . '/application/views/');
         return $view;
     });
     $this->setDI($di);
 }
Esempio n. 9
0
 /**
  * @return $this
  */
 public function createDependencies()
 {
     $dependency = new FactoryDefault();
     $dependency->set('db', function () {
         return $this->getDatabase();
     });
     $dependency->set('router', function () {
         $router = new Router(false);
         $routes = Routes::get();
         foreach ($routes as $group => $controllers) {
             foreach ($controllers as $controller) {
                 $router->add($controller['route'], ['namespace' => "App\\Controllers\\{$group}", 'controller' => $controller['class'], 'action' => 'run'], $controller['method']);
             }
         }
         $router->notFound(['namespace' => 'PhRest\\Controllers', 'controller' => 'Missing', 'action' => 'run']);
         return $router;
     });
     $dependency->set('view', function () {
         return new View();
     }, true);
     $this->setDI($dependency);
     return $this;
 }
Esempio n. 10
0
 /**
  * Sets the test up by loading the DI container and other stuff
  *
  * @author Nikos Dimopoulos <*****@*****.**>
  * @since  2012-09-30
  * @param  \Phalcon\DiInterface $di
  * @param  \Phalcon\Config      $config
  * @return void
  */
 protected function setUp(DiInterface $di = null, Config $config = null)
 {
     $this->checkExtension('phalcon');
     if (!is_null($config)) {
         $this->config = $config;
     }
     if (is_null($di)) {
         // Reset the DI container
         DI::reset();
         // Instantiate a new DI container
         $di = new FactoryDefault();
         // Set the URL
         $di->set('url', function () {
             $url = new Url();
             $url->setBaseUri('/');
             return $url;
         });
         $di->set('escaper', function () {
             return new \Phalcon\Escaper();
         });
     }
     $this->di = $di;
 }
Esempio n. 11
0
 /**
  * Register the services here to make them general or register in the ModuleDefinition to make them module-specific
  */
 protected function registerServices()
 {
     $di = new FactoryDefault();
     require '../../../autoloader.php';
     $loader = new Loader();
     /**
      * We're a registering a set of directories taken from the configuration file
      */
     $loader->registerDirs(array(__DIR__ . '/../apps/library/'))->register();
     $router = new Router();
     $router->setDefaultModule("frontend");
     //Registering a router
     $di->set('router', function () use($router) {
         return $router;
     });
     $this->setDI($di);
 }
Esempio n. 12
0
 public function setUp(Phalcon\DiInterface $di = NULL, Phalcon\Config $config = NULL)
 {
     $this->checkExtension('phalcon');
     if (!is_null($config)) {
         $this->config = $config;
     }
     if (is_null($di)) {
         // Reset the DI container
         DI::reset();
         // Instantiate a new DI container
         $di = new FactoryDefault();
         // Set the URL
         $di->set('collectionManager', function () {
             return new CollectionManager();
         }, true);
     }
     $this->di = $di;
 }
Esempio n. 13
0
 /**
  * Register the services here to make them general or register in the ModuleDefinition to make them module-specific
  */
 protected function registerServices()
 {
     $di = new FactoryDefault();
     $loader = new Loader();
     /**
      * We're a registering a set of directories taken from the configuration file
      */
     $loader->registerDirs(array(__DIR__ . '/../apps/library/'))->register();
     //Registering a router
     $di->set('router', function () {
         $router = new Router();
         $router->setDefaultModule("frontend");
         $router->add('/:controller/:action', array('module' => 'frontend', 'controller' => 1, 'action' => 2));
         $router->add("/login", array('module' => 'backend', 'controller' => 'login', 'action' => 'index'));
         $router->add("/admin/products/:action", array('module' => 'backend', 'controller' => 'products', 'action' => 1));
         $router->add("/products/:action", array('module' => 'frontend', 'controller' => 'products', 'action' => 1));
         return $router;
     });
     $this->setDI($di);
 }
Esempio n. 14
0
 /**
  * Constructor of the App
  */
 public function __construct()
 {
     $di = new DI\FactoryDefault();
     $this->_config = config('config');
     $registry = new Registry();
     $registry->directories = (object) ['modules' => APP_PATH . '/modules/', 'engine' => APP_PATH . '/engine/', 'library' => APP_PATH . '/library/'];
     $di->set('registry', $registry);
     $di->setShared('config', $this->_config);
     $eventsManager = new EventsManager();
     $this->setEventsManager($eventsManager);
     $this->_initLogger($di, $this->_config);
     $this->_initLoader($di, $this->_config, $eventsManager);
     foreach ($this->_loaders as $service) {
         $serviceName = ucfirst($service);
         $eventsManager->fire('init:before' . $serviceName, null);
         $result = $this->{'_init' . $serviceName}($di, $this->_config, $eventsManager);
         $eventsManager->fire('init:after' . $serviceName, $result);
     }
     $di->setShared('eventsManager', $eventsManager);
     $this->_noAuthPages = array();
 }
Esempio n. 15
0
 /**
  * Constructor.
  */
 public function __construct()
 {
     /**
      * Create default DI.
      */
     $di = new DI\FactoryDefault();
     /**
      * Get config.
      */
     $this->_config = Config::factory();
     /**
      * Adding modules to registry to load.
      * Module namespace - directory will be load from here.
      */
     $registry = new PhRegistry();
     $registry->modules = array_merge([self::SYSTEM_DEFAULT_MODULE], $this->_config->modules->toArray());
     $registry->directories = (object) ['engine' => ROOT_PATH . '/app/engine/', 'modules' => ROOT_PATH . '/app/modules/'];
     $di->set('registry', $registry);
     /**
      * Store config in the DI container.
      */
     $di->setShared('config', $this->_config);
     parent::__construct($di);
 }
Esempio n. 16
0
use App\Libary\Elements;
/**
 * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
 */
$di = new FactoryDefault();
/**
 * We register the events manager
 */
$di->set('dispatcher', function () use($di) {
    $eventsManager = new EventsManager();
    /**
     * Check if the user is allowed to access certain action using the SecurityPlugin
     */
    $eventsManager->attach('dispatch:beforeDispatch', new SecurityPlugin());
    /**
     * Handle exceptions and not-found exceptions using NotFoundPlugin
     */
    $eventsManager->attach('dispatch:beforeException', new NotFoundPlugin());
    $dispatcher = new Dispatcher();
    $dispatcher->setEventsManager($eventsManager);
    $dispatcher->setDefaultNamespace('app\\controllers');
    return $dispatcher;
});
/**
 * The URL component is used to generate all kind of urls in the application
 */
$di->set('url', function () use($config) {
    $url = new UrlProvider();
    $url->setBaseUri($config->application->baseUri);
    return $url;
});
Esempio n. 17
0
<?php

use Phalcon\Loader, Phalcon\DI\FactoryDefault, Phalcon\Mvc\Application, Phalcon\Mvc\View;
$loader = new Loader();
$loader->registerDirs(array('../apps/controllers/', '../apps/models/'))->register();
$di = new FactoryDefault();
// Registering the view component
$di->set('view', function () {
    $view = new View();
    $view->setViewsDir('../apps/views/');
    return $view;
});
try {
    $application = new Application($di);
    echo $application->handle()->getContent();
} catch (\Exception $e) {
    echo $e->getMessage();
}
Esempio n. 18
0
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Events\Manager as EventsManager;
/**
 * Framework
 */
$di = new FactoryDefault();
/**
 * Events manager
 */
$di->set('dispatcher', function () use($di) {
    $eventsManager = new EventsManager();
    //NotFound
    $eventsManager->attach('dispatch:beforeException', new NotFoundPlugin());
    $dispatcher = new Dispatcher();
    $dispatcher->setEventsManager($eventsManager);
    return $dispatcher;
});
/**
 * URL
 */
$di->set('url', function () use($config) {
    $url = new UrlProvider();
    $url->setBaseUri($config->application->baseUri);
    return $url;
});
/**
 * View
 */
$di->set('view', function () use($config) {
Esempio n. 19
0
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Mvc\Dispatcher;
use Twm\Db\Adapter\Pdo\Mssql as DbAdapter;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;
/**
 * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
 */
$di = new FactoryDefault();
/**
 * The URL component is used to generate all kind of urls in the application
 */
$di->set('url', function () use($config) {
    $url = new UrlResolver();
    $url->setBaseUri($config->application->baseUri);
    return $url;
}, true);
/**
 * The Dispatcher component
 */
$di->setShared('dispatcher', function () use($di) {
    $eventsManager = $di->getShared('eventsManager');
    $dispatcher = new Dispatcher();
    $dispatcher->setEventsManager($eventsManager);
    return $dispatcher;
});
/**
 * Setting up the view component
 */
$di->set('view', function () use($config) {
Esempio n. 20
0
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Db\Adapter\Pdo\Postgresql as DbAdapter;
try {
    // Register an autoloader
    $loader = new Loader();
    $loader->registerDirs(array('../app/controllers/', '../app/models/'))->register();
    // Setup the database service
    $di->set('db', function () {
        return new DbAdapter(array("host" => "pellefant-01.db.elephantsql.com", "username" => "yxlluzxv", "password" => "XlspU6IwYJrlKnn9drdaRfpRv2PjyFR5", "dbname" => "yxlluzxv"));
    });
    // Create a DI
    $di = new FactoryDefault();
    // Setup the view component
    $di->set('view', function () {
        $view = new View();
        $view->setViewsDir('../app/views/');
        return $view;
    });
    // Setup a base URI so that all generated URIs include the "tutorial" folder
    $di->set('url', function () {
        $url = new UrlProvider();
        $url->setBaseUri('');
        return $url;
    });
    // Handle the request
    $application = new Application($di);
    echo $application->handle()->getContent();
} catch (\Exception $e) {
    echo "Exception: ", $e->getMessage();
}
Esempio n. 21
0
 /**
  * Normally, the framework creates the Dispatcher automatically. In our case, we want to perform a verification
  * before executing the required action, checking if the user has access to it or not. To achieve this,
  * we have replaced the component by creating a function in the bootstrap
  * @param \Phalcon\DI\FactoryDefault     $di
  * @param null                           $security
  */
 public function registerSecureDispatcher($di, $security)
 {
     /**
      * @return Dispatcher
      */
     $di->set('dispatcher', function () use($di, $security) {
         $dispatcher = new Dispatcher();
         $dispatcher->setDefaultNamespace($this->default_namespace);
         /**
          * Obtain the standard eventsManager from the DI
          */
         $eventsManager = $di->getShared('eventsManager');
         /**
          * Listen for events produced in the dispatcher using the Security plugin
          */
         $eventsManager->attach('dispatch', $security);
         /**
          * Bind the EventsManager to the Dispatcher
          */
         $dispatcher->setEventsManager($eventsManager);
         return $dispatcher;
     });
 }
Esempio n. 22
0
<?php

use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Flash\Direct as Flash;
$di = new FactoryDefault();
$di->set('dispatcher', function () {
    $dispatcher = new Dispatcher();
    $dispatcher->setDefaultNamespace("TestApp\\Controllers");
    return $dispatcher;
});
$di->setShared('url', function () use($config) {
    $url = new UrlResolver();
    $url->setBaseUri($config->application->baseUri);
    return $url;
});
$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;
});
Esempio n. 23
0
use Phalcon\Config\Adapter\Ini as ConfigIni;
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\Dispatcher;
/**
 * Read the configuration
 */
$config = (require __DIR__ . '/config/config.php');
/**
 * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
 */
$di = new FactoryDefault();
/**
 * We register the events manager
 */
$di->set('dispatcher', function () use($di) {
    $dispatcher = new Dispatcher();
    return $dispatcher;
});
/**
 * Auto-loader configuration
 */
require __DIR__ . '/config/loader.php';
/**
 * Load application services
 */
require __DIR__ . '/config/services.php';
$application = new Application($di);
// register moudles
$modules = (require __DIR__ . '/config/modules.php');
foreach ($modules as $m) {
    $_modules[$m] = array('className' => "Application\\{$m}\\Module", 'path' => __DIR__ . "/{$m}/Module.php");
}
Esempio n. 24
0
use Phalcon\Session\Adapter\Files as SessionAdapter;
/**
 * We're a registering a set of directories taken from the configuration file
 */
$loader = new \Phalcon\Loader();
$loader->registerDirs(array($config->application->controllersDir, $config->application->modelsDir))->register();
/**
 * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
 */
$di = new FactoryDefault();
/**
 * The URL component is used to generate all kind of urls in the application
 */
$di->set('url', function () use($config) {
    $url = new UrlResolver();
    $url->setBaseUri($config->application->baseUri);
    return $url;
}, true);
/**
 * Setting up the view component
 */
$di->set('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;
}, true);
Esempio n. 25
0
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;
/**
 * The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
 */
$di = new FactoryDefault();
/**
 * Register config
 */
$di->setShared('config', $config);
/**
 * Registering a dispatcher
 */
$di->set('dispatcher', function () {
    $dispatcher = new \Phalcon\Mvc\Dispatcher();
    $dispatcher->setDefaultNamespace('Tools\\Controllers');
    return $dispatcher;
});
/**
 * Register routers
 */
$di->setShared('router', function () use($config) {
    $router = new \Phalcon\Mvc\Router();
    $router->removeExtraSlashes(true);
    $router->setDefaults(array('namespace' => 'Tools\\Controllers', 'controller' => 'index', 'action' => 'index'));
    $router->add('/:controller/:action/:params', array('namespace' => 'Tools\\Controllers', 'controller' => 1, 'action' => 2, 'params' => 3));
    return $router;
});
/**
 *  Register assets that will be loaded in every page
 */
Esempio n. 26
0
 /**
  * @return bool
  * @throws BuilderException
  */
 public function build()
 {
     if ($this->options->contains('directory')) {
         $this->path->setRootPath($this->options->get('directory'));
     }
     $name = $this->options->get('name');
     $config = $this->getConfig();
     if (!isset($config->database->adapter)) {
         throw new BuilderException('Adapter was not found in the config. Please specify a config variable [database][adapter].');
     }
     $adapter = 'Mysql';
     if (isset($config->database->adapter)) {
         $adapter = ucfirst($config->database->adapter);
         $this->isSupportedAdapter($adapter);
     }
     $di = new FactoryDefault();
     $di->set('db', function () use($adapter, $config) {
         if (is_object($config->database)) {
             $configArray = $config->database->toArray();
         } else {
             $configArray = $config->database;
         }
         $adapterName = 'Phalcon\\Db\\Adapter\\Pdo\\' . $adapter;
         unset($configArray['adapter']);
         return new $adapterName($configArray);
     });
     if (!isset($config->application->modelsDir)) {
         throw new BuilderException('The builder is unable to find the models directory.');
     }
     $modelPath = $config->application->modelsDir;
     if (false == $this->isAbsolutePath($modelPath)) {
         $modelPath = $this->path->getRootPath($config->application->modelsDir);
     }
     $this->options->offsetSet('modelsDir', rtrim($modelPath, '\\/') . DIRECTORY_SEPARATOR);
     if (!isset($config->application->controllersDir)) {
         throw new BuilderException('The builder is unable to find the controllers directory.');
     }
     $controllerPath = $config->application->controllersDir;
     if (false == $this->isAbsolutePath($controllerPath)) {
         $controllerPath = $this->path->getRootPath($config->application->controllersDir);
     }
     $this->options->offsetSet('controllersDir', rtrim($controllerPath, '\\/') . DIRECTORY_SEPARATOR);
     if (!isset($config->application->viewsDir)) {
         throw new BuilderException('The builder is unable to find the views directory.');
     }
     $viewPath = $config->application->viewsDir;
     if (false == $this->isAbsolutePath($viewPath)) {
         $viewPath = $this->path->getRootPath($config->application->viewsDir);
     }
     $this->options->offsetSet('viewsDir', $viewPath);
     $this->options->offsetSet('manager', $di->getShared('modelsManager'));
     $this->options->offsetSet('className', Text::camelize($this->options->get('name')));
     $this->options->offsetSet('fileName', Text::uncamelize($this->options->get('className')));
     $modelsNamespace = '';
     if ($this->options->contains('modelsNamespace') && $this->checkNamespace($this->options->get('modelsNamespace'))) {
         $modelsNamespace = $this->options->get('modelsNamespace');
     }
     $modelName = Text::camelize($name);
     if ($modelsNamespace) {
         $modelClass = '\\' . trim($modelsNamespace, '\\') . '\\' . $modelName;
     } else {
         $modelClass = $modelName;
     }
     $modelPath = $this->options->get('modelsDir') . $modelName . '.php';
     if (!file_exists($modelPath) || $this->options->get('force')) {
         $modelBuilder = new ModelBuilder(array('name' => $name, 'schema' => $this->options->get('schema'), 'className' => $this->options->get('className'), 'fileName' => $this->options->get('fileName'), 'genSettersGetters' => $this->options->get('genSettersGetters'), 'directory' => $this->options->get('directory'), 'force' => $this->options->get('force'), 'namespace' => $this->options->get('modelsNamespace')));
         $modelBuilder->build();
     }
     if (!class_exists($modelClass)) {
         require $modelPath;
     }
     $entity = new $modelClass();
     $metaData = $di['modelsMetadata'];
     $attributes = $metaData->getAttributes($entity);
     $dataTypes = $metaData->getDataTypes($entity);
     $identityField = $metaData->getIdentityField($entity);
     $primaryKeys = $metaData->getPrimaryKeyAttributes($entity);
     $setParams = array();
     $selectDefinition = array();
     $relationField = '';
     $single = $name;
     $this->options->offsetSet('name', strtolower(Text::camelize($single)));
     $this->options->offsetSet('plural', $this->_getPossiblePlural($name));
     $this->options->offsetSet('singular', $this->_getPossibleSingular($name));
     $this->options->offsetSet('modelClass', $modelClass);
     $this->options->offsetSet('entity', $entity);
     $this->options->offsetSet('setParams', $setParams);
     $this->options->offsetSet('attributes', $attributes);
     $this->options->offsetSet('dataTypes', $dataTypes);
     $this->options->offsetSet('primaryKeys', $primaryKeys);
     $this->options->offsetSet('identityField', $identityField);
     $this->options->offsetSet('relationField', $relationField);
     $this->options->offsetSet('selectDefinition', $selectDefinition);
     $this->options->offsetSet('autocompleteFields', array());
     $this->options->offsetSet('belongsToDefinitions', array());
     // Build Controller
     $this->_makeController();
     if ($this->options->get('templateEngine') == 'volt') {
         // View layouts
         $this->_makeLayoutsVolt();
         // View index.phtml
         $this->makeViewVolt('index');
         // View search.phtml
         $this->_makeViewSearchVolt();
         // View new.phtml
         $this->makeViewVolt('new');
         // View edit.phtml
         $this->makeViewVolt('edit');
     } else {
         // View layouts
         $this->_makeLayouts();
         // View index.phtml
         $this->makeView('index');
         // View search.phtml
         $this->_makeViewSearch();
         // View new.phtml
         $this->makeView('new');
         // View edit.phtml
         $this->makeView('edit');
     }
     return true;
 }
Esempio n. 27
0
 /**
  * Execute Phalcon Developer Tools
  *
  * @param  string             $path The path to the Phalcon Developer Tools
  * @param  string             $ip   Optional IP address for securing Developer Tools
  * @return void
  * @throws \Exception         if Phalcon extension is not installed
  * @throws \Exception         if Phalcon version is not compatible Developer Tools
  * @throws \Phalcon\Exception if Application config could not be loaded
  */
 public static function main($path, $ip = null)
 {
     if (!extension_loaded('phalcon')) {
         throw new \Exception('Phalcon extension is not installed, follow these instructions to install it: http://phalconphp.com/documentation/install');
     }
     if ($ip !== null) {
         self::$ip = $ip;
     }
     if (!defined('TEMPLATE_PATH')) {
         define('TEMPLATE_PATH', $path . '/templates');
     }
     chdir('..');
     // Read configuration
     $configPaths = array('config', 'app/config', 'apps/frontend/config');
     $readed = false;
     foreach ($configPaths as $configPath) {
         $cpath = $configPath . '/config.ini';
         if (file_exists($cpath)) {
             $config = new \Phalcon\Config\Adapter\Ini($cpath);
             $readed = true;
             break;
         } else {
             $cpath = $configPath . '/config.php';
             if (file_exists($cpath)) {
                 $config = (require $cpath);
                 $readed = true;
                 break;
             }
         }
     }
     if ($readed === false) {
         throw new \Phalcon\Exception('Configuration file could not be loaded!');
     }
     $loader = new \Phalcon\Loader();
     $loader->registerDirs(array($path . '/scripts/', $path . '/scripts/Phalcon/Web/Tools/controllers/'));
     $loader->registerNamespaces(array('Phalcon' => $path . '/scripts/'));
     $loader->register();
     if (Version::getId() < Script::COMPATIBLE_VERSION) {
         throw new \Exception('Your Phalcon version is not compatible with Developer Tools, download the latest at: http://phalconphp.com/download');
     }
     try {
         $di = new FactoryDefault();
         $di->set('view', function () use($path) {
             $view = new View();
             $view->setViewsDir($path . '/scripts/Phalcon/Web/Tools/views/');
             return $view;
         });
         $di->set('config', $config);
         $di->set('url', function () use($config) {
             $url = new \Phalcon\Mvc\Url();
             $url->setBaseUri($config->application->baseUri);
             return $url;
         });
         $di->set('flash', function () {
             return new \Phalcon\Flash\Direct(array('error' => 'alert alert-error', 'success' => 'alert alert-success', 'notice' => 'alert alert-info'));
         });
         $di->set('db', function () use($config) {
             if (isset($config->database->adapter)) {
                 $adapter = $config->database->adapter;
             } else {
                 $adapter = 'Mysql';
             }
             if (is_object($config->database)) {
                 $configArray = $config->database->toArray();
             } else {
                 $configArray = $config->database;
             }
             $className = 'Phalcon\\Db\\Adapter\\Pdo\\' . $adapter;
             unset($configArray['adapter']);
             return new $className($configArray);
         });
         self::$di = $di;
         $app = new \Phalcon\Mvc\Application();
         $app->setDi($di);
         echo $app->handle()->getContent();
     } catch (\Phalcon\Exception $e) {
         echo get_class($e), ': ', $e->getMessage(), "<br>";
         echo nl2br($e->getTraceAsString());
     } catch (\PDOException $e) {
         echo get_class($e), ': ', $e->getMessage(), "<br>";
         echo nl2br($e->getTraceAsString());
     }
 }
Esempio n. 28
0
    return $view;
});
/**
 * Database connection is created based in the parameters defined in the configuration file
 */
$di->setShared('db', function () use($config) {
    $dbConfig = $config->database->toArray();
    $adapter = $dbConfig['adapter'];
    unset($dbConfig['adapter']);
    $class = 'Phalcon\\Db\\Adapter\\Pdo\\' . $adapter;
    return new $class($dbConfig);
});
/**
 * Registering the Models-Metadata
 */
$di->set('modelsMetadata', 'Phalcon\\Mvc\\Model\\Metadata\\Files');
/*
 * Registering the Models Manager
 */
$di->set('modelsManager', 'Phalcon\\Mvc\\Model\\Manager');
/**
 * If the configuration specify the use of metadata adapter use it or use memory otherwise
 */
$di->setShared('modelsMetadata', function () {
    return new MetaDataAdapter();
});
/**
 * Register the session flash service with the Twitter Bootstrap classes
 */
$di->set('flash', function () {
    return new Flash(array('error' => 'alert alert-danger', 'success' => 'alert alert-success', 'notice' => 'alert alert-info', 'warning' => 'alert alert-warning'));
Esempio n. 29
0
<?php

use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;
$di = new FactoryDefault();
$di->set('url', function () use($config) {
    $url = new UrlResolver();
    $url->setBaseUri($config->application->baseUri);
    return $url;
}, true);
$di->set('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' => '_', 'compileAlways' => true));
        return $volt;
    }, '.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
    return $view;
}, true);
$di->set('db', function () use($config) {
    return new DbAdapter(array('host' => $config->database->host, 'username' => $config->database->username, 'password' => $config->database->password, 'dbname' => $config->database->dbname, "charset" => $config->database->charset));
});
$di->set('modelsMetadata', function () {
    return new MetaDataAdapter();
});
Esempio n. 30
0
 */
$config = (include __DIR__ . '/config.php');
if (file_exists(__DIR__ . '/config.' . APPLICATION_ENV . '.php')) {
    $overrideConfig = (include __DIR__ . '/config.' . APPLICATION_ENV . '.php');
    $config->merge($overrideConfig);
}
//It crreated when save in admin dashboard
if (file_exists(__DIR__ . '/options.php')) {
    $overrideConfig = new AdapterPhp(__DIR__ . '/options.php');
    $config->merge($overrideConfig);
}
if (file_exists(__DIR__ . '/config.menu.php')) {
    $overrideConfig = (include __DIR__ . '/config.menu.php');
    $config->merge($overrideConfig);
}
$di->set('config', $config, true);
// setup timezone
date_default_timezone_set($di->get('config')->application->timezone ?: 'UTC');
/**
 * Router
 */
$di->set('router', function () {
    return include ROOT_DIR . "/common/config/routes.php";
}, true);
/**
 * The URL component is used to generate all kind of urls in the application
 */
$di->set('url', function () use($di) {
    $url = new UrlResolver();
    $config = $di->get('config');
    $url->setBaseUri($config->application->baseUri);