Skip to content

Commit

Permalink
added IncludePath class
Browse files Browse the repository at this point in the history
  • Loading branch information
robfrawley committed Jun 5, 2016
1 parent 18aa3e3 commit e6239b1
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"issues" : "https://src.run/vermicious-environment-library/issues"
},
"keywords" : [
"vermicious-environment-library", "augustus", "reflection", "library", "rmf", "src-run"
"vermicious-environment-library", "augustus", "environment", "library", "rmf", "src-run"
],
"authors" : [
{
Expand Down
83 changes: 83 additions & 0 deletions lib/IncludePath.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/*
* This file is part of the `src-run/vermicious-environment-library` project.
*
* (c) Rob Frawley 2nd <rmf@src.run>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace SR\Environment;

/**
* Class IncludePath.
*/
final class IncludePath
{
/**
* @return string
*/
static public function restore()
{
restore_include_path();

return static::get();
}

/**
* @return string
*/
static public function get()
{
return (string) get_include_path();
}

/**
* @param string $includePath
* @param bool $prepend
*
* @return bool|string
*/
static public function add($includePath, $prepend = true)
{
if (!file_exists($includePath)) {
return false;
}

$existing = static::get();

if ($prepend) {
return set_include_path(
($includePath . PATH_SEPARATOR) .
(substr($existing, 0, 1) === PATH_SEPARATOR ? substr($existing, 1) : $existing));
}

return set_include_path(
(substr($existing, -1, 1) === PATH_SEPARATOR ? substr($existing, 0, strlen($existing) - 1) : $existing) .
(PATH_SEPARATOR . $includePath));
}

/**
* @param string $includePath
*
* @return bool|string
*/
static public function prependAdd($includePath)
{
return static::add($includePath, true);
}

/**
* @param string $includePath
*
* @return bool|string
*/
static public function appendAdd($includePath)
{
return static::add($includePath, false);
}
}

/* EOF */
71 changes: 71 additions & 0 deletions tests/IncludePathTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/*
* This file is part of the `src-run/vermicious-environment-library` project.
*
* (c) Rob Frawley 2nd <rmf@src.run>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace SR\Environment\Tests;

use SR\Environment\IncludePath;

/**
* Class IncludePath.
*/
class IncludePathTest extends \PHPUnit_Framework_TestCase
{
public function testPrependAdd()
{
IncludePath::restore();

$default = IncludePath::get();
IncludePath::prependAdd(__DIR__);
$expected = __DIR__ . PATH_SEPARATOR . $default;

$this->assertSame($expected, IncludePath::get());
}

public function testAppendAdd()
{
IncludePath::restore();

$default = IncludePath::get();
IncludePath::appendAdd(__DIR__);
$expected = $default . __DIR__;

$this->assertSame($expected, IncludePath::get());
}

public function testAddInvalid()
{
IncludePath::restore();

$default = IncludePath::get();
IncludePath::appendAdd(__DIR__ . '/does/not/exist');
$expected = $default;

$this->assertSame($expected, IncludePath::get());
}

public function testRestore()
{
IncludePath::restore();

$default = IncludePath::get();
IncludePath::appendAdd(__DIR__);
$expected = $default . __DIR__;

$this->assertSame($expected, IncludePath::get());

IncludePath::restore();
$expected = '.:';

$this->assertSame($expected, IncludePath::get());
}
}

/* EOF */
1 change: 0 additions & 1 deletion tests/RequesterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public function testIsLocal()

$this->assertRegExp('{Is Local Requester: true}', $response->text());
}

}

/* EOF */

0 comments on commit e6239b1

Please sign in to comment.