/** * Gets a project by name * * @param string $name The project name * * @return \Codex\Projects\Project * @throws \Codex\Exception\CodexException */ public function get($name) { if (!$this->has($name)) { throw CodexException::projectNotFound((string) $name); } return parent::get($name); }
protected function hasOrDie($theme) { if (false === $this->has($theme)) { throw CodexException::because("Theme [{$theme}] does not exist"); } return $this; }
protected function query($query) { try { $data = $this->codex->get($query); } catch (Exception $e) { throw $e; } if ($data === null) { throw CodexException::because("Query '{$query}' has no valid result"); } return $data; }
/** * @param null $ext * * @return mixed|\Codex\Codex * @throws \Codex\Exception\CodexException */ function codex($ext = null) { if (!app()->bound('codex')) { //|| !app()->hasBeenBootstrapped()){ throw \Codex\Exception\CodexException::because('Codex is not bound yet'); } /** @var mixed $codex */ $codex = app('codex'); if ($ext === null) { return $codex; } return $codex->{$ext}; }
/** * Defines a point where hooks can hook * * @param string $event The hook name * @param array $args (optional) The arguments to pass along * @param bool $halt (optional) If this hook can halt * * @return array|null */ protected function hookPoint($event, array $args = [], $halt = true) { if (config('codex.dev.enabled', false) === true) { if (array_key_exists($event, static::$hookPoints)) { throw CodexException::because('hook point already exists'); } Arr::add(static::$hooks, $event, static::getDispatcher()->getListeners($event)); $caller = $this->hookPointGetCaller(debug_backtrace(), 1); $caller = array_only($caller, ['function', 'class']); Arr::set(static::$hookPoints, static::getEventName($event), $caller); } return $this->fireEvent($event, $args, $halt); }
public function handle(Menu $menu, $project, $ref) { $project = $project instanceof Project ? $project : $this->codex->projects->get($project); $ref = $ref instanceof Ref ? $ref : $project->refs->get($ref); $this->menu = $menu; $this->project = $project; $this->ref = $ref; $menu->setView($this->codex->view('menus.sidebar')); $this->menu->clear(); $items = $ref->config('menu', []); if (!is_array($items)) { throw CodexException::invalidMenuConfiguration(": menu.yml in [{$this}]"); } $this->recurse($items); }
/** * Check if the model needs to be booted and if so, do it. * * @throws \Codex\Exception\ContractMissingException */ protected function bootIfNotBooted() { $class = get_class($this); if (!class_implements($class, Hookable::class)) { throw CodexException::implementationMismatch(Hookable::class); } if (!class_implements($class, Bootable::class)) { throw CodexException::implementationMismatch(Bootable::class); } if (!isset(static::$booted[$class])) { static::$booted[$class] = true; $this->fireEvent('booting', false); static::boot(); $this->fireEvent('booted', false); } }
public function getVersion() { if ($this->isVersion() === false) { throw CodexException::because("Can not getVersion for Ref {$this->project}/{$this}. The Ref is not a semver. Check by using isVersion() first."); } return $this->version; }
public function setCacheMode($mode) { if ($mode === true) { $mode = self::CACHE_ENABLED; } elseif ($mode === false) { $mode = self::CACHE_DISABLED; } elseif ($mode === null) { $mode = self::CACHE_AUTO; } if (!in_array($mode, [self::CACHE_ENABLED, self::CACHE_DISABLED, self::CACHE_AUTO], true)) { throw CodexException::because('Cache mode not supported: ' . (string) $mode); } $this->mode = $mode; }
public function getSorted($names) { $all = $this->whereIn('name', $names)->sortBy('priority')->replaceProcessors(); $all->each(function ($processor) use($all) { /** @var Processor $annotation */ $annotation = $processor['annotation']; foreach ($annotation->before as $before) { $otherProcessor = $all->where('name', $before)->first(); if ($otherProcessor !== null && false === in_array($annotation->name, $otherProcessor['after'], true)) { $otherProcessor['after'][] = $annotation->name; $all->set($before, $otherProcessor); } } }); $sorter = new Sorter(); foreach ($all as $item) { $sorter->addItem($item['name'], $item['after']); } $sorted = $sorter->sort(); if (count($sorter->getMissing()) > 0) { if (static::$handleMissing === self::MISSING_IGNORED) { } elseif (static::$handleMissing === self::MISSING_THROWS_EXECEPTION) { $dep = array_keys($sorter->getMissing()); $dep = implode(', ', $dep); throw CodexException::because("Sorter encountered a missing dependency for {$dep}"); } } $sorted = array_merge($sorted, array_diff($names, $sorted)); return $sorted; }
public function run() { if ($this->canRun()) { $content = $this->document->getContent(); $this->parseArguments(); $arguments = array_merge([$this->isClosing()], $this->arguments); $result = call_user_func_array($this->getCallable(), $arguments); $content = preg_replace('/' . preg_quote($this->raw, '/') . '/', $result, $content, 1); $this->document->setContent($content); } else { throw CodexException::because("Macro [{$this->cleaned}] cannot call because some properties havent been set. Prevent the Macro from running by using the canRun() method."); } }
/** * Shorthand helper method for getting projects, refs or documents * * <strong>Example:</strong> * <code> * <?php * $projects = codex()->get('*'); # Codex\Projects\Projects * $project = codex()->get('codex'); # Codex\Projects\Project * $refs = codex()->get('codex/*'); # Codex\Projects\Refs * $ref = codex()->get('codex/!'); # Codex\Projects\Ref (default ref) * $ref = codex()->get('codex/master'); # Codex\Projects\Ref * $ref = codex()->get('codex/1.0.0'); # Codex\Projects\Ref * $documents = codex()->get('codex::index); # Codex\Documents\Document (from default ref) * $documents = codex()->get('codex/master::*'); # Codex\Documents\Documents * $document = codex()->get('codex/master::!'); # Codex\Documents\Document (default document) * $document = codex()->get('codex/master::index'); # Codex\Documents\Document * $document = codex()->get('codex/master::develop/hooks'); # Codex\Documents\Document * </code> * * <strong>Syntax:</strong> * {project?}/{$ref?}::{documentPath?} * * <strong>Special modifiers:</strong> * * = The collection (projects, refs or documents) * ! = The default of the collection (project, def or document) * * <strong>Syntax examples:</strong> * codex/master::getting-started/installation * * @param $query * * @return \Codex\Documents\Document|\Codex\Documents\Documents|\Codex\Projects\Project|\Codex\Projects\Projects * @throws \Codex\Exception\CodexException * @example * <?php * $projects = codex()->get('*'); # Codex\Projects\Projects * $project = codex()->get('codex'); # Codex\Projects\Project * $refs = codex()->get('codex/*'); # Codex\Projects\Refs * $ref = codex()->get('codex/!'); # Codex\Projects\Ref (default ref) * $ref = codex()->get('codex/master'); # Codex\Projects\Ref * $ref = codex()->get('codex/1.0.0'); # Codex\Projects\Ref * $documents = codex()->get('codex::index); # Codex\Documents\Document (from default ref) * $documents = codex()->get('codex/master::*'); # Codex\Documents\Documents * $document = codex()->get('codex/master::!'); # Codex\Documents\Document (default document) * $document = codex()->get('codex::!'); # Codex\Documents\Document (from default ref, default document) * $document = codex()->get('codex/master::index'); # Codex\Documents\Document * $document = codex()->get('codex/master::develop/hooks'); # Codex\Documents\Document */ public function get($query) { // project/ref::path/to/document $segments = explode('::', $query); $psegments = explode('/', $segments[0]); $projectName = $psegments[0]; $projectRef = isset($psegments[1]) ? $psegments[1] : false; $documentPathName = isset($segments[1]) ? $segments[1] : false; # # Projects / Project if ($projectName === '*') { // get('*') return $this->projects; } elseif ($projectName === '!') { $projectName = config('codex.default_project'); } if (false === $this->projects->has($projectName)) { throw CodexException::projectNotFound($projectName); } $project = $this->projects->get($projectName); # # Refs / Ref if ($projectRef === false) { if ($documentPathName === false) { // get('codex') return $project; } $projectRef = '!'; // make it so that the default ref will be chosen when using get('codex::path/to/document') } if ($projectRef === '*') { // get('codex/*') return $project->refs; } if ($projectRef === '!') { $ref = $project->refs->getDefault(); } else { if (false === $project->refs->has($projectRef)) { throw CodexException::refNotFound($projectRef); } $ref = $project->refs->get($projectRef); } if ($documentPathName === false) { // get('codex/!') // get('codex/ref') return $ref; } # # Documents / Document if ($documentPathName === '*') { return $ref->documents; } elseif ($documentPathName === '!') { $documentPathName = $project->config('index'); } elseif (false === $ref->documents->has($documentPathName)) { throw CodexException::documentNotFound($documentPathName); } return $ref->documents->get($documentPathName); }