/** * When the found controller method wants to have the request injected, * this method will do it. * * @param ControllerWillRespond $event Event triggered before execution of controller. */ public function injectRequest(ControllerWillRespond $event) { $route = $this->router->getRoute($event->getControllerName()); // find the method's meta data $methods = $route->getMethods(); $i = ArrayUtils::search($methods, 'method', $event->getMethod()); if ($i === false) { return; } $method = $methods[$i]; $arguments = $event->getArguments(); foreach ($method['params'] as $i => $param) { if ($param['class'] && Debugger::isExtending($param['class'], Request::class, true) && !$arguments[$i] instanceof Request) { $arguments[$i] = $this->request; } } $event->setArguments($arguments); }
/** * Create an article. * * Returns path to the created markdown file. * * @param string $title Title of the article. * @param string $slug [optional] Slug of the article. If ommitted, it will be built based on the title. * @param string $image [optional] Path to cover image, relative to the web root folder. * * @return string */ public function create($title, $slug = null, $image = null) { $slug = $slug ? StringUtils::urlFriendly($slug) : StringUtils::urlFriendly($title); $articles = $this->reader->load(); // check if maybe there already is such article if (ArrayUtils::search($articles, 'slug', $slug) !== false) { throw new NotUniqueException('There already is a blog article with a slug "' . $slug . '".'); } // create a markdown post $markdown = $title . NL . '==========' . NL . date('Y-m-d') . NL . ($image ? '![cover](' . $image . ')' . NL : '') . NL; $markdownFile = $this->articlesDir . $slug . '.md'; // create the articles directory if doesn't exist $this->filesystem->dumpFile($markdownFile, $markdown); // add to articles array array_unshift($articles, array('slug' => $slug, 'title' => $title)); $this->writer->write($this->dataFile, $articles); return $markdownFile; }
public function testSearch() { $this->assertFalse(ArrayUtils::search($this->_getArrayPreset('empty_array'), 'lorem', 'ipsum')); $this->assertFalse(ArrayUtils::search($this->_getArrayPreset('2D_collection_5'), 'id', 999)); $this->assertFalse(ArrayUtils::search($this->_getArrayPreset('2D_collection_5'), 'id', '1')); // make sure its strict comparison $this->assertEquals(0, ArrayUtils::search($this->_getArrayPreset('2D_collection_5'), 'id', 1)); $this->assertEquals('lorem', ArrayUtils::search($this->_getArrayPreset('2D_collection_5_named'), 'id', 1)); }
/** * Gets an article based on slug. * * @param string $slug Slug of the article to get. * * @return Article */ public function getArticle($slug) { $data = $this->load(); $i = ArrayUtils::search($data, 'slug', $slug); if ($i === false) { throw new NotFoundException('Could not find blog article with slug "' . $slug . '".'); } return $this->readArticle($data[$i]['slug']); }