"Refacorting App::import() to implement vendors loading.

Added ability to set the file extension of the file to be loaded"

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6241 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2007-12-24 22:39:28 +00:00
parent 586e93f59f
commit 71039959d0
4 changed files with 100 additions and 7 deletions

View file

@ -742,13 +742,13 @@ class App extends Object {
*
* @param mixed $type The type of Class if passed as a string, or all params can be passed as an single array to $type,
* @param string $name Name of the Class or a unique name for the file
* @param mixed $parent boolean true if Class Parent should be searched, accepts key => value array('parent' => $parent ,'file' => $file, 'search' => $search);
* @param mixed $parent boolean true if Class Parent should be searched, accepts key => value array('parent' => $parent ,'file' => $file, 'search' => $search, 'ext' => '$ext');
* $ext allows setting the extension of the file name based on Inflector::underscore($name) . ".$ext";
* @param array $search paths to search for files, array('path 1', 'path 2', 'path 3');
* @param string $file full name of the file to search for including extension
* @param boolean $return, return the loaded file, the file must have a return statement in it to work: return $variable;
* @return boolean true if Class is already in memory or if file is found and loaded, false if not
* @access public
* @todo when App::import() is called without params initialize all the files from the core in one call
*/
function import($type = null, $name = null, $parent = true, $search = array(), $file = null, $return = false) {
$plugin = null;
@ -804,6 +804,10 @@ class App extends Object {
}
$_this =& App::getInstance();
$_this->return = $return;
if (isset($ext)) {
$file = Inflector::underscore($name) . ".$ext";
}
$ext = $_this->__settings($type, $plugin, $parent);
if ($name != null && !class_exists($name . $ext['class'])) {
@ -834,7 +838,10 @@ class App extends Object {
$find = Inflector::underscore($name . $ext['suffix']).'.php';
if ($plugin) {
$find = $ext['path'] . $find;
$paths = $_this->search;
foreach ($paths as $key => $value) {
$_this->search[$key] = $value . $ext['path'];
}
$plugin = Inflector::camelize($plugin);
}
}
@ -1084,10 +1091,10 @@ class App extends Object {
return array('class' => $type, 'suffix' => null, 'path' => $path);
break;
case 'vendor':
die('Not Implemented');
if ($plugin) {
$path = $plugin . DS . 'vendors' . DS;
}
return array('class' => null, 'suffix' => null, 'path' => $path);
break;
default:
$type = $suffix = $path = null;

View file

@ -151,17 +151,41 @@ class AppImportTest extends UnitTestCase {
/*
function testMultipleLoadingByType() {
$classes = array_flip(get_declared_classes());
$this->assertFalse(isset($classes['Apple']));
$this->assertFalse(isset($classes['OtherPlugin']));
$this->assertFalse(isset($classes['MyPlugin']));
$load = App::import('Model', array('MyPlugin.OtherModel', 'MyPlugin.MyPlugin'));
$load = App::import('Model', array('MyPlugin.OtherPlugin', 'MyPlugin.MyPlugin'));
$this->assertTrue($load);
$classes = array_flip(get_declared_classes());
$this->assertTrue(isset($classes['Apple']));
$this->assertTrue(isset($classes['OtherPlugin']));
$this->assertTrue(isset($classes['MyPlugin']));
}
*/
function testLoadingVendor() {
Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS));
Configure::write('vendorPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors'. DS));
ob_start();
$result = App::import('Vendor', 'TestPlugin.TestPluginAsset', array('ext' => 'css'));
$text = ob_get_clean();
$this->assertTrue($result);
$this->assertEqual($text, 'this is the test plugin asset css file');
ob_start();
$result = App::import('Vendor', 'TestAsset', array('ext' => 'css'));
$text = ob_get_clean();
$this->assertTrue($result);
$this->assertEqual($text, 'this is the test asset css file');
$result = App::import('Vendor', 'TestPlugin.SamplePlugin');
$this->assertTrue($result);
$this->assertTrue(class_exists('SamplePluginClassTestName'));
$result = App::import('Vendor', 'Sample');
$this->assertTrue($result);
$this->assertTrue(class_exists('SampleClassTestName'));
}
}
?>

View file

@ -0,0 +1,31 @@
<?php
/* SVN FILE: $Id$ */
/**
* Short description for file.
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2005-2007, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake.tests
* @subpackage cake.tests.test_app.plugins.test_plugin.vendors.sample
* @since CakePHP(tm) v 1.2.0.4206
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
class SamplePluginClassTestName {
}
?>

View file

@ -0,0 +1,31 @@
<?php
/* SVN FILE: $Id$ */
/**
* Short description for file.
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2005-2007, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake.tests
* @subpackage cake.tests.test_app.vendors.sample
* @since CakePHP(tm) v 1.2.0.4206
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
class SampleClassTestName {
}
?>