Example #1
0
 public function render($view = null, $layout = null)
 {
     $event = new CakeEvent('Controller.beforeRender', $this);
     $this->getEventManager()->dispatch($event);
     if ($event->isStopped()) {
         $this->autoRender = false;
         return $this->response;
     }
     if (!empty($this->uses) && is_array($this->uses)) {
         foreach ($this->uses as $model) {
             list($plugin, $className) = pluginSplit($model);
             $this->request->params['models'][$className] = compact('plugin', 'className');
         }
     }
     $viewClass = $this->viewClass;
     if ($this->viewClass != 'View') {
         list($plugin, $viewClass) = pluginSplit($viewClass, true);
         $viewClass = $viewClass . 'View';
         App::uses($viewClass, $plugin . 'View');
     } else {
         // ELSE IS CUSTOM In ORDER To Provide the right layout even were we are ( as plugin ... )
         list($plugin, $viewClass) = pluginSplit($viewClass, true);
         $viewClass = 'Trois' . 'View';
         App::uses($viewClass, 'Trois.' . 'View');
     }
     $View = new $viewClass($this);
     $models = ClassRegistry::keys();
     foreach ($models as $currentModel) {
         $currentObject = ClassRegistry::getObject($currentModel);
         if (is_a($currentObject, 'Model')) {
             $className = get_class($currentObject);
             list($plugin) = pluginSplit(App::location($className));
             $this->request->params['models'][$currentObject->alias] = compact('plugin', 'className');
             $View->validationErrors[$currentObject->alias] =& $currentObject->validationErrors;
         }
     }
     $this->autoRender = false;
     $this->View = $View;
     $this->response->body($View->render($view, $layout));
     return $this->response;
 }
Example #2
0
 /**
  * Instantiates the correct view class, hands it its data, and uses it to render the view output.
  *
  * @param string $view View to use for rendering
  * @param string $layout Layout to use
  * @return CakeResponse A response object containing the rendered view.
  * @triggers Controller.beforeRender $this
  * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::render
  */
 public function render($view = null, $layout = null)
 {
     $event = new CakeEvent('Controller.beforeRender', $this);
     $this->getEventManager()->dispatch($event);
     if ($event->isStopped()) {
         $this->autoRender = false;
         return $this->response;
     }
     if (!empty($this->uses) && is_array($this->uses)) {
         foreach ($this->uses as $model) {
             list($plugin, $className) = pluginSplit($model);
             $this->request->params['models'][$className] = compact('plugin', 'className');
         }
     }
     $this->View = $this->_getViewObject();
     $models = ClassRegistry::keys();
     foreach ($models as $currentModel) {
         $currentObject = ClassRegistry::getObject($currentModel);
         if ($currentObject instanceof Model) {
             $className = get_class($currentObject);
             list($plugin) = pluginSplit(App::location($className));
             $this->request->params['models'][$currentObject->alias] = compact('plugin', 'className');
             $this->View->validationErrors[$currentObject->alias] =& $currentObject->validationErrors;
         }
     }
     $this->autoRender = false;
     $this->response->body($this->View->render($view, $layout));
     return $this->response;
 }
 /**
  * Combine add and edit views
  *
  * @see Controller::render()
  */
 public function render($view = null, $layout = null)
 {
     list($plugin, ) = pluginSplit(App::location(get_parent_class($this)));
     if ($plugin) {
         App::build(array('View' => array(CakePlugin::path($plugin) . 'View' . DS)), App::APPEND);
     }
     if (strpos($view, '/') !== false || $this instanceof CakeErrorController) {
         return parent::render($view, $layout);
     }
     $fallbackView = $this->__getDefaultFallbackView();
     if (is_null($view) && in_array($this->request->action, array('admin_edit', 'admin_add', 'edit', 'add'))) {
         $viewPaths = App::path('View', $this->plugin);
         $themePath = $this->theme ? App::themePath($this->theme) : null;
         $searchPaths = array_merge((array) $themePath, $viewPaths);
         $view = $this->__findRequestedView($searchPaths);
         if (empty($view)) {
             $view = $fallbackView;
         }
     }
     return parent::render($view, $layout);
 }
Example #4
0
 /**
  * Tests that  App::location() returns the defined path for a class
  *
  * @return void
  */
 public function testClassLocation()
 {
     App::uses('MyCustomClass', 'MyPackage/Name');
     $this->assertEquals('MyPackage/Name', App::location('MyCustomClass'));
 }
Example #5
0
 /**
 	 75:  * Gets a reference to a DataSource object
 	 76:  *
 	 77:  * @param string $name The name of the DataSource, as defined in app/Config/database.php
 	 78:  * @return DataSource Instance
 	 79:  * @throws MissingDatasourceException
 	 80:  */
 public static function getDataSource($name)
 {
     if (empty(self::$_init)) {
         self::_init();
     }
     if (!empty(self::$_dataSources[$name])) {
         return self::$_dataSources[$name];
     }
     if (empty(self::$_connectionsEnum[$name])) {
         self::_getConnectionObject($name);
     }
     self::loadDataSource($name);
     $conn = self::$_connectionsEnum[$name];
     $class = $conn['classname'];
     if (strpos(App::location($class), 'Datasource') === false) {
         throw new MissingDatasourceException(array('class' => $class, 'plugin' => null, 'message' => 'Datasource is not found in Model/Datasource package.'));
     }
     self::$_dataSources[$name] = new $class(self::$config->{$name});
     self::$_dataSources[$name]->configKeyName = $name;
     return self::$_dataSources[$name];
 }
