Skip to content

Commit

Permalink
Merge pull request #13 from dextermb/fix/directory-seperator
Browse files Browse the repository at this point in the history
Use directory separator
  • Loading branch information
dextermb authored Jul 31, 2019
2 parents 07c03d7 + 781dafd commit 42722a4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
28 changes: 24 additions & 4 deletions src/Apps/PrivateXeroApp.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace LangleyFoxall\XeroLaravel\Apps;

use BadMethodCallException;
Expand All @@ -12,6 +13,7 @@

/**
* Class PrivateXeroApp
*
* @package LangleyFoxall\XeroLaravel
*/
class PrivateXeroApp extends PrivateApplication
Expand Down Expand Up @@ -48,6 +50,7 @@ public function __construct($config)
public function getAvailableRelationships()
{
$relationships = array_keys($this->relationshipToModelMap);

sort($relationships);

return collect($relationships);
Expand All @@ -63,18 +66,35 @@ public function getAvailableRelationships()
*/
public function populateRelationshipToModelMap($modelSubdirectory, $prefix)
{
$directory = Utils::getProjectRootDirectory();
$vendor = Utils::getVendorDirectory();

$dependencyDirectory = Utils::normalizePath(
$vendor.'/calcinai/xero-php/src/'
);

$modelsDirectory = $directory.'/vendor/calcinai/xero-php/src/XeroPHP/Models/'.$modelSubdirectory;
$modelsDirectory = Utils::normalizePath(
$dependencyDirectory.'/XeroPHP/Models/'.$modelSubdirectory
);

$di = new RecursiveDirectoryIterator($modelsDirectory);
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
if ($file->isDir() || !Str::endsWith($filename, '.php')) {
continue;
}

$relationship = Str::camel($prefix.Str::plural(str_replace([$modelsDirectory, '.php', '/'], ['', '', ''], $filename)));
$model = str_replace([$directory.'/vendor/calcinai/xero-php/src/', '/', '.php'], ['', '\\', ''], $filename);
$relationship = Str::camel(
$prefix.Str::plural(
str_replace(
[$modelsDirectory, '.php', DIRECTORY_SEPARATOR], '',
$filename
)
)
);

$model = str_replace(
[$dependencyDirectory, DIRECTORY_SEPARATOR, '.php'], ['', '\\'],
$filename
);

$this->relationshipToModelMap[$relationship] = $model;
}
Expand Down
29 changes: 25 additions & 4 deletions src/Utils.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace LangleyFoxall\XeroLaravel;

use Exception;
Expand All @@ -19,18 +20,38 @@ public static function getProjectRootDirectory()

do {
$directory = dirname($directory);
$composer = $directory.'/composer.json';
$vendor = $directory.'/vendor/';
$composer = self::normalizePath($directory.'/composer.json');
$vendor = self::normalizePath($directory.'/vendor/');

if (file_exists($composer) && file_exists($vendor)) {
if (file_exists($composer) && is_dir($vendor)) {
$root = $directory;
}
} while (is_null($root) && $directory != '/');
} while (is_null($root) && $directory != DIRECTORY_SEPARATOR);

if (!is_null($root)) {
return $root;
}

throw new Exception('Unable to determine project root directory.');
}

/**
* @return string
* @throws Exception
*/
public static function getVendorDirectory()
{
return self::normalizePath(
self::getProjectRootDirectory().'/vendor'
);
}

/**
* @param string $path
* @return string
*/
public static function normalizePath(string $path)
{
return str_replace('/', DIRECTORY_SEPARATOR, $path);
}
}

0 comments on commit 42722a4

Please sign in to comment.