Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / guides / guidance / PhabricatorGuidanceEngine.php
blob2b12402ce70db89662e07e7dc6baf055657692d2
1 <?php
3 final class PhabricatorGuidanceEngine
4 extends Phobject {
6 private $viewer;
7 private $guidanceContext;
9 public function setGuidanceContext(
10 PhabricatorGuidanceContext $guidance_context) {
11 $this->guidanceContext = $guidance_context;
12 return $this;
15 public function getGuidanceContext() {
16 return $this->guidanceContext;
19 public function setViewer(PhabricatorUser $viewer) {
20 $this->viewer = $viewer;
21 return $this;
24 public function getViewer() {
25 return $this->viewer;
28 public function newInfoView() {
29 $extensions = PhabricatorGuidanceEngineExtension::getAllExtensions();
30 $context = $this->getGuidanceContext();
32 $keep = array();
33 foreach ($extensions as $key => $extension) {
34 if (!$extension->canGenerateGuidance($context)) {
35 continue;
37 $keep[$key] = id(clone $extension);
40 $guidance_map = array();
41 foreach ($keep as $extension) {
42 $guidance_list = $extension->generateGuidance($context);
43 foreach ($guidance_list as $guidance) {
44 $key = $guidance->getKey();
46 if (isset($guidance_map[$key])) {
47 throw new Exception(
48 pht(
49 'Two guidance extensions generated guidance with the same '.
50 'key ("%s"). Each piece of guidance must have a unique key.',
51 $key));
54 $guidance_map[$key] = $guidance;
58 foreach ($keep as $extension) {
59 $guidance_map = $extension->didGenerateGuidance($context, $guidance_map);
62 if (!$guidance_map) {
63 return null;
66 $guidance_map = msortv($guidance_map, 'getSortVector');
68 $severity = PhabricatorGuidanceMessage::SEVERITY_NOTICE;
69 $strength = null;
70 foreach ($guidance_map as $guidance) {
71 if ($strength !== null) {
72 if ($guidance->getSeverityStrength() <= $strength) {
73 continue;
77 $strength = $guidance->getSeverityStrength();
78 $severity = $guidance->getSeverity();
81 $severity_map = array(
82 PhabricatorGuidanceMessage::SEVERITY_NOTICE
83 => PHUIInfoView::SEVERITY_NOTICE,
84 PhabricatorGuidanceMessage::SEVERITY_WARNING
85 => PHUIInfoView::SEVERITY_WARNING,
88 $messages = mpull($guidance_map, 'getMessage', 'getKey');
90 return id(new PHUIInfoView())
91 ->setViewer($this->getViewer())
92 ->setSeverity(idx($severity_map, $severity, $severity))
93 ->setErrors($messages);