Skip to content

Commit 4b07198

Browse files
committed
Removes customer autoloader
1 parent a28982e commit 4b07198

File tree

10 files changed

+66
-87
lines changed

10 files changed

+66
-87
lines changed

.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,37 @@
77
vendor/*
88
composer.lock
99
phpunit.xml
10+
11+
# IDEs and editors
12+
/.idea
13+
.project
14+
.classpath
15+
.c9/
16+
*.launch
17+
.settings/
18+
*.sublime-workspace
19+
.editorconfig
20+
21+
# IDE - VSCode
22+
.vscode/*
23+
!.vscode/settings.json
24+
!.vscode/tasks.json
25+
!.vscode/launch.json
26+
!.vscode/extensions.json
27+
28+
# misc
29+
/.sass-cache
30+
/connect.lock
31+
/coverage
32+
/libpeerconnection.log
33+
npm-debug.log
34+
yarn-error.log
35+
testem.log
36+
/typings
37+
38+
# System Files
39+
.DS_Store
40+
Thumbs.db
41+
42+
#
43+
*~

ActiveRecord.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
define('PHP_ACTIVERECORD_VERSION_ID','1.0');
66

7-
if (!defined('PHP_ACTIVERECORD_AUTOLOAD_PREPEND'))
8-
define('PHP_ACTIVERECORD_AUTOLOAD_PREPEND',true);
9-
107
require __DIR__.'/lib/Singleton.php';
118
require __DIR__.'/lib/Config.php';
129
require __DIR__.'/lib/Utils.php';
@@ -23,28 +20,3 @@
2320
require __DIR__.'/lib/CallBack.php';
2421
require __DIR__.'/lib/Exceptions.php';
2522
require __DIR__.'/lib/Cache.php';
26-
27-
if (!defined('PHP_ACTIVERECORD_AUTOLOAD_DISABLE'))
28-
spl_autoload_register('activerecord_autoload',false,PHP_ACTIVERECORD_AUTOLOAD_PREPEND);
29-
30-
function activerecord_autoload($class_name)
31-
{
32-
$path = ActiveRecord\Config::instance()->get_model_directory();
33-
$root = realpath(isset($path) ? $path : '.');
34-
35-
if (($namespaces = ActiveRecord\get_namespaces($class_name)))
36-
{
37-
$class_name = array_pop($namespaces);
38-
$directories = array();
39-
40-
foreach ($namespaces as $directory)
41-
$directories[] = $directory;
42-
43-
$root .= DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $directories);
44-
}
45-
46-
$file = "$root/$class_name.php";
47-
48-
if (file_exists($file))
49-
require_once $file;
50-
}

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,16 @@ Of course, there are some differences which will be obvious to the user if they
5252

5353
## Installation ##
5454

55-
Setup is very easy and straight-forward. There are essentially only three configuration points you must concern yourself with:
55+
Setup is very easy and straight-forward. There are essentially only two configuration points you must concern yourself with:
5656

57-
1. Setting the model autoload directory.
58-
2. Configuring your database connections.
59-
3. Setting the database connection to use for your environment.
57+
1. Configuring your database connections.
58+
2. Setting the database connection to use for your environment.
6059

6160
Example:
6261

6362
```php
6463
ActiveRecord\Config::initialize(function($cfg)
6564
{
66-
$cfg->set_model_directory('/path/to/your/model_directory');
6765
$cfg->set_connections(
6866
array(
6967
'development' => 'mysql://username:password@localhost/development_database_name',
@@ -78,7 +76,6 @@ Alternatively (w/o the 5.3 closure):
7876

7977
```php
8078
$cfg = ActiveRecord\Config::instance();
81-
$cfg->set_model_directory('/path/to/your/model_directory');
8279
$cfg->set_connections(
8380
array(
8481
'development' => 'mysql://username:password@localhost/development_database_name',

examples/orders/orders.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// initialize ActiveRecord
55
ActiveRecord\Config::initialize(function($cfg)
66
{
7-
$cfg->set_model_directory(__DIR__ . '/models');
87
$cfg->set_connections(array('development' => 'mysql://test:[email protected]/orders_test'));
98

109
// you can change the default connection with the below

examples/simple/simple.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class Book extends ActiveRecord\Model { }
99
// change the connection settings to whatever is appropriate for your mysql server
1010
ActiveRecord\Config::initialize(function($cfg)
1111
{
12-
$cfg->set_model_directory('.');
1312
$cfg->set_connections(array('development' => 'mysql://test:[email protected]/test'));
1413
});
1514

examples/simple/simple_with_options.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class Book extends ActiveRecord\Model
2424
// initialize ActiveRecord
2525
ActiveRecord\Config::initialize(function($cfg) use ($connections)
2626
{
27-
$cfg->set_model_directory('.');
2827
$cfg->set_connections($connections);
2928
});
3029

lib/Config.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class Config extends Singleton
2626
*
2727
* <code>
2828
* ActiveRecord\Config::initialize(function($cfg) {
29-
* $cfg->set_model_directory('/your/app/models');
3029
* $cfg->set_connections(array(
3130
* 'development' => 'mysql://user:[email protected]/awesome_development',
3231
* 'production' => 'mysql://user:[email protected]/awesome_production'));
@@ -50,14 +49,6 @@ class Config extends Singleton
5049
*/
5150
private $connections = array();
5251

