Exemplo n.º 1
0
 public static function getMeta($entityName)
 {
     $tableName = new String(String::set($entityName)->toLower()->split('\\.')->last());
     $entityName = String::set($entityName)->replace('.', '\\');
     $refClass = new \ReflectionClass((string) $entityName);
     $tokens = token_get_all(file_get_contents($refClass->getFileName()));
     $meta = array('System.Data.Entity.Attributes.Table' => new Attributes\Table((string) $tableName), 'System.Data.Entity.Attributes.Key' => new Attributes\Key((string) $tableName->append('_id')), 'Columns' => array());
     $tmp = array();
     $exitLoop = false;
     foreach ($tokens as $token) {
         switch ($token[0]) {
             case T_DOC_COMMENT:
                 $lines = explode(PHP_EOL, $token[1]);
                 foreach ($lines as $line) {
                     $strAttr = trim(str_replace(array('*', '/'), '', $line));
                     if ($strAttr) {
                         $attribute = String::set($strAttr)->get('@', '(');
                         $args = String::set($strAttr)->get('(', ')');
                         if (!$attribute->indexOf('.')) {
                             $attribute = $attribute->prepend('System.Data.Entity.Attributes.');
                         }
                         try {
                             $meta[(string) $attribute] = Object::getInstance($attribute, str_getcsv($args, ','));
                         } catch (\Exception $e) {
                             throw new \System\Data\Entity\EntityException('The attribute "' . (string) $attribute . '" does not exist.');
                         }
                     }
                 }
                 break;
             case T_COMMENT:
                 $attribute = String::set($token[1])->subString(2)->get('@', '(');
                 $args = String::set($token[1])->get('(', ')', true);
                 if (!$attribute->indexOf('.')) {
                     $attribute = $attribute->prepend('System.Data.Entity.Attributes.');
                 }
                 if ((string) $args) {
                     $tmp[] = Object::getInstance($attribute, str_getcsv($args, ','));
                 } else {
                     $tmp[] = Object::getInstance($attribute);
                 }
                 break;
             case T_VARIABLE:
                 $property = String::set($token[1])->subString(1);
                 $meta['Columns'][(string) $property] = $tmp;
                 $tmp = array();
                 break;
             case T_FUNCTION:
                 $exitLoop = true;
                 break;
         }
         if ($exitLoop) {
             break;
         }
     }
     $metaData = new EntityMeta((string) $entityName, $meta);
     return $metaData;
 }
Exemplo n.º 2
0
 public function render(ViewContext $viewContext)
 {
     $request = $viewContext->getHttpContext()->getRequest();
     $response = $viewContext->getHttpContext()->getResponse();
     $routeData = $viewContext->getRouteData();
     $viewFile = String::set($this->viewFilePattern)->prepend(Environment::getControllerPath())->replace('@module', String::set($routeData->module)->toLower()->toUpperFirst())->replace('@controller', String::set($routeData->controller)->toLower()->toUpperFirst())->replace('@action', String::set($routeData->action)->toLower()->toUpperFirst())->append('.php')->replace('\\', '/');
     if (file_exists($viewFile)) {
         extract($viewContext->getViewBag()->toArray());
         ob_start();
         require_once $viewFile;
         $this->output['view'] = ob_get_clean();
         if ($this->layoutFile) {
             if (substr($this->layoutFile, 0, 1) == '~') {
                 $this->layoutFile = \System\Std\Environment::getControllerPath() . substr($this->layoutFile, 1);
             }
             if (file_exists($this->layoutFile)) {
                 ob_start();
                 require_once $this->layoutFile;
                 $this->output['layoutFile'] = ob_get_clean();
             } else {
                 throw new ViewNotFoundException(sprintf("The layout file '%s' was not found", $this->layoutFile));
             }
         }
         if (isset($this->output['layoutFile'])) {
             return $this->output['layoutFile'];
         }
         return $this->output['view'];
     } else {
         throw new ViewNotFoundException(sprintf("The view '%s' was not found.", $viewFile));
     }
 }
