/** * {@inheritdoc} */ public function register(MVC $mvc) { $this->pdo = new PDO($this->options['dsn'], $this->options['user'], $this->options['passwd'], $this->options['driverOptions']); if (!$mvc->hasCvpp('pdo')) { $mvc->setCvpp('pdo', $this->pdo); } }
/** * Init the Symfony Console Provider * * @return BaseController */ protected function initSymfonyConsoleProvider() { if (!$this->mvc->hasCvpp('symfony.console')) { $this->mvc->registerProvider(new ConsoleSymfonyProvider(array('modules' => $this->mvc->setModules(), 'commands' => array(new ListCommand())))); } return $this; }
/** * Register the properties of the Doctrine DBAL Provider * @access public * @param MVC $mvc * @return void */ public function register(MVC $mvc) { if (!$mvc->hasCvpp('dbal')) { $mvc->setCvpp('dbal', $this->connection); } $config = new Configuration(); $this->connection = DriverManager::getConnection($this->options, $config); }
/** * Example of index action for someone route with the use of the a User model * Using the render view * * @access public * @param MVC $mvc * @return string * @throws \LogicException */ public function index(MVC $mvc) { if (!$mvc->hasCvpp('pdo')) { throw new \LogicException('PDO don\'t exists. Register the MVC\\DataBase\\PdoProvider for use this function.'); } $userModel = new User($mvc->getCvpp('pdo')); $users = $userModel->findAll(); return $mvc->view()->render('User/index.html', array('users' => $users)); }
/** * Register the properties of the Monolog Provider * @access public * @param MVC $mvc * @return void */ public function register(MVC $mvc) { $defaultOptions = array('log_file' => $mvc->getAppDir() . '/logs/app_name.log', 'log_name' => 'app_name'); $options = array_merge($defaultOptions, $this->options); $logger = new Logger($options['log_name']); $logger->pushHandler(new StreamHandler($options['log_file'])); if (!$mvc->hasCvpp('monolog')) { $mvc->setCvpp('monolog', $logger); } }
/** * Register the Console Symfony * * @access public * @param MVC $mvc */ public function register(MVC $mvc) { $application = new Application('Simple PHP MVC', '1.5'); if (!$mvc->hasCvpp('symfony.console')) { $mvc->setCvpp('symfony.console', $application); } foreach ($this->modules as $module) { $module->registerCommands($mvc->getCvpp('symfony.console')); } }
/** * Call a action of controller * * @access public * @param MVC $mvc MVC Application object * @param string $method Method or Function of the Class Controller * @param string $fileView String of the view file * @return array Response array * @throws \LogicException */ public final function call(MVC $mvc, $method, $fileView = null) { if (!method_exists($this, $method)) { throw new \LogicException(sprintf('Method "s" don\'t exists.', $method)); } # Replace the view object $this->view = $mvc->view(); # Arguments of method $arguments = array(); # Create a reflection method $reflectionMethod = new \ReflectionMethod(get_class($this), $method); $reflectionParams = $reflectionMethod->getParameters(); foreach ($reflectionParams as $param) { if ($paramClass = $param->getClass()) { $className = $paramClass->name; if ($className === 'MVC\\MVC' || $className === '\\MVC\\MVC') { $arguments[] = $mvc; } elseif ($className === 'MVC\\Server\\HttpRequest' || $className === '\\MVC\\Server\\HttpRequest') { $arguments[] = $mvc->request(); } } else { foreach ($mvc->request()->params as $keyReqParam => $valueReqParam) { if ($param->name === $keyReqParam) { $arguments[] = $valueReqParam; break; } } } } $response = call_user_func_array($reflectionMethod->getClosure($this), $arguments); if (empty($response)) { throw new \LogicException('Response null returned.'); } if (is_string($response)) { $this->response['body'] = $response; } elseif ($mvc->request()->isAjax()) { $this->response['body'] = $this->renderJson($response); } elseif (is_array($response)) { if (!$fileView) { throw new \LogicException('File view is null.'); } $class = explode("\\", get_called_class()); $classname = end($class); // Class without Controller $classname = str_replace('Controller', '', $classname); $file = $classname . "/{$fileView}"; $this->response['body'] = $this->renderHtml($file, $response); } return $this->response; }
/** * Register the properties of the Twig Framework Provider * @access public * @param MVC $mvc * @return void */ public function register(MVC $mvc) { $defaultOptions = array('charset' => $mvc->getSetting('charset'), 'debug' => $mvc->getSetting('debug'), 'strict_variables' => $mvc->getSetting('debug'), 'templates_path' => array($mvc->getSetting('templates_path'))); $options = array_merge($defaultOptions, $this->options); $mvc->setCvpp('twig.loader.filesystem', new \Twig_Loader_Filesystem($options['path'])); $mvc->setCvpp('twig.loader.array', new \Twig_Loader_Array($options['templates_path'])); # Register templates path modules foreach ($mvc->getModules() as $module) { $module->registerTemplatesPathTwig($mvc); } $mvc->setCvpp('twig.loader', new \Twig_Loader_Chain(array($mvc->getCvpp('twig.loader.array'), $mvc->getCvpp('twig.loader.filesystem')))); $twig = new \Twig_Environment($mvc->getCvpp('twig.loader'), $options); $twig->addGlobal('mvc', $mvc); if ($options['debug']) { $twig->addExtension(new \Twig_Extension_Debug()); } $mvc->setCvpp('twig', $twig); }
/** * Register the properties of the Doctrine ORM Provider * * @access public * @param MVC $mvc * @return void */ public function register(MVC $mvc) { $default_options = array('params' => array('charset' => null, 'driver' => 'pdo_mysql', 'dbname' => null, 'host' => 'localhost', 'user' => 'root', 'password' => null, 'port' => null), 'dev_mode' => false, 'etities_type' => 'annotations', 'path_entities' => array(), 'proxy_dir' => null); $options = array_merge($default_options, $this->options); if (empty($options['path_entities']) || !is_array($options['path_entities'])) { throw new \Exception('Option path_entities should be an array of path files entities.'); } if ($options['etities_type'] == 'annotations') { $config = Setup::createAnnotationMetadataConfiguration($options['path_entities'], $options['dev_mode'], $options['proxy_dir']); } elseif ($options['etities_type'] == 'yaml' || $options['etities_type'] == 'yml') { $config = Setup::createYAMLMetadataConfiguration($options['path_entities'], $options['dev_mode'], $options['proxy_dir']); } elseif ($options['etities_type'] == 'xml') { $config = Setup::createXMLMetadataConfiguration($options['path_entities'], $options['dev_mode'], $options['proxy_dir']); } if ($mvc->hasCvpp('dbal')) { $entityManager = EntityManager::create($mvc->getCvpp('dbal'), $config); } else { $entityManager = EntityManager::create($options['params'], $config); } if (!$mvc->hasCvpp('dbal')) { $mvc->setCvpp('dbal', $entityManager->getConnection()); } $mvc->setCvpp('em', $entityManager); }
/** * Set MVC Application Routes * * @return Route[] */ public function setRoutes() { $routes = parent::setRoutes(); return $routes; }
function index(MVC $mvc) { $learnUri = $mvc->urlFor('app_learn', 'route'); return $mvc->view()->render('App/index.php', array('learnUri' => $learnUri)); }
/** * Redirect Action * * @param MVC $mvc * @return string */ public function redirectAction(MVC $mvc) { return $mvc->redirect('./redirected'); }
/** * {@inheritdoc} */ public function __construct() { parent::__construct(array('templates_path' => __DIR__ . '/TestModule/Resources/views')); }
/** * Register Templates Path Twig * * @param MVC $mvc */ public function registerTemplatesPathTwig(MVC $mvc) { $viewsPath = $this->getPath() . '/Resources/views'; if (file_exists(dirname($viewsPath)) && file_exists($viewsPath)) { $mvc->getCvpp('twig.loader.filesystem')->addPath($viewsPath); } }
/** * With MVC Application, HttpRequest and Route Params Action */ public function testWithRouteParamsAction() { $this->mvcApp->request()->params = array('foo_key' => 'foo_value'); $response = $this->fooCtrl->call($this->mvcApp, 'withRouteParamsAction'); $this->assertTrue(is_array($response) && !empty($response)); }