53-
/**
54-
* Directory for the auto_loading of model classes.
55-
*
56-
* @see activerecord_autoload
57-
* @var string
58-
*/
59-
private $model_directory;
60-
6152
/**
6253
* Switch for logging.
6354
*
@@ -94,7 +85,6 @@ class Config extends Singleton
9485
*
9586
* <code>
9687
* ActiveRecord\Config::initialize(function($cfg) {
97-
* $cfg->set_model_directory('/path/to/your/model_directory');
9888
* $cfg->set_connections(array(
9989
* 'development' => 'mysql://username:[email protected]/database_name'));
10090
* });
@@ -197,31 +187,6 @@ public function set_default_connection($name)
197187
$this->default_connection = $name;
198188
}
199189

200-
/**
201-
* Sets the directory where models are located.
202-
*
203-
* @param string $dir Directory path containing your models
204-
* @return void
205-
*/
206-
public function set_model_directory($dir)
207-
{
208-
$this->model_directory = $dir;
209-
}
210-
211-
/**
212-
* Returns the model directory.
213-
*
214-
* @return string
215-
* @throws ConfigException if specified directory was not found
216-
*/
217-
public function get_model_directory()
218-
{
219-
if ($this->model_directory && !file_exists($this->model_directory))
220-
throw new ConfigException('Invalid or non-existent directory: '.$this->model_directory);
221-
222-
return $this->model_directory;
223-
}
224-
225190
/**
226191
* Turn on/off logging
227192
*

test/ActiveRecordTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,6 @@ public function test_reload_protected_attribute()
164164
$this->assert_not_equals("Should not stay", $book->name);
165165
}
166166

167-
public function test_active_record_model_home_not_set()
168-
{
169-
$home = ActiveRecord\Config::instance()->get_model_directory();
170-
ActiveRecord\Config::instance()->set_model_directory(__FILE__);
171-
$this->assert_equals(false,class_exists('TestAutoload'));
172-
173-
ActiveRecord\Config::instance()->set_model_directory($home);
174-
}
175-
176-
public function test_auto_load_with_namespaced_model()
177-
{
178-
$this->assert_true(class_exists('NamespaceTest\Book'));
179-
}
180-
181167
public function test_namespace_gets_stripped_from_table_name()
182168
{
183169
$model = new NamespaceTest\Book();

test/helpers/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
**/
1919

2020
require_once 'vendor/autoload.php';
21+
require_once __DIR__ .'/model_autoloader.php';
2122

2223
require_once 'SnakeCase_PHPUnit_Framework_TestCase.php';
2324

@@ -38,7 +39,6 @@
3839

3940
ActiveRecord\Config::initialize(function($cfg)
4041
{
41-
$cfg->set_model_directory(realpath(__DIR__ . '/../models'));
4242
$cfg->set_connections(array(
4343
'mysql' => getenv('PHPAR_MYSQL') ?: 'mysql://test:[email protected]/test',
4444
'pgsql' => getenv('PHPAR_PGSQL') ?: 'pgsql://test:[email protected]/test',
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/*
3+
* We currently run our own autoloader because our model files cannot be readily loaded by Composer.
4+
* Eventually, we will want our models to be loaded via Composer and will achieve this by "moving"
5+
* them to the namespaces corresponding to their locations on disk.
6+
*
7+
* However, changing the namespace of all our models is a large and risky refactoring. Rather than
8+
* perform this refactoring all at once, this autoloader allows us to refactor models one at a time.
9+
*/
10+
11+
// Register our autoloader to run after Composer's by using prepend = false
12+
spl_autoload_register("phparTestAutoloaderTemporary", true, false);
13+
14+
function phparTestAutoloaderTemporary($className) {
15+
// Look in these directories for the class that is to be autoloader
16+
$root = __DIR__ . "/..";
17+
$modelDirectories = ["models"];
18+
19+
// Replace namespace seperators with directory separators
20+
$modelPath = str_replace("\\", "/", $className);
21+
22+
foreach ($modelDirectories as $modelDirectory) {
23+
if (file_exists("$root/$modelDirectory/$modelPath.php")) {
24+
require_once "$root/$modelDirectory/$modelPath.php";
25+
break;
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)