Skip to content

Commit

Permalink
[mms] Add Horde_Autoloader_ClassPathMapper_PrefixString mapper implem…
Browse files Browse the repository at this point in the history
…entation.
  • Loading branch information
slusarz committed Jan 30, 2014
1 parent ab9bd3a commit fd027a0
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 9 deletions.
@@ -0,0 +1,71 @@
<?php
/**
* Copyright 2014 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 2014 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Autoloader
*/

/**
* Provides a classmapper that implements prefix matching using a simple
* string search within a base application directory.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2014 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Autoloader
*/
class Horde_Autoloader_ClassPathMapper_PrefixString
implements Horde_Autoloader_ClassPathMapper
{
/**
* Include path.
*
* @var string
*/
private $_includePath;

/**
* Prefix to search for.
*
* @var string
*/
private $_prefix;

/**
* Constructor
*
* @param string $prefix Prefix.
* @param string $includePath Include path.
*/
public function __construct($prefix, $includePath)
{
$this->_includePath = $includePath;
$this->_prefix = $prefix;
}

/**
*/
public function mapToPath($className)
{
if (stripos($className, $this->_prefix) === 0) {
$len = strlen($this->_prefix);
if ($len === strlen($className)) {
return $this->_includePath . '/' . $className . '.php';
} elseif (($c = $className[$len]) &&
($c == '_') || ($c == '\\')) {
return $this->_includePath . '/' .
str_replace($c, '/', substr($className, $len + 1)) . '.php';
}
}

return false;
}

}
21 changes: 12 additions & 9 deletions framework/Autoloader/package.xml
Expand Up @@ -17,19 +17,18 @@
<email>jan@horde.org</email>
<active>yes</active>
</lead>
<date>2012-11-19</date>
<time>14:13:00</time>
<date>2014-01-30</date>
<version>
<release>2.0.2</release>
<api>1.0.0</api>
<release>2.1.0</release>
<api>2.1.0</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [mms] Add Horde_Autoloader_ClassPathMapper_PrefixString mapper implementation.
</notes>
<contents>
<dir baseinstalldir="/" name="/">
Expand All @@ -50,6 +49,7 @@
<file name="Application.php" role="php" />
<file name="Default.php" role="php" />
<file name="Prefix.php" role="php" />
<file name="PrefixString.php" role="php" />
</dir> <!-- /lib/Horde/Autoloader/ClassPathMapper -->
<file name="ClassPathMapper.php" role="php" />
<file name="Default.php" role="php" />
Expand All @@ -63,6 +63,7 @@
<dir name="ClassPathMapper">
<file name="ApplicationTest.php" role="test" />
<file name="DefaultTest.php" role="test" />
<file name="PrefixStringTest.php" role="test" />
<file name="PrefixTest.php" role="test" />
</dir> <!-- /test/Horde/Autoloader/ClassPathMapper -->
<file name="AllTests.php" role="test" />
Expand Down Expand Up @@ -103,12 +104,14 @@
<install as="Horde/Autoloader/ClassPathMapper/Application.php" name="lib/Horde/Autoloader/ClassPathMapper/Application.php" />
<install as="Horde/Autoloader/ClassPathMapper/Default.php" name="lib/Horde/Autoloader/ClassPathMapper/Default.php" />
<install as="Horde/Autoloader/ClassPathMapper/Prefix.php" name="lib/Horde/Autoloader/ClassPathMapper/Prefix.php" />
<install as="Horde/Autoloader/ClassPathMapper/PrefixString.php" name="lib/Horde/Autoloader/ClassPathMapper/PrefixString.php" />
<install as="Horde/Autoloader/AllTests.php" name="test/Horde/Autoloader/AllTests.php" />
<install as="Horde/Autoloader/AutoloaderTest.php" name="test/Horde/Autoloader/AutoloaderTest.php" />
<install as="Horde/Autoloader/bootstrap.php" name="test/Horde/Autoloader/bootstrap.php" />
<install as="Horde/Autoloader/phpunit.xml" name="test/Horde/Autoloader/phpunit.xml" />
<install as="Horde/Autoloader/ClassPathMapper/ApplicationTest.php" name="test/Horde/Autoloader/ClassPathMapper/ApplicationTest.php" />
<install as="Horde/Autoloader/ClassPathMapper/DefaultTest.php" name="test/Horde/Autoloader/ClassPathMapper/DefaultTest.php" />
<install as="Horde/Autoloader/ClassPathMapper/PrefixStringTest.php" name="test/Horde/Autoloader/ClassPathMapper/PrefixStringTest.php" />
<install as="Horde/Autoloader/ClassPathMapper/PrefixTest.php" name="test/Horde/Autoloader/ClassPathMapper/PrefixTest.php" />
</filelist>
</phprelease>
Expand Down Expand Up @@ -258,15 +261,15 @@
</release>
<release>
<version>
<release>2.0.2</release>
<api>1.0.0</api></version>
<release>2.1.0</release>
<api>2.1.0</api></version>
<stability>
<release>stable</release>
<api>stable</api></stability>
<date>2012-11-19</date>
<date>2014-01-30</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [mms] Add Horde_Autoloader_ClassPathMapper_PrefixString mapper implementation.
</notes>
</release>
</changelog>
Expand Down
@@ -0,0 +1,41 @@
<?php
/**
* @category Horde
* @package Autoloader
*/
class Horde_Autoloader_ClassPathMapper_PrefixStringTest
extends PHPUnit_Framework_TestCase
{
private $_mapper;

public function setUp()
{
$this->_mapper = new Horde_Autoloader_ClassPathMapper_PrefixString(
'App',
'dir'
);
}

public function providerClassNames()
{
return array(
array('App', 'dir/App.php'),
array('App_Foo', 'dir/Foo.php'),
array('App_Foo_Bar', 'dir/Foo/Bar.php'),
array('App\Foo\Bar', 'dir/Foo/Bar.php'),
array('app_foo', 'dir/foo.php')
);
}

/**
* @dataProvider providerClassNames
*/
public function testShouldMapClassToPath($className, $classPath)
{
$this->assertEquals(
$classPath,
$this->_mapper->mapToPath($className)
);
}

}

0 comments on commit fd027a0

Please sign in to comment.