1 <?php
 2 
 3 /**
 4  * @package redaxo\core
 5  */
 6 class rex_editor
 7 {
 8     use rex_factory_trait;
 9 
10     // see https://github.com/filp/whoops/blob/master/docs/Open%20Files%20In%20An%20Editor.md
11     // keep this list in sync with the array in getSupportedEditors()
12     private $editors = [
13         'atom' => 'atom://core/open/file?filename=%f&line=%l',
14         'emacs' => 'emacs://open?url=file://%f&line=%l',
15         'idea' => 'idea://open?file=%f&line=%l',
16         'macvim' => 'mvim://open/?url=file://%f&line=%l',
17         'phpstorm' => 'phpstorm://open?file=%f&line=%l',
18         'sublime' => 'subl://open?url=file://%f&line=%l',
19         'textmate' => 'txmt://open?url=file://%f&line=%l',
20         'vscode' => 'vscode://file/%f:%l',
21     ];
22 
23     // we expect instantiation via factory()
24     private function __construct()
25     {
26     }
27 
28     /**
29      * Creates a rex_editor instance.
30      *
31      * @return static Returns a rex_editor instance
32      */
33     public static function factory()
34     {
35         $class = static::getFactoryClass();
36         return new $class();
37     }
38 
39     public function getUrl($filePath, $line)
40     {
41         $editor = rex::getProperty('editor');
42 
43         $editorUrl = null;
44 
45         if (false !== strpos($filePath, '://')) {
46             // don't provide editor urls for paths containing "://", like "rex://..."
47             // but they can be converted into an url by the extension point below
48         } elseif (isset($this->editors[$editor]) || 'xdebug' === $editor) {
49             if ('xdebug' === $editor) {
50                 // if xdebug is not enabled, use `get_cfg_var` to get the value directly from php.ini
51                 $editorUrl = ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
52             } else {
53                 $editorUrl = $this->editors[$editor];
54             }
55 
56             $editorUrl = str_replace('%l', $line, $editorUrl);
57             $editorUrl = str_replace('%f', $filePath, $editorUrl);
58         }
59 
60         $editorUrl = rex_extension::registerPoint(new rex_extension_point('EDITOR_URL', $editorUrl, [
61             'file' => $filePath,
62             'line' => $line,
63         ]));
64 
65         return $editorUrl;
66     }
67 
68     public function getSupportedEditors()
69     {
70         return [
71             'atom' => 'Atom',
72             'emacs' => 'Emacs',
73             'idea' => 'IDEA',
74             'macvim' => 'MacVim',
75             'phpstorm' => 'PhpStorm',
76             'sublime' => 'Sublime Text',
77             'textmate' => 'Textmate',
78             'vscode' => 'Visual Studio Code',
79             'xdebug' => 'Xdebug via xdebug.file_link_format (php.ini)',
80         ];
81     }
82 }
83