/** * Parse @mentions from a string of text * https://github.com/thephpleague/commonmark/blob/gh-pages/customization/inline-parsing.md#example * * @param ContextInterface $context * @param InlineParserContext $inlineContext * @return bool */ public function parse(ContextInterface $context, InlineParserContext $inlineContext) { $cursor = $inlineContext->getCursor(); $previousChar = $cursor->peek(-1); if ($previousChar !== null && $previousChar !== ' ') { return false; } $previousState = $cursor->saveState(); $cursor->advance(); $handle = $cursor->match('/^\\w+/'); if (empty($handle)) { $cursor->restoreState($previousState); return false; } $userCache = new UserCache(); $usernames = $userCache->usernames(); $found = in_array($handle, $usernames, true); if (!$found) { $cursor->restoreState($previousState); return false; } $users = $userCache->users(); $userKey = $this->searchArray($users, $handle); $user = $users[$userKey]; $url = $user['url']; $inlineContext->getInlines()->add(new Link($url, '@' . $handle)); return true; }
/** * Bootstrap the application services. * * @return void */ public function boot() { // User model $userCache = new UserCache(); User::created(function ($user) use($userCache) { $userCache->update($user); }); User::updated(function ($user) use($userCache) { $userCache->update($user); }); User::saved(function ($user) use($userCache) { $userCache->update($user); }); User::deleted(function ($user) use($userCache) { $userCache->delete($user); }); }