/** * Set all definitions into array * @param $propertyInjections * @throws \Exception */ public function setPropertyInjection($propertyInjections) { if (!is_array($propertyInjections)) { throw new \Exception(__METHOD__ . " only accept parameter as array."); } foreach ($propertyInjections as $controller => $properties) { foreach ($properties as $key => $value) { $classInstance = Inflector::instance()->toNamespace($value); $this->definitions[$this->namespace . $controller][$key] = new $classInstance(); } } }
public function render($view, $values = array(), $ui_content = false) { $controller = Inflector::instance()->getClassNameFromNamespace(get_called_class()); $controller = strtolower(str_replace('Controller', '', $controller)); $viewPath = null; $viewPath = strpos($this->viewsFilePath, '.') == true ? str_replace('.', DS, $this->viewsFilePath) : $this->viewsFilePath; $path = getcwd() . DS . APPPATH . DS . $viewPath . DS . $controller . DS; if (is_object($this->tpl) && is_file($path . $view . $this->templateExtension)) { $this->template = $this->tpl->loadTemplate($controller . DS . $view . $this->templateExtension); return $this; } if (!file_exists($path . $view . '.view' . EXT) && !is_readable($path . $view . '.view' . EXT)) { throw new \Exception('The Path ' . $path . $view . '.view' . EXT . ' is invalid.'); } self::$name[strtolower($view)] = $view; $viewPage = ''; $viewPage = $path . self::$name[$view] . '.view' . EXT; if (is_readable($viewPage)) { $this->layout = Inflector::instance()->toDirectorySeparator($this->layout); if ($this->layout !== '') { // render view page into the layout $layout = getcwd() . DS . APPPATH . DS . $viewPath . DS . $this->layout . '.view' . EXT; //$this->view_path = $path.self::$name[$view].'.view'.EXT; // $layout; $this->assignToProperties($values); ob_start(); include $viewPage; $data = array(); $data['yield'] = ob_get_contents(); ob_get_clean(); extract($data); include $layout; $data = array(); $content = null; $output = ob_get_contents(); ob_get_clean(); echo $output; return $this; } if ($ui_content == true) { self::$uiContent = $ui_content; $this->view_path = $viewPage; $this->loadView(); return $this->content; } $this->view_path = $viewPage; $this->loadView(); return $this; } }
/** * Run validation rules and catch errors * * @return bool * @throws \Exception */ public function run() { $isValid = true; if (empty($this->rules)) { return true; } foreach ($this->rules as $key => $val) { $rules = explode('|', $val); foreach ($rules as $rule) { if (!strstr($rule, 'max') && !strstr($rule, 'min')) { $method = Inflector::instance()->toCameCase($rule); if (is_callable(array($this, $method)) === false) { throw new \Exception('Undefined method ' . __CLASS__ . ' ' . $method . ' called.'); } //echo $key."<br>"; if ($isValid === false) { $this->setErrors($key . self::ERROR, Inflector::camelize(str_replace('_', ' ', $key))); } $isValid = $this->{$method}($key); //$isValid = call_user_func(array($this, $rule[0]), array($key)); } else { $rule = explode(':', $rule); $method = Inflector::instance()->toCameCase($rule[0]); if (is_callable(array($this, $method)) === false) { throw new \Exception('Undefined method ' . __CLASS__ . ' ' . $method . ' called.'); } if ($isValid === false) { $this->setErrors($key . self::ERROR, Inflector::camelize(str_replace('_', ' ', $key))); } //$isValid = call_user_func_array(array($this, $rule[0]), array($key,$rule[1])); $isValid = $this->{$method}($key, $rule[1]); } } } return $isValid; }
/** * Covert dash-dot to namespace * * @param $key * @return string */ public function toNamespace($key) { $class = null; $class = explode('.', $key); $class = array_map('ucfirst', Inflector::instance()->classify($class)); $class = '\\' . implode('\\', $class); return $class; }
/** * Set controller and method name here * @param $arguments */ private function setUpControllerAndMethodName($arguments) { $expression = Helper::stringSplit($arguments[0]); $this->controller = Inflector::instance()->classify($expression[0]) . 'Controller'; $this->controllerWithNS = $this->namespace . $this->controller; $this->method = Inflector::instance()->toCameCase($expression[1]) . 'Action'; }
/** * @return mixed */ public function getTotalNumberOfPages() { $numRecords = null; $modelClass = Inflector::instance()->getClassNameFromNamespace(get_class($this->model)); $table = Inflector::instance()->tabilize($modelClass); $numRecords = $this->model->query("SELECT " . $this->count() . " as " . $this->numCount . " FROM `" . $table . "`")->getAll(); return $numRecords[0]->{$this->numCount}; }
/** * @param $actionName * @return string */ public function getActionName($actionName) { return Inflector::instance()->toCameCase(!isset($actionName) ? 'index' : $actionName) . 'Action'; }
private function update($args) { $query = $debugQuery = $x = ""; $updateBy = $updateValue = null; $updateMethod = null; $updateMethod = strtoupper(Inflector::instance()->tabilize(__FUNCTION__)); if (method_exists($this, 'beforeUpdate')) { Event::instance()->trigger('beforeUpdate', $this); } if (is_array($args) && !empty($args)) { $x = array_keys($args); $updateBy = $x[0]; $updateValue = $args[$x[0]]; } else { $updateBy = $this->primaryKey; $updateValue = $args; } $query .= $updateMethod . " `" . $this->database . "`.`" . $this->tableName . "` SET "; $debugQuery .= $updateMethod . " `" . $this->database . "`.`" . $this->tableName . "` SET "; $arrCount = count($this->attributes); $i = 0; foreach ($this->attributes as $key => $value) { $query .= " `" . $key . "` " . "=" . " '" . $value . "'" . " "; $debugQuery .= " `" . $key . "` " . "=" . " '" . $value . "'" . " "; $query .= $i < $arrCount - 1 ? ',' : ''; $debugQuery .= $i < $arrCount - 1 ? ',' : ''; $i++; } $query .= " WHERE " . $updateBy . " = :column"; $debugQuery .= " WHERE " . $updateBy . " = " . $updateValue; //$this->debugLastQuery($debugQuery); try { $statement = $this->getDatabaseConnection()->prepare($query); $statement->bindValue(':column', $updateValue); $statement->execute(); if (method_exists($this, 'afterUpdate')) { Event::instance()->trigger('afterUpdate', $this); } return $statement->rowCount(); } catch (\PDOException $exception) { throw new Exception($exception->getMessage()); } }
/** <code> * // Call the "index" method on the "user" controller * $response = $this->call('admin::user@index'); * * // Call the "user/admin" controller and pass parameters * $response = $this->call('modules.admin.user@profile', $arguments); * </code> */ public function call($resource, $arguments = array()) { //$expression = explode('@', $resource); list($name, $method) = explode('@', $resource); $method = $method . 'Action'; $class = array_map('ucfirst', explode('.', $name)); $className = Inflector::instance()->classify(end($class)) . 'Controller'; $namespace = str_replace(end($class), '', $class); $class = '\\' . ucfirst(APPPATH) . '\\' . implode('\\', $namespace) . $className; return call_user_func_array(array(new $class(), $method), $arguments); }
/** * Resolve all dependencies of your class and return instance of * your class * * @param $class string * @throws \Exception * @return object */ public function make($class) { $reflection = new Reflection(); $reflection->setClass($class); if (false === $reflection->reflectionClass->isInstantiable()) { throw new DependencyException("Cannot instantiate " . ($reflection->reflectionClass->isInterface() ? 'interface' : 'class') . " '{$class}'"); } $constructor = null; $constructorArgsCount = ''; if ($reflection->reflectionClass->hasMethod('__construct')) { $constructor = $reflection->reflectionClass->getConstructor(); $constructorArgsCount = $constructor->getNumberOfParameters(); $constructor->setAccessible(true); } // if class does not have explicitly defined constructor or constructor does not have parameters // get the new instance if (!isset($constructor) && is_null($constructor) || $constructorArgsCount < 1) { $this->storage[$class] = $reflection->reflectionClass->newInstance(); } else { $dependencies = $constructor->getParameters(); foreach ($dependencies as $dependency) { if (!is_null($dependency->getClass())) { //Get constructor class name $resolveClass = $dependency->getClass()->name; $reflectionParam = new ReflectionClass($resolveClass); // Application and Container cannot be injected into controller currently // since Application constructor is protected if ($reflectionParam->IsInstantiable()) { $constructorArgs[] = $this->makeInstance($resolveClass); } /* | Check if constructor dependency is Interface or not. | if interface we will check definition for the interface | and inject into controller constructor */ if (!$reflectionParam->IsInstantiable() && $reflectionParam->isInterface()) { $definition = $this->getDefinition(); $aliases = $definition()->registerAlias(); $interface = Inflector::instance()->getClassName($reflectionParam->getName()); if (array_key_exists($interface, $aliases)) { $constructorArgs[] = $this->makeInstance($aliases[$interface]); } } } else { /* | Check parameters are optional or not | if it is optional we will set the default value */ if ($dependency->isOptional()) { $constructorArgs[] = $dependency->getDefaultValue(); } } } $this->storage[$class] = $reflection->reflectionClass->newInstanceArgs($constructorArgs); } return $this->storage[$class]; }