Example #6
0
/**
 * Instantiates the correct view class, hands it its data, and uses it to render the view output.
 *
 * @param string $view View to use for rendering
 * @param string $layout Layout to use
 * @return CakeResponse A response object containing the rendered view.
 * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::render
 */
	public function render($view = null, $layout = null) {
		$this->beforeRender();
		$this->Components->trigger('beforeRender', array(&$this));

		$viewClass = $this->viewClass;
		if ($this->viewClass != 'View') {
			list($plugin, $viewClass) = pluginSplit($viewClass, true);
			$viewClass = $viewClass . 'View';
			App::uses($viewClass, $plugin . 'View');
		}

		$View = new $viewClass($this);

		if (!empty($this->uses)) {
			foreach ($this->uses as $model) {
				list($plugin, $className) = pluginSplit($model);
				$this->request->params['models'][$className] = compact('plugin', 'className');
			}
		} if (!empty($this->modelClass) && ($this->uses === false || $this->uses === array())) {
			$this->request->params['models'][$this->modelClass] = array('plugin' => $this->plugin, 'className' => $this->modelClass);
		}

		$models = ClassRegistry::keys();
		foreach ($models as $currentModel) {
			$currentObject = ClassRegistry::getObject($currentModel);
			if (is_a($currentObject, 'Model')) {
				$className = get_class($currentObject);
				list($plugin) = pluginSplit(App::location($className));
				$this->request->params['models'][$currentObject->alias] = compact('plugin', 'className');
				$View->validationErrors[$currentObject->alias] =& $currentObject->validationErrors;
			}
		}

		$this->autoRender = false;
		$this->View = $View;
		$this->response->body($View->render($view, $layout));
		return $this->response;
	}
Example #7
0
 protected function _correctFile($file)
 {
     $fileContent = $content = file_get_contents($file);
     preg_match_all('/class \\w+ extends (.+)\\s*{/', $fileContent, $matches);
     if (empty($matches)) {
         continue;
     }
     $excludes = array('Fixture', 'Exception', 'TestSuite', 'CakeTestModel');
     $missingClasses = array();
     foreach ($matches[1] as $match) {
         $match = trim($match);
         preg_match('/\\bApp\\:\\:uses\\(\'' . $match . '\'/', $fileContent, $usesMatches);
         if (!empty($usesMatches)) {
             continue;
         }
         preg_match('/class ' . $match . '\\s*(w+)?{/', $fileContent, $existingMatches);
         if (!empty($existingMatches)) {
             continue;
         }
         if (in_array($match, $missingClasses)) {
             continue;
         }
         $break = false;
         foreach ($excludes as $exclude) {
             if (strposReverse($match, $exclude) === 0) {
                 $break = true;
                 break;
             }
         }
         if ($break) {
             continue;
         }
         $missingClasses[] = $match;
     }
     if (empty($missingClasses)) {
         return;
     }
     $fileContent = explode(LF, $fileContent);
     $inserted = array();
     $pos = 1;
     if (!empty($fileContent[1]) && $fileContent[1] === '/**') {
         $count = count($fileContent);
         for ($i = $pos; $i < $count - 1; $i++) {
             if (strpos($fileContent[$i], '*/') !== false) {
                 if (strpos($fileContent[$i + 1], 'class ') !== 0) {
                     $pos = $i + 1;
                 }
                 break;
             }
         }
     }
     // try to find the best position to insert app uses statements
     foreach ($fileContent as $row => $rowValue) {
         preg_match('/^App\\:\\:uses\\(/', $rowValue, $matches);
         if ($matches) {
             $pos = $row;
             break;
         }
     }
     foreach ($missingClasses as $missingClass) {
         $classes = array('Controller' => 'Controller', 'Component' => 'Controller/Component', 'Shell' => 'Console/Command', 'Model' => 'Model', 'Behavior' => 'Model/Behavior', 'Datasource' => 'Model/Datasource', 'Task' => 'Console/Command/Task', 'View' => 'View', 'Helper' => 'View/Helper');
         $type = null;
         foreach ($classes as $class => $namespace) {
             if (($t = strposReverse($missingClass, $class)) === 0) {
                 $type = $namespace;
                 break;
             }
         }
         if (empty($type)) {
             $this->err($missingClass . ' (' . $file . ') could not be matched');
             continue;
         }
         if ($class === 'Model') {
             $missingClassName = $missingClass;
         } else {
             $missingClassName = substr($missingClass, 0, strlen($missingClass) - strlen($class));
         }
         $objects = App::objects(($this->params['plugin'] ? $this->params['plugin'] . '.' : '') . $class);
         //FIXME: correct result for plugin classes
         if ($missingClass === 'Component') {
             $type = 'Controller';
         } elseif ($missingClass === 'Helper') {
             $type = 'View';
         } elseif ($missingClass === 'ModelBehavior') {
             $type = 'Model';
         } elseif (!empty($this->params['plugin']) && ($location = App::location($missingClass))) {
             $type = $location;
         } elseif (in_array($missingClass, $objects)) {
             $type = ($this->params['plugin'] ? $this->params['plugin'] . '.' : '') . $type;
         }
         $inserted[] = 'App::uses(\'' . $missingClass . '\', \'' . $type . '\');';
     }
     if (!$inserted) {
         return;
     }
     array_splice($fileContent, $pos, 0, $inserted);
     $fileContent = implode(LF, $fileContent);
     if (empty($this->params['dry-run'])) {
         file_put_contents($file, $fileContent);
         $this->out(__d('cake_console', 'Correcting %s', $file), 1, Shell::VERBOSE);
     }
 }