-
Notifications
You must be signed in to change notification settings - Fork 438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Debugger: resolve source location #844
Merged
chingor13
merged 15 commits into
googleapis:master
from
chingor13:debugger-resolve-location
Jan 19, 2018
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a71f2e3
Add test for SourceLocationResolver
chingor13 ebcb327
Add SourceLocationResolver
chingor13 cf822cc
Breakpoint validation attempts to resolve the source location
chingor13 2d03d26
Move MatchingFileIterator to its own file for phpcs
chingor13 24b23dc
Add snippet tests for new classes
chingor13 c993962
Install stackdriver_debugger extension on travis. PHPCS fix
chingor13 71d079e
Merge branch 'master' into debugger-resolve-location
chingor13 6bb29c3
only try to install stackdriver extension on php 7
chingor13 69f5e32
Merge branch 'master' into debugger-resolve-location
chingor13 e10b59e
Make resolveLocation public and add tests
chingor13 54a6cdd
Fix path separator for windows
chingor13 a4fe0fc
Add MatchingFileIterator unit test
chingor13 e01751f
Fix merge. SourceLocation validations should happen on the resolved s…
chingor13 fda5ee9
Fix phpcs
chingor13 ea57515
Add a comment about the MatchingFileIterator accept logic
chingor13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* Copyright 2018 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Google\Cloud\Debugger; | ||
|
||
/** | ||
* This iterator returns files that match the provided file in the provided | ||
* search path. | ||
* | ||
* Example: | ||
* ``` | ||
* $iterator = new MatchingFileIterator('.', 'src/Debugger/DebuggerClient.php'); | ||
* $matches = iterator_to_array($iterator); | ||
* ``` | ||
* | ||
* @access private | ||
*/ | ||
class MatchingFileIterator extends \FilterIterator | ||
{ | ||
/** | ||
* @var string The file pattern to search for. | ||
*/ | ||
private $file; | ||
|
||
/** | ||
* Create a new MatchingFileIterator. | ||
* | ||
* @param string $searchPath The root path to search in | ||
* @param string $file The file to search for | ||
*/ | ||
public function __construct($searchPath, $file) | ||
{ | ||
parent::__construct( | ||
new \RecursiveIteratorIterator( | ||
new \RecursiveDirectoryIterator( | ||
realpath($searchPath), | ||
\FilesystemIterator::SKIP_DOTS | ||
) | ||
) | ||
); | ||
$this->file = $file; | ||
} | ||
|
||
/** | ||
* FilterIterator callback to determine whether or not the value should be | ||
* accepted. | ||
* | ||
* @access private | ||
* @return boolean | ||
*/ | ||
public function accept() | ||
{ | ||
$candidate = $this->getInnerIterator()->current(); | ||
|
||
// Check that the candidate file (a full file path) ends in the pattern we are searching for. | ||
return strrpos($candidate, $this->file) === strlen($candidate) - strlen($this->file); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
/** | ||
* Copyright 2018 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Google\Cloud\Debugger; | ||
|
||
/** | ||
* This class handles searching for a source file in the application's source | ||
* tree. A debugger breakpoint may be requested for a source path that has | ||
* extra or missing folders. | ||
* | ||
* Example: | ||
* ``` | ||
* $location = new SourceLocation('src/Debugger/DebuggerClient.php', 1); | ||
* $resolver = new SourceLocationResolver(); | ||
* $resolvedLocation = $resolver->resolve($location); | ||
* ``` | ||
*/ | ||
class SourceLocationResolver | ||
{ | ||
/** | ||
* Resolve the full path of an existing file in the application's source. | ||
* If no matching source file is found, then return null. If found, the | ||
* resolved location will include the full, absolute path to the source | ||
* file. | ||
* | ||
* There are 3 cases for resolving a SourceLocation: | ||
* | ||
* Case 1: The exact path is found | ||
* | ||
* Example: | ||
* ``` | ||
* $location = new SourceLocation('src/Debugger/DebuggerClient.php', 1); | ||
* $resolver = new SourceLocationResolver(); | ||
* $resolvedLocation = $resolver->resolve($location); | ||
* ``` | ||
* | ||
* Case 2: There are extra folder(s) in the requested breakpoint path | ||
* | ||
* Example: | ||
* ``` | ||
* $location = new SourceLocation('extra/folder/src/Debugger/DebuggerClient.php', 1); | ||
* $resolver = new SourceLocationResolver(); | ||
* $resolvedLocation = $resolver->resolve($location); | ||
* ``` | ||
* | ||
* Case 3: There are fewer folders in the requested breakpoint path | ||
* | ||
* Example: | ||
* ``` | ||
* $location = new SourceLocation('Debugger/DebuggerClient.php', 1); | ||
* $resolver = new SourceLocationResolver(); | ||
* $resolvedLocation = $resolver->resolve($location); | ||
* | ||
* @param SourceLocation $location The location to resolve. | ||
* @return SourceLocation|null | ||
*/ | ||
public function resolve(SourceLocation $location) | ||
{ | ||
$basename = basename($location->path()); | ||
$prefixes = $this->searchPrefixes($location->path()); | ||
$includePaths = explode(PATH_SEPARATOR, get_include_path()); | ||
|
||
// Phase 1: search for an exact file match and try stripping off extra | ||
// folders | ||
foreach ($prefixes as $prefix) { | ||
foreach ($includePaths as $path) { | ||
$file = implode(DIRECTORY_SEPARATOR, [$path, $prefix, $basename]); | ||
if (file_exists($file)) { | ||
return new SourceLocation(realpath($file), $location->line()); | ||
} | ||
} | ||
} | ||
|
||
// Phase 2: recursively search folders for | ||
foreach ($includePaths as $includePath) { | ||
$iterator = new MatchingFileIterator( | ||
$includePath, | ||
$location->path() | ||
); | ||
foreach ($iterator as $file => $info) { | ||
return new SourceLocation(realpath($file), $location->line()); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Returns an array of relative paths for this file by recursively removing | ||
* each leading directory. | ||
* | ||
* @param string $path The source path | ||
* @return string[] | ||
*/ | ||
private function searchPrefixes($path) | ||
{ | ||
$dirname = dirname($path); | ||
$directoryParts = explode(DIRECTORY_SEPARATOR, $dirname); | ||
$directories = []; | ||
while ($directoryParts) { | ||
$directories[] = implode(DIRECTORY_SEPARATOR, $directoryParts); | ||
array_shift($directoryParts); | ||
} | ||
return $directories; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
/** | ||
* Copyright 2018 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace Google\Cloud\Tests\Snippets\Debugger; | ||
|
||
use Google\Cloud\Dev\Snippet\SnippetTestCase; | ||
use Google\Cloud\Debugger\MatchingFileIterator; | ||
|
||
/** | ||
* @group debugger | ||
*/ | ||
class MatchingFileIteratorTest extends SnippetTestCase | ||
{ | ||
public function testClass() | ||
{ | ||
$snippet = $this->snippetFromClass(MatchingFileIterator::class); | ||
$snippet->addUse(MatchingFileIterator::class); | ||
$matches = $snippet->invoke('matches')->returnVal(); | ||
$this->assertCount(1, $matches); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.