Exemplo n.º 3
0
 public function execute($httpRequest, $route, $defaults = array())
 {
     $defaults = new Dictionary($defaults);
     $uri = $httpRequest->getUri();
     $tokens = String::set($route)->tokenize('{', '}');
     $uriPattern = $uri;
     foreach ($tokens as $token) {
         $uriPattern = str_replace($token, '@', $uriPattern);
     }
     $uriSegments = String::set($uriPattern)->split('@');
     $counter = 0;
     foreach ($tokens as $idx => $token) {
         if (substr($token, 0, 1) == '{') {
             $tokenName = String::set($token)->get('{', '}');
             $tokens[$idx] = $uriSegments->get($counter);
             if ($tokens[$idx]) {
                 $defaults->set((string) $tokenName, $tokens[$idx]);
             }
             ++$counter;
         }
     }
     if ($uri == trim(join('', $tokens), '/')) {
         $httpRequest->getRouteData()->merge($defaults);
         $httpRequest->getParam()->merge($defaults);
         return $httpRequest->getRouteData();
     }
     return false;
 }
Exemplo n.º 4
0
 public function __construct($name = '')
 {
     $dataFile = \System\Std\String::set(dirname(__FILE__) . '/Data/' . $name . '.xml')->replace('\\', '/');
     if (is_file($dataFile)) {
         $this->xml = simplexml_load_file($dataFile);
     } else {
         throw new \Exception(sprintf("Culture '%s' is not supported", $name));
     }
     $this->numberFormat = new NumberFormat($this->xml->numberFormat);
 }
Exemplo n.º 5
0
 public function render()
 {
     if (is_object($this->params)) {
         $this->params = \System\Std\Object::getProperties($this->params);
     }
     if (is_array($this->params)) {
         $href = $this->attributes['href'];
         foreach ($this->params as $param => $val) {
             $href = \System\Std\String::set($href)->replace('@' . $param, $val);
         }
         $this->attributes['href'] = $href;
     }
     return $this->control->append('<a ')->append($this->renderAttributes())->append('>')->append($this->title)->append('</a>')->toString();
 }
Exemplo n.º 6
0
 public function __construct(SqlQuery $sqlQuery, $fields, $entityName)
 {
     $this->sqlQuery = $sqlQuery;
     $this->fields = $fields;
     if (array_key_exists($entityName, $this->metaCollection)) {
         $metaData = $this->metaCollection[$entityName];
     } else {
         $metaData = MetaReader::getMeta($entityName);
         $this->metaCollection[$entityName] = $metaData;
     }
     $tableAlias = $this->getTableNameAlias($metaData->getTable()->getTableName());
     $this->lastTableAlias = $tableAlias;
     $this->sql = \System\Std\String::set('SELECT ')->append($fields)->append(' FROM ')->append($metaData->getTable()->getTableName())->append(' ' . $tableAlias)->append(PHP_EOL);
 }
Exemplo n.º 7
0
 /**
  * Gets a System.Std.String where all elements are join using the specified glue.
  * 
  * @method  join
  * @param   string $glue
  * @param   bool $removeEmptyEntries = true
  * @return  System.Std.String
  */
 public function join($glue, $removeEmptyEntries = true)
 {
     return \System\Std\String::join($glue, $this->collection, $removeEmptyEntries);
 }
