/** * Execute the command. */ public function execute() { $title = $this->ask('Article Title', null, array(), function ($answer) { if (empty($answer)) { throw new \InvalidArgumentException('The title cannot be empty'); } return $answer; }); $slug = $this->ask('Article Slug', StringUtils::urlFriendly($title)); $image = $this->ask('Article Cover Image Path'); $markdownFile = $this->get('blog.writer')->create($title, $slug, $image); $this->writeln('Created article <info>' . $title . '</info> in <comment>' . $markdownFile . '</comment>'); }
/** * 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 testUrlFriendly() { $strings = array('lorem ipsum dolor sit amet', 'zażółć żółtą gęśl', '<?php echo $stuff->whatever; ?>', '123123123', 123123123, '<html><head /></html>', 'I want to do something like that & whatever'); foreach ($strings as $string) { $this->assertEquals(1, preg_match('/([a-z0-9-_]+)/', StringUtils::urlFriendly($string))); } foreach ($strings as $string) { $this->assertEquals(1, preg_match('/([a-zA-Z0-9-_]+)/', StringUtils::urlFriendly($string, false))); } }