@@ -25,8 +25,6 @@ class Project
2525 const XPATH_PROJECT_NAME = "(//component[@name='ProjectView']/panes/pane[@id='ProjectPane']/subPane/PATH/PATH_ELEMENT/option/@value)[1] " ;
2626 const XPATH_PROJECT_NAME_ALT = "(//component[@name='ProjectView']/panes/pane[@id='ProjectPane']/subPane/expand/path/item[contains(@type, ':ProjectViewProjectNode')]/@name)[1] " ;
2727 const XPATH_PROJECT_NAME_AS = "((/project/component[@name='ChangeListManager']/ignored[contains(@path, '.iws')]/@path)[1]) " ;
28- // doesn't works: http://php.net/manual/en/simplexmlelement.xpath.php#93730
29- // const XPATH_PROJECT_NAME_AS = "substring-before(((/project/component[@name='ChangeListManager']/ignored[contains(@path, '.iws')]/@path)[1]), '.iws')";
3028
3129
3230 /**
@@ -48,7 +46,7 @@ class Project
4846 /**
4947 * @var bool
5048 */
51- private $ debug ;
49+ private $ debug = false ;
5250 /**
5351 * @var string
5452 */
@@ -66,7 +64,7 @@ class Project
6664
6765 /**
6866 * @param string $jetbrainsApp
69- * @throws \RuntimeException
67+ * @throws WorkflowException
7068 */
7169 public function __construct ($ jetbrainsApp )
7270 {
@@ -77,11 +75,13 @@ public function __construct($jetbrainsApp)
7775 $ this ->jetbrainsApp = $ jetbrainsApp ;
7876 $ this ->result = new Result ();
7977
80- $ this ->debug = isset ($ _SERVER ['jb_debug ' ]) ? (bool )$ _SERVER ['jb_debug ' ] : false ;
78+ if (isset ($ _SERVER ['jb_debug ' ])) {
79+ $ this ->debug = (bool )$ _SERVER ['jb_debug ' ];
80+ }
8181
8282 $ this ->cacheDir = $ _SERVER ['alfred_workflow_cache ' ];
8383 if (!mkdir ($ this ->cacheDir ) && !is_dir ($ this ->cacheDir )) {
84- throw new \ RuntimeException (sprintf ('Directory "%s" was not created ' , $ this ->cacheDir ));
84+ throw new WorkflowException (sprintf ('Directory "%s" was not created ' , $ this ->cacheDir ));
8585 }
8686
8787 if ($ this ->debug ) {
@@ -166,21 +166,7 @@ private function getProjectsData()
166166 return $ projectsData ;
167167 }
168168
169- if (is_readable ($ this ->jetbrainsAppConfigPath . self ::PATH_RECENT_PROJECT_DIRECTORIES )) {
170- $ this ->log (' Work with: ' . self ::PATH_RECENT_PROJECT_DIRECTORIES );
171- $ file = $ this ->jetbrainsAppConfigPath . self ::PATH_RECENT_PROJECT_DIRECTORIES ;
172- $ xpath = self ::XPATH_RECENT_PROJECT_DIRECTORIES ;
173- } elseif (is_readable ($ this ->jetbrainsAppConfigPath . self ::PATH_RECENT_PROJECTS )) {
174- $ this ->log (' Work with: ' . self ::PATH_RECENT_PROJECTS );
175- $ file = $ this ->jetbrainsAppConfigPath . self ::PATH_RECENT_PROJECTS ;
176- $ xpath = self ::XPATH_RECENT_PROJECTS ;
177- } elseif (is_readable ($ this ->jetbrainsAppConfigPath . self ::PATH_RECENT_SOLUTIONS )) {
178- $ this ->log (' Work with: ' . self ::PATH_RECENT_SOLUTIONS );
179- $ file = $ this ->jetbrainsAppConfigPath . self ::PATH_RECENT_SOLUTIONS ;
180- $ xpath = self ::XPATH_RECENT_SOLUTIONS ;
181- } else {
182- throw new \RuntimeException ("Can't find 'options' XML in ' {$ this ->jetbrainsAppConfigPath }' " , 100 );
183- }
169+ list ($ file , $ xpath ) = $ this ->getFileAndXpath ();
184170
185171 $ projectsData = [];
186172
@@ -225,6 +211,32 @@ private function getProjectsData()
225211 return $ projectsData ;
226212 }
227213
214+ /**
215+ * @return array
216+ * @throws WorkflowException
217+ */
218+ private function getFileAndXpath ()
219+ {
220+ $ message = ' Work with: %s ' ;
221+ if (is_readable ($ this ->jetbrainsAppConfigPath . self ::PATH_RECENT_PROJECT_DIRECTORIES )) {
222+ $ this ->log (sprintf ($ message , self ::PATH_RECENT_PROJECT_DIRECTORIES ));
223+ $ file = $ this ->jetbrainsAppConfigPath . self ::PATH_RECENT_PROJECT_DIRECTORIES ;
224+ $ xpath = self ::XPATH_RECENT_PROJECT_DIRECTORIES ;
225+ } elseif (is_readable ($ this ->jetbrainsAppConfigPath . self ::PATH_RECENT_PROJECTS )) {
226+ $ this ->log (sprintf ($ message , self ::PATH_RECENT_PROJECTS ));
227+ $ file = $ this ->jetbrainsAppConfigPath . self ::PATH_RECENT_PROJECTS ;
228+ $ xpath = self ::XPATH_RECENT_PROJECTS ;
229+ } elseif (is_readable ($ this ->jetbrainsAppConfigPath . self ::PATH_RECENT_SOLUTIONS )) {
230+ $ this ->log (sprintf ($ message , self ::PATH_RECENT_SOLUTIONS ));
231+ $ file = $ this ->jetbrainsAppConfigPath . self ::PATH_RECENT_SOLUTIONS ;
232+ $ xpath = self ::XPATH_RECENT_SOLUTIONS ;
233+ } else {
234+ throw new WorkflowException ("Can't find 'options' XML in ' {$ this ->jetbrainsAppConfigPath }' " , 100 );
235+ }
236+
237+ return [$ file , $ xpath ];
238+ }
239+
228240 /**
229241 * @param string $path
230242 * @return bool|string
@@ -259,7 +271,7 @@ private function getProjectName($path)
259271 /**
260272 *
261273 * @throws \InvalidArgumentException
262- * @throws \RuntimeException
274+ * @throws WorkflowException
263275 */
264276 private function checkJetbrainsApp ()
265277 {
@@ -274,34 +286,19 @@ private function checkJetbrainsApp()
274286 if ($ handle ) {
275287 while (($ row = fgets ($ handle )) !== false ) {
276288
277- foreach ($ paths as $ var => $ field ) {
278- if (strpos ($ row , "{$ var } = " ) === 0 ) {
279- $ path = str_replace ("{$ var } = u " , '' , $ row );
280- $ path = trim ($ path );
281- $ path = trim ($ path , "' " );
282- if (is_dir ($ path ) && is_readable ($ path )) {
283- $ this ->$ field = $ path ;
284-
285- $ this ->log ("{$ field }: {$ this ->$ field }" );
286-
287- break ;
288- }
289- }
290-
291- }
289+ $ this ->searchAppAndConfigPath ($ paths , $ row );
292290
293291 if ($ this ->jetbrainsAppPath && $ this ->jetbrainsAppConfigPath ) {
294292 $ this ->result ->addVariable ('bin ' , $ this ->jetbrainsApp );
295293
296294 break ;
297295 }
298-
299296 }
300297 if (!$ this ->jetbrainsAppPath ) {
301- throw new \ RuntimeException ("Can't find application path for ' {$ this ->jetbrainsApp }' " );
298+ throw new WorkflowException ("Can't find application path for ' {$ this ->jetbrainsApp }' " );
302299 }
303300 if (!$ this ->jetbrainsAppConfigPath ) {
304- throw new \ RuntimeException ("Can't find application configuration path for ' {$ this ->jetbrainsApp }' " );
301+ throw new WorkflowException ("Can't find application configuration path for ' {$ this ->jetbrainsApp }' " );
305302 }
306303 } else {
307304 throw new \InvalidArgumentException ("Can't find command line launcher for ' {$ this ->jetbrainsApp }' " );
@@ -316,6 +313,28 @@ private function checkJetbrainsApp()
316313 $ this ->cache = new Cache ($ cacheFile );
317314 }
318315
316+ /**
317+ * @param array $paths
318+ * @param string $row
319+ */
320+ private function searchAppAndConfigPath ($ paths , $ row )
321+ {
322+ foreach ($ paths as $ var => $ field ) {
323+ if (strpos ($ row , "{$ var } = " ) === 0 ) {
324+ $ path = str_replace ("{$ var } = u " , '' , $ row );
325+ $ path = trim ($ path );
326+ $ path = trim ($ path , "' " );
327+ if (is_dir ($ path ) && is_readable ($ path )) {
328+ $ this ->$ field = $ path ;
329+
330+ $ this ->log ("{$ field }: {$ this ->$ field }" );
331+
332+ break ;
333+ }
334+ }
335+ }
336+ }
337+
319338 /**
320339 * @param string $name
321340 * @param string $path
@@ -329,7 +348,7 @@ private function addProjectItem($name, $path)
329348 ->setSubtitle ($ path )
330349 ->setArg ($ path )
331350 ->setAutocomplete ($ name )
332- ->setIcon ($ this ->jetbrainsAppPath , ' fileicon ' )
351+ ->setIcon ($ this ->jetbrainsAppPath , Item:: ICON_TYPE_FILEICON )
333352 ->setText ($ path , $ path )
334353 ->setVariables ('name ' , $ name );
335354
@@ -348,7 +367,7 @@ private function addNoProjectMatchItem($query)
348367 ->setArg ('' )
349368 ->setAutocomplete ('' )
350369 ->setValid (false )
351- ->setIcon ($ this ->jetbrainsAppPath , ' fileicon ' );
370+ ->setIcon ($ this ->jetbrainsAppPath , Item:: ICON_TYPE_FILEICON );
352371
353372 $ this ->result ->addItem ($ item );
354373
@@ -409,7 +428,7 @@ private function addDebugItem()
409428 }
410429
411430 /**
412- * @param string|array|\stdClass $message
431+ * @param string|array|\stdClass|\Exception $message
413432 */
414433 private function log ($ message )
415434 {
@@ -427,3 +446,7 @@ private function log($message)
427446 }
428447
429448}
449+
450+ class WorkflowException extends \RuntimeException
451+ {
452+ }
0 commit comments