Exemplo n.º 8
0
 /**
  * Dispatches a controller. This method is declared final and cannot be overriden.
  * 
  * @method  run
  * @return  void
  */
 public final function run()
 {
     if ($this->routes->count() == 0) {
         throw new \RuntimeException('One or more routes must be registered.');
     }
     $controllerDispacthed = false;
     foreach ($this->routes as $route) {
         $route->setHttpRequest($this->httpContext->getRequest());
         $routeData = $route->execute();
         if ($routeData) {
             $namespace = '';
             if (Environment::getRootPath() != Environment::getControllerPath()) {
                 $namespace = String::set(Environment::getControllerPath())->replace(Environment::getRootPath(), '')->trim('/');
             }
             $moduleName = $routeData->get('module') ? $routeData->get('module') . '.' : '';
             $class = String::set(sprintf('%s.%sControllers.%sController', $namespace, ucfirst(strtolower($moduleName)), ucfirst(strtolower($routeData->get('controller')))));
             try {
                 $controller = Object::getInstance($class);
                 $controller->getRegistry()->merge(get_object_vars($this));
             } catch (\ReflectionException $e) {
                 throw new Mvc\ControllerNotFoundException(sprintf("The controller '%s' does not exist.", $class));
             }
             if (!$controller instanceof Mvc\Controller) {
                 throw new Mvc\MvcException(sprintf("The controller '%s' does not inherit from System\\Web\\Mvc\\Controller.", $class));
             }
             $moduleClassName = String::set(sprintf('%s.%sControllers.%s', $namespace, ucfirst($moduleName), 'Module'));
             $this->authenticateRequest($controller);
             $this->preAction($controller);
             $moduleInstance = Object::getInstance($moduleClassName, null, false);
             if ($moduleInstance) {
                 if (method_exists($moduleInstance, 'load')) {
                     $moduleInstance->load($controller);
                 }
             }
             $controller->execute($this->httpContext);
             if ($moduleInstance) {
                 if (method_exists($moduleInstance, 'unload')) {
                     $moduleInstance->unload($controller);
                 }
             }
             $this->postAction($controller);
             $controllerDispacthed = true;
             break;
         }
     }
     if (false === $controllerDispacthed) {
         throw new Mvc\MvcException("Unable to dispatch a controller. None of the registered routes matched the request URI.");
     }
 }
Exemplo n.º 9
0
 public function __construct($entity)
 {
     $this->entityHash = spl_object_hash($entity);
     $this->entityName = \System\Std\String::set(get_class($entity))->trim('\\')->replace('\\', '.')->toString();
     $this->entity = $entity;
 }
Exemplo n.º 10
0
 public function execute(HttpContext $httpContext, array $routeData = array())
 {
     $this->httpContext = $httpContext;
     $routeData = $httpContext->getRequest()->getRouteData()->set('controller', (string) String::set(get_called_class())->get('\\', 'Controller', String::LAST_LAST)->toLower())->merge($routeData);
     $refClass = new \ReflectionClass(get_class($this));
     $actionName = $routeData->get('action');
     $requestMethod = $this->httpContext->getRequest()->getHttpMethod();
     if (in_array($requestMethod, array('POST', 'PUT', 'DELETE', 'AJAX')) && $refClass->hasMethod($actionName . $requestMethod)) {
         $actionName .= ucfirst(strtolower($requestMethod));
     }
     if (!$refClass->hasMethod($actionName)) {
         throw new ActionNotFoundException(sprintf("The action '%s' does not exist in '%s'", $actionName, get_called_class()));
     }
     $actionMethod = $refClass->getMethod($actionName);
     $methodParams = $actionMethod->getParameters();
     $requestParams = $this->httpContext->getRequest()->getParam();
     $args = array();
     foreach ($methodParams as $param) {
         $object = $param->getClass();
         if (is_object($object)) {
             $modelName = $object->getName();
             $params = $this->httpContext->getRequest()->toArray();
             $refClass = new \ReflectionClass($modelName);
             $modelInstance = $refClass->newInstanceArgs(array());
             $properties = $refClass->getProperties();
             foreach ($properties as $property) {
                 $property->setAccessible(true);
                 $name = $property->getName();
                 if (array_key_exists($name, $params)) {
                     $property->setValue($modelInstance, $params[$name]);
                 }
             }
             $args[] = $modelInstance;
         } else {
             $value = $requestParams->get($param->getName());
             if (strlen($value) == 0 && $param->isOptional()) {
                 $value = $param->getDefaultValue();
             }
             $args[] = $value;
         }
     }
     $this->load();
     $actionResult = $actionMethod->invokeArgs($this, $args);
     if (!$actionResult) {
         $actionResult = new ActionResult();
     } elseif (!$actionResult instanceof ActionResult) {
         $actionResult = new StringResult($actionResult);
     }
     $this->render($actionResult);
 }