Skip to content

Commit

Permalink
dev/core#472 Allow for APIv3 to find examples in Extenion folders as …
Browse files Browse the repository at this point in the history
…well as core

Improve inclusion scanning by using get_include_path as suggested by Coleamn and alpha sort the list of entities

Deal with windows directory separators and more helpfully handle the trailing and non trailing slash directories
  • Loading branch information
seamuslee001 committed Oct 29, 2018
1 parent 9b574de commit 9b30dfb
Showing 1 changed file with 54 additions and 11 deletions.
65 changes: 54 additions & 11 deletions CRM/Admin/Page/APIExplorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@
*/
class CRM_Admin_Page_APIExplorer extends CRM_Core_Page {

/**
* Return unique paths for checking for examples.
* @return array
*/
private static function uniquePaths() {
// Ensure that paths with trailing slashes are properly dealt with
$paths = explode(PATH_SEPARATOR, get_include_path());
foreach ($paths as $id => $rawPath) {
$pathParts = explode(DIRECTORY_SEPARATOR, $rawPath);
foreach ($pathParts as $partId => $part) {
if (empty($part)) {
unset($pathParts[$partId]);
}
}
$newRawPath = implode(DIRECTORY_SEPARATOR, $pathParts);
if ($newRawPath != $rawPath) {
$paths[$id] = DIRECTORY_SEPARATOR . $newRawPath;
}
}
$paths = array_unique($paths);
return $paths;
}


/**
* Run page.
*
Expand All @@ -51,12 +75,20 @@ public function run() {
$this->assign('operators', CRM_Core_DAO::acceptedSQLOperators());

// List example directories
// use get_include_path to ensure that extensions are captured.
$examples = array();
foreach (scandir(\Civi::paths()->getPath('[civicrm.root]/api/v3/examples')) as $item) {
if ($item && strpos($item, '.') === FALSE) {
$examples[] = $item;
$paths = self::uniquePaths();
foreach ($paths as $path) {
$dir = \CRM_Utils_File::addTrailingSlash($path) . 'api' . DIRECTORY_SEPARATOR . 'v3' . DIRECTORY_SEPARATOR . 'examples';
if (is_dir($dir)) {
foreach (scandir($dir) as $item) {
if ($item && strpos($item, '.') === FALSE && array_search($item, $examples) === FALSE) {
$examples[] = $item;
}
}
}
}
sort($examples);
$this->assign('examples', $examples);

return parent::run();
Expand All @@ -78,20 +110,31 @@ public function userContext() {
public static function getExampleFile() {
if (!empty($_GET['entity']) && strpos($_GET['entity'], '.') === FALSE) {
$examples = array();
foreach (scandir(\Civi::paths()->getPath("[civicrm.root]/api/v3/examples/{$_GET['entity']}")) as $item) {
$item = str_replace('.php', '', $item);
if ($item && strpos($item, '.') === FALSE) {
$examples[] = array('key' => $item, 'value' => $item);
$paths = self::uniquePaths();
foreach ($paths as $path) {
$dir = \CRM_Utils_File::addTrailingSlash($path) . 'api' . DIRECTORY_SEPARATOR . 'v3' . DIRECTORY_SEPARATOR . 'examples' . DIRECTORY_SEPARATOR . $_GET['entity'];
if (is_dir($dir)) {
foreach (scandir($dir) as $item) {
$item = str_replace('.php', '', $item);
if ($item && strpos($item, '.') === FALSE) {
$examples[] = array('key' => $item, 'value' => $item);
}
}
}
}
CRM_Utils_JSON::output($examples);
}
if (!empty($_GET['file']) && strpos($_GET['file'], '.') === FALSE) {
$fileName = \Civi::paths()->getPath("[civicrm.root]/api/v3/examples/{$_GET['file']}.php");
if (file_exists($fileName)) {
echo file_get_contents($fileName);
$paths = self::uniquePaths();
$fileFound = FALSE;
foreach ($paths as $path) {
$fileName = $path . '/api/v3/examples/' . $_GET['file'] . '.php';
if (!$fileFound && file_exists($fileName)) {
$fileFound = TRUE;
echo file_get_contents($fileName);
}
}
else {
if (!$fileFound) {
echo "Not found.";
}
CRM_Utils_System::civiExit();
Expand Down

0 comments on commit 9b30dfb

Please sign in to comment.