{"action":"created","comment":{"url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/pulls/comments/38411533","id":38411533,"diff_hunk":"@@ -50,57 +50,103 @@ public function __construct(\n     public function onKernelException(GetResponseForExceptionEvent $event)\n     {\n         if ($event->getException() instanceof HttpExceptionInterface && '404' == $event->getException()->getStatusCode()) {\n-            $currentSite = $this->siteRepository->findByAliasDomain($this->request->getHost());\n-\n-            if ($currentSite) {\n-                $language = $this->getLanguage($currentSite);\n-                $siteId = $currentSite->getSiteId();\n-                $nodeId = ReadNodeInterface::ERROR_404_NODE_ID;\n-                $node = $this->nodeRepository->findOnePublishedByNodeIdAndLanguageAndSiteIdInLastVersion($nodeId, $language, $siteId);\n-\n-                if ($node) {\n-                    $html = $this->templating->render(\n-                        'OpenOrchestraFrontBundle:Node:show.html.twig',\n-                        array(\n-                            'node' => $node,\n-                            'parameters' => array('siteId' => $node->getSiteId(), '_locale' => $node->getLanguage())\n-                        )\n-                    );\n-                    $event->setResponse(new Response($html, 404));\n-                }\n+\n+            $siteInfo = $this->getCurrentSiteInfo(\n+                trim($this->request->getHost(), '/'),\n+                trim($this->request->getPathInfo(), '/')\n+            );\n+\n+            if ($html = $this->getCustom404Html($siteInfo['site'], $siteInfo['language'])) {\n+                $event->setResponse(new Response($html, 404));\n             }\n         }\n     }\n \n     /**\n-     * Get the language to display the 404 page for $site\n-     * If a language prefix maps the url, get the corresponding language\n-     * Else get the language of the main alias\n+     * Try to find and set the current site and language\n      * \n-     * @param ReadSiteInterface $site\n+     * @param string $host\n+     * @param string $path\n      * \n-     * @return string\n+     * @return array\n      */\n-    protected function getLanguage(ReadSiteInterface $site)\n+    protected function getCurrentSiteInfo($host, $path)\n     {\n-        $targetAlias = null;\n-        $path = trim($this->request->getPathInfo(), '/');\n-        $host = $this->request->getHost();\n+        $path = $this->formatPath($path);\n+        $possibleSite = null;\n+        $possibleAlias = null;\n+        $matchingLength = -1;\n+\n+        $matchingSites = $this->siteRepository->findByAliasDomain($host);\n \n-        if ('' != $path) {\n+        /** @var ReadSiteInterface $site */\n+        foreach ($matchingSites as $site) {\n             foreach ($site->getAliases() as $alias) {\n-                if ($host == $alias->getDomain() && strpos($path . '/', $alias->getPrefix() . '/') === 0) {\n-                    $targetAlias = $alias;\n-                    break;\n+                $aliasPrefix = $this->formatPath($alias->getPrefix());\n+                if ($host == $alias->getDomain() && strpos($path, $aliasPrefix) === 0) {\n+                    $splitLength = count(explode('/', $aliasPrefix));\n+                    if ($splitLength > $matchingLength) {\n+                        $possibleAlias = $alias;\n+                        $possibleSite = $site;\n+                        $matchingLength = $splitLength;\n+                    }\n                 }\n             }\n         }\n \n-        if (!$targetAlias) {\n-            $targetAlias = $site->getMainAlias();\n+        return array(\n+            'site' => $possibleSite,\n+            'language' => $possibleAlias->getLanguage()\n+        );\n+    }\n+\n+    /**\n+     * Format a path to be compared with another formatted path\n+     * Ouptut a path formatted like this : /lvl1/lvl2/lvl3/ (/ if no path)\n+     * \n+     * @param string $path\n+     * \n+     * @return string\n+     */\n+    protected function formatPath($path)\n+    {\n+        $path = trim($path, '/') . '/';\n+        if (strlen($path) > 1) {\n+            $path = '/' . $path;\n+        }\n+\n+        return $path;\n+    }\n+\n+    /**\n+     * Get the 404 custom page for the current site / language if it has been contributed\n+     * \n+     * @param ReadSiteInterface $site\n+     * @param string            $language\n+     * \n+     * @return string | null\n+     */\n+    protected function getCustom404Html(ReadSiteInterface $site, $language)\n+    {\n+        if (!$site || !$language) {\n+            return null;\n+        }\n+\n+        $siteId = $site->getSiteId();\n+        $nodeId = ReadNodeInterface::ERROR_404_NODE_ID;\n+        $node = $this->nodeRepository->findOnePublishedByNodeIdAndLanguageAndSiteIdInLastVersion($nodeId, $language, $siteId);\n+\n+        if ($node) {\n+            return $this->templating->render(","path":"FrontBundle/EventSubscriber/KernelExceptionSubscriber.php","position":124,"original_position":124,"commit_id":"53f62f372a71e86a1d7114ac5ce7ad6529e357d1","original_commit_id":"53f62f372a71e86a1d7114ac5ce7ad6529e357d1","user":{"login":"itkg-alavieille","id":11750768,"avatar_url":"https://avatars.githubusercontent.com/u/11750768?v=3","gravatar_id":"","url":"https://api.github.com/users/itkg-alavieille","html_url":"https://github.com/itkg-alavieille","followers_url":"https://api.github.com/users/itkg-alavieille/followers","following_url":"https://api.github.com/users/itkg-alavieille/following{/other_user}","gists_url":"https://api.github.com/users/itkg-alavieille/gists{/gist_id}","starred_url":"https://api.github.com/users/itkg-alavieille/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/itkg-alavieille/subscriptions","organizations_url":"https://api.github.com/users/itkg-alavieille/orgs","repos_url":"https://api.github.com/users/itkg-alavieille/repos","events_url":"https://api.github.com/users/itkg-alavieille/events{/privacy}","received_events_url":"https://api.github.com/users/itkg-alavieille/received_events","type":"User","site_admin":false},"body":"add an empty line","created_at":"2015-09-01T12:13:47Z","updated_at":"2015-09-01T12:13:47Z","html_url":"https://github.com/open-orchestra/open-orchestra-front-bundle/pull/122#discussion_r38411533","pull_request_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/pulls/122","_links":{"self":{"href":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/pulls/comments/38411533"},"html":{"href":"https://github.com/open-orchestra/open-orchestra-front-bundle/pull/122#discussion_r38411533"},"pull_request":{"href":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/pulls/122"}}},"pull_request":{"url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/pulls/122","id":43637567,"html_url":"https://github.com/open-orchestra/open-orchestra-front-bundle/pull/122","diff_url":"https://github.com/open-orchestra/open-orchestra-front-bundle/pull/122.diff","patch_url":"https://github.com/open-orchestra/open-orchestra-front-bundle/pull/122.patch","issue_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/issues/122","number":122,"state":"open","locked":false,"title":"Update KernelExceptionSubscriber in case several sites share the same domain","user":{"login":"itkg-ngilain","id":6725138,"avatar_url":"https://avatars.githubusercontent.com/u/6725138?v=3","gravatar_id":"","url":"https://api.github.com/users/itkg-ngilain","html_url":"https://github.com/itkg-ngilain","followers_url":"https://api.github.com/users/itkg-ngilain/followers","following_url":"https://api.github.com/users/itkg-ngilain/following{/other_user}","gists_url":"https://api.github.com/users/itkg-ngilain/gists{/gist_id}","starred_url":"https://api.github.com/users/itkg-ngilain/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/itkg-ngilain/subscriptions","organizations_url":"https://api.github.com/users/itkg-ngilain/orgs","repos_url":"https://api.github.com/users/itkg-ngilain/repos","events_url":"https://api.github.com/users/itkg-ngilain/events{/privacy}","received_events_url":"https://api.github.com/users/itkg-ngilain/received_events","type":"User","site_admin":false},"body":"https://github.com/open-orchestra/open-orchestra-front-bundle/pull/122\r\nhttps://github.com/open-orchestra/open-orchestra-model-bundle/pull/415","created_at":"2015-08-28T15:04:30Z","updated_at":"2015-09-01T12:13:47Z","closed_at":null,"merged_at":null,"merge_commit_sha":"762ec05185a039611255ed9cbd64414b4ebd5251","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/pulls/122/commits","review_comments_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/pulls/122/comments","review_comment_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/pulls/comments{/number}","comments_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/issues/122/comments","statuses_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/statuses/53f62f372a71e86a1d7114ac5ce7ad6529e357d1","head":{"label":"open-orchestra:update_404_subscriber","ref":"update_404_subscriber","sha":"53f62f372a71e86a1d7114ac5ce7ad6529e357d1","user":{"login":"open-orchestra","id":11756116,"avatar_url":"https://avatars.githubusercontent.com/u/11756116?v=3","gravatar_id":"","url":"https://api.github.com/users/open-orchestra","html_url":"https://github.com/open-orchestra","followers_url":"https://api.github.com/users/open-orchestra/followers","following_url":"https://api.github.com/users/open-orchestra/following{/other_user}","gists_url":"https://api.github.com/users/open-orchestra/gists{/gist_id}","starred_url":"https://api.github.com/users/open-orchestra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/open-orchestra/subscriptions","organizations_url":"https://api.github.com/users/open-orchestra/orgs","repos_url":"https://api.github.com/users/open-orchestra/repos","events_url":"https://api.github.com/users/open-orchestra/events{/privacy}","received_events_url":"https://api.github.com/users/open-orchestra/received_events","type":"Organization","site_admin":false},"repo":{"id":22715331,"name":"open-orchestra-front-bundle","full_name":"open-orchestra/open-orchestra-front-bundle","owner":{"login":"open-orchestra","id":11756116,"avatar_url":"https://avatars.githubusercontent.com/u/11756116?v=3","gravatar_id":"","url":"https://api.github.com/users/open-orchestra","html_url":"https://github.com/open-orchestra","followers_url":"https://api.github.com/users/open-orchestra/followers","following_url":"https://api.github.com/users/open-orchestra/following{/other_user}","gists_url":"https://api.github.com/users/open-orchestra/gists{/gist_id}","starred_url":"https://api.github.com/users/open-orchestra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/open-orchestra/subscriptions","organizations_url":"https://api.github.com/users/open-orchestra/orgs","repos_url":"https://api.github.com/users/open-orchestra/repos","events_url":"https://api.github.com/users/open-orchestra/events{/privacy}","received_events_url":"https://api.github.com/users/open-orchestra/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/open-orchestra/open-orchestra-front-bundle","description":"","fork":false,"url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle","forks_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/forks","keys_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/keys{/key_id}","collaborators_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/teams","hooks_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/hooks","issue_events_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/issues/events{/number}","events_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/events","assignees_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/assignees{/user}","branches_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/branches{/branch}","tags_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/tags","blobs_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/git/refs{/sha}","trees_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/git/trees{/sha}","statuses_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/statuses/{sha}","languages_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/languages","stargazers_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/stargazers","contributors_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/contributors","subscribers_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/subscribers","subscription_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/subscription","commits_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/commits{/sha}","git_commits_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/git/commits{/sha}","comments_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/comments{/number}","issue_comment_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/issues/comments{/number}","contents_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/contents/{+path}","compare_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/compare/{base}...{head}","merges_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/merges","archive_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/downloads","issues_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/issues{/number}","pulls_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/pulls{/number}","milestones_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/milestones{/number}","notifications_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/labels{/name}","releases_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/releases{/id}","created_at":"2014-08-07T09:14:26Z","updated_at":"2015-05-07T15:14:48Z","pushed_at":"2015-08-31T09:25:18Z","git_url":"git://github.com/open-orchestra/open-orchestra-front-bundle.git","ssh_url":"git@github.com:open-orchestra/open-orchestra-front-bundle.git","clone_url":"https://github.com/open-orchestra/open-orchestra-front-bundle.git","svn_url":"https://github.com/open-orchestra/open-orchestra-front-bundle","homepage":null,"size":9459,"stargazers_count":2,"watchers_count":2,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":2,"forks":3,"open_issues":2,"watchers":2,"default_branch":"master"}},"base":{"label":"open-orchestra:master","ref":"master","sha":"42e1abe232dd164d29ae0d2b7b2b1e89f6939746","user":{"login":"open-orchestra","id":11756116,"avatar_url":"https://avatars.githubusercontent.com/u/11756116?v=3","gravatar_id":"","url":"https://api.github.com/users/open-orchestra","html_url":"https://github.com/open-orchestra","followers_url":"https://api.github.com/users/open-orchestra/followers","following_url":"https://api.github.com/users/open-orchestra/following{/other_user}","gists_url":"https://api.github.com/users/open-orchestra/gists{/gist_id}","starred_url":"https://api.github.com/users/open-orchestra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/open-orchestra/subscriptions","organizations_url":"https://api.github.com/users/open-orchestra/orgs","repos_url":"https://api.github.com/users/open-orchestra/repos","events_url":"https://api.github.com/users/open-orchestra/events{/privacy}","received_events_url":"https://api.github.com/users/open-orchestra/received_events","type":"Organization","site_admin":false},"repo":{"id":22715331,"name":"open-orchestra-front-bundle","full_name":"open-orchestra/open-orchestra-front-bundle","owner":{"login":"open-orchestra","id":11756116,"avatar_url":"https://avatars.githubusercontent.com/u/11756116?v=3","gravatar_id":"","url":"https://api.github.com/users/open-orchestra","html_url":"https://github.com/open-orchestra","followers_url":"https://api.github.com/users/open-orchestra/followers","following_url":"https://api.github.com/users/open-orchestra/following{/other_user}","gists_url":"https://api.github.com/users/open-orchestra/gists{/gist_id}","starred_url":"https://api.github.com/users/open-orchestra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/open-orchestra/subscriptions","organizations_url":"https://api.github.com/users/open-orchestra/orgs","repos_url":"https://api.github.com/users/open-orchestra/repos","events_url":"https://api.github.com/users/open-orchestra/events{/privacy}","received_events_url":"https://api.github.com/users/open-orchestra/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/open-orchestra/open-orchestra-front-bundle","description":"","fork":false,"url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle","forks_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/forks","keys_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/keys{/key_id}","collaborators_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/teams","hooks_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/hooks","issue_events_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/issues/events{/number}","events_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/events","assignees_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/assignees{/user}","branches_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/branches{/branch}","tags_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/tags","blobs_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/git/refs{/sha}","trees_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/git/trees{/sha}","statuses_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/statuses/{sha}","languages_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/languages","stargazers_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/stargazers","contributors_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/contributors","subscribers_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/subscribers","subscription_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/subscription","commits_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/commits{/sha}","git_commits_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/git/commits{/sha}","comments_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/comments{/number}","issue_comment_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/issues/comments{/number}","contents_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/contents/{+path}","compare_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/compare/{base}...{head}","merges_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/merges","archive_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/downloads","issues_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/issues{/number}","pulls_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/pulls{/number}","milestones_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/milestones{/number}","notifications_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/labels{/name}","releases_url":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/releases{/id}","created_at":"2014-08-07T09:14:26Z","updated_at":"2015-05-07T15:14:48Z","pushed_at":"2015-08-31T09:25:18Z","git_url":"git://github.com/open-orchestra/open-orchestra-front-bundle.git","ssh_url":"git@github.com:open-orchestra/open-orchestra-front-bundle.git","clone_url":"https://github.com/open-orchestra/open-orchestra-front-bundle.git","svn_url":"https://github.com/open-orchestra/open-orchestra-front-bundle","homepage":null,"size":9459,"stargazers_count":2,"watchers_count":2,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":2,"forks":3,"open_issues":2,"watchers":2,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/pulls/122"},"html":{"href":"https://github.com/open-orchestra/open-orchestra-front-bundle/pull/122"},"issue":{"href":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/issues/122"},"comments":{"href":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/issues/122/comments"},"review_comments":{"href":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/pulls/122/comments"},"review_comment":{"href":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/pulls/122/commits"},"statuses":{"href":"https://api.github.com/repos/open-orchestra/open-orchestra-front-bundle/statuses/53f62f372a71e86a1d7114ac5ce7ad6529e357d1"}}}}