/** * @param string $name command name (case-insensitive) * * @return \CConsoleCommand The command object. Null if the name is invalid. */ public function createCommand($name) { $name = StringHelper::toLowerCase($name); $command = null; if (isset($this->commands[$name])) { $command = $this->commands[$name]; } else { $commands = array_change_key_case($this->commands); if (isset($commands[$name])) { $command = $commands[$name]; } } if ($command !== null) { if (is_string($command)) { $className = 'NerdsAndCompany\\Schematic\\ConsoleCommands\\' . IOHelper::getFileName($command, false); return new $className($name, $this); } else { // an array configuration return Craft::createComponent($command, $name, $this); } } elseif ($name === 'help') { return new \CHelpCommand('help', $this); } else { return; } }
/** * Returns the requested elements as JSON * * @param callable|null $configFactory A function for generating the config * @param array|null $config The API endpoint configuration * * @throws Exception * @throws HttpException */ public function actionGetElements($configFactory = null, array $config = null) { if ($configFactory !== null) { $params = craft()->urlManager->getRouteParams(); $variables = isset($params['variables']) ? $params['variables'] : null; $config = $this->_callWithParams($configFactory, $variables); } // Merge in default config options $config = array_merge(['paginate' => true, 'pageParam' => 'page', 'elementsPerPage' => 100, 'first' => false, 'transformer' => 'Craft\\ElementApi_ElementTransformer'], craft()->config->get('defaults', 'elementapi'), $config); if ($config['pageParam'] == 'p') { throw new Exception('The pageParam setting cannot be set to "p" because that’s the parameter Craft uses to check the requested path.'); } if (!isset($config['elementType'])) { throw new Exception('Element API configs must specify the elementType.'); } /** @var ElementCriteriaModel $criteria */ $criteria = craft()->elements->getCriteria($config['elementType'], ['limit' => null]); if (!empty($config['criteria'])) { $criteria->setAttributes($config['criteria']); } // Load Fractal $pluginPath = craft()->path->getPluginsPath() . 'elementapi/'; require $pluginPath . 'vendor/autoload.php'; $fractal = new Manager(); $fractal->setSerializer(new ArraySerializer()); // Define the transformer if (is_callable($config['transformer']) || $config['transformer'] instanceof TransformerAbstract) { $transformer = $config['transformer']; } else { Craft::import('plugins.elementapi.ElementApi_ElementTransformer'); $transformer = Craft::createComponent($config['transformer']); } if ($config['first']) { $element = $criteria->first(); if (!$element) { throw new HttpException(404); } $resource = new Item($element, $transformer); } else { if ($config['paginate']) { // Create the paginator require $pluginPath . 'ElementApi_PaginatorAdapter.php'; $paginator = new ElementApi_PaginatorAdapter($config['elementsPerPage'], $criteria->total(), $config['pageParam']); // Fetch this page's elements $criteria->offset = $config['elementsPerPage'] * ($paginator->getCurrentPage() - 1); $criteria->limit = $config['elementsPerPage']; $elements = $criteria->find(); $paginator->setCount(count($elements)); $resource = new Collection($elements, $transformer); $resource->setPaginator($paginator); } else { $resource = new Collection($criteria, $transformer); } } JsonHelper::sendJsonHeaders(); echo $fractal->createData($resource)->toJson(); // End the request craft()->end(); }