Example #1
0
 protected function buildEndpoint()
 {
     $options = $this->options;
     if (isset($options['to']) && (is_string($options['to']) && ctype_upper(substr($options['to'], 0, 1)) || $options['to'] instanceof SerializableClosure)) {
         $this->endPoint = $options['to'];
         unset($this->options['to']);
         return;
     }
     $scope = $this->scope;
     $parts = ['namespaces' => []];
     if ($options['to']) {
         $token = new ActionToken($options['to']);
         $parts['controller'] = $token->controller();
         $parts['action'] = $token->action();
     } elseif (array_key_exists('action', $options) || isset($scope['action'])) {
         $parts['action'] = $options['action'] ?: $scope['action'];
         if (isset($scope['controller'])) {
             $parts['controller'] = $scope['controller'];
         } elseif (isset($scope['scopeLevelResource'])) {
             $parts['controller'] = $scope['scopeLevelResource']->controller();
         } else {
             throw new \UnexpectedValueException("Couldn't find controller option");
         }
     }
     if (isset($scope['namespace'])) {
         $parts['namespaces'][] = $scope['namespace'];
     }
     if (isset($options['namespace'])) {
         $parts['namespaces'][] = $options['namespace'];
     }
     $token = new ActionToken($parts);
     $this->to = $token;
     $this->controller = $token->controller();
     $this->action = $token->action();
     $this->namespaces = $token->namespaces();
 }
Example #2
0
 public function render($baseHelper, $model, $options, Closure $block = null)
 {
     if ($options instanceof Closure) {
         $block = $options;
         $options = [];
     }
     $inf = $baseHelper->getService('inflector');
     $modelClass = get_class($model);
     $controller = str_replace('/', ActionToken::NAMESPACE_SEPARATOR, lcfirst($inf->pluralize($modelClass)));
     // $primaryKey = $modelClass::primaryKey();
     $urlPath = null;
     if (isset($options['url'])) {
         if ($baseHelper->isUrl($options['url'])) {
             $urlPath = $options['url'];
         } else {
             if (is_array($options['url'])) {
                 $token = new ActionToken($options['url'][0]);
             } else {
                 # Action token (string) assumed.
                 $token = new ActionToken($options['url']);
             }
             $action = $token->action();
             $urlPath = $baseHelper->urlFor($token->toString());
         }
     } else {
         if ($id = $model->id()) {
             $action = 'update';
             $urlParams = [$controller . '#' . $action, $model];
         } else {
             $action = 'create';
             $urlParams = [$controller . '#' . $action];
         }
         $urlPath = $baseHelper->urlFor($urlParams);
     }
     if (!isset($options['html'])) {
         $htmlAttrs = [];
     } else {
         $htmlAttrs = $options['html'];
     }
     if (isset($options['as'])) {
         $inputNamespace = $options['as'];
     } else {
         $inputNamespace = null;
     }
     switch ($action) {
         case 'create':
             $resourceName = $baseHelper->objectName($model);
             if (empty($htmlAttrs['method'])) {
                 $htmlAttrs['method'] = 'post';
             }
             if (empty($htmlAttrs['id'])) {
                 $htmlAttrs['id'] = 'new_' . $resourceName;
             }
             $baseHelper->addClass($htmlAttrs, 'new_' . $resourceName);
             break;
         case 'update':
             $resourceName = $baseHelper->objectName($model);
             if (empty($htmlAttrs['method'])) {
                 $htmlAttrs['method'] = 'patch';
             }
             if (empty($htmlAttrs['id'])) {
                 $htmlAttrs['id'] = 'edit_' . $resourceName . '_' . $model->id();
             }
             $baseHelper->addClass($htmlAttrs, 'edit_' . $resourceName);
             break;
         case 'destroy':
             if (empty($htmlAttrs['method'])) {
                 $htmlAttrs['method'] = 'delete';
             }
             break;
     }
     if (!empty($options['remote'])) {
         $htmlAttrs['data-remote'] = 'true';
     }
     if (empty($options['builder'])) {
         $formBuilder = $baseHelper->helperSet()->invoke('getFormBuilder', [$model, $inputNamespace]);
     } else {
         # "builder" option could be the name of a class extending FormBuilder.
         $formBuilder = new $options['builder']($baseHelper->helperSet(), $model, $inputNamespace);
     }
     return $baseHelper->helperSet()->invoke('formTag', [$urlPath, $htmlAttrs, function () use($block, $formBuilder) {
         $formBuilder->runBlock($block);
     }]);
 }