Skip to content

Commit 69e5a1d

Browse files
author
Samuel Leathers
committed
initial commit
0 parents  commit 69e5a1d

10 files changed

+220
-0
lines changed

autoload.inc

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
class AutoLoader {
3+
protected static $paths = array();
4+
public static function load($class) {
5+
if(class_exists($class, FALSE)) {
6+
return;
7+
}
8+
foreach (self::$paths as $path) {
9+
if(file_exists("$path/controllers/$class.class.inc")) {
10+
require_once("$path/controllers/$class.class.inc");
11+
return;
12+
}
13+
else if(file_exists("$path/models/$class.class.inc")) {
14+
require_once("$path/models/$class.class.inc");
15+
return;
16+
}
17+
18+
}
19+
/** Since we override drupal's autoloader, make sure we
20+
hand it off to drupal if nothing is found.
21+
*/
22+
return _registry_check_code('class', $class);
23+
}
24+
public static function addPath($path) {
25+
$path = realpath($path);
26+
if($path) {
27+
self::$paths[] = $path;
28+
}
29+
}
30+
}
31+
spl_autoload_register(array('AutoLoader', 'load'));

controllers/BaseController.class.inc

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
class BaseController {
4+
protected $data;
5+
public function __construct() {
6+
$this->data = array();
7+
}
8+
public function access() {
9+
return false;
10+
}
11+
protected function render($template = NULL) {
12+
global $controllerName, $method, $module_path;
13+
if(!$template) {
14+
$template = "$method";
15+
}
16+
foreach($this->data as $key => $value) {
17+
$$key = $value;
18+
}
19+
ob_start();
20+
include("$module_path/views/$controllerName/$template.html.php");
21+
$template = ob_get_contents();
22+
ob_end_clean();
23+
return $template;
24+
25+
}
26+
protected function setVar($name,$var) {
27+
$this->data[$name] = $var;
28+
}
29+
}

controllers/HelloController.class.inc

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
class HelloController extends BaseController {
4+
public function hello($args) {
5+
global $user;
6+
if(isset($args[0])) {
7+
$name = $args[0];
8+
}
9+
else {
10+
$name = "Anonymous";
11+
}
12+
$this->setVar('name',$name);
13+
if($user->uid) {
14+
return $this->render();
15+
}
16+
return $this->render('hello_guest');
17+
}
18+
public function denied() {
19+
return t('this page should be inaccessible');
20+
21+
}
22+
/**
23+
*/
24+
public function access() {
25+
global $method;
26+
if($method == 'denied') {
27+
return false;
28+
}
29+
return true;
30+
}
31+
32+
}
33+
?>

models/Applicant.class.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
class Applicant extends BaseModel {
4+
5+
}

models/BaseModel.class.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
class BaseModel {
3+
private $data;
4+
5+
public function __construct() {
6+
$this->data = array();
7+
8+
}
9+
public function __get($var) {
10+
if(isset($this->data[$var])) {
11+
return $this->data[$var];
12+
}
13+
return $this->$var;
14+
}
15+
public function __set($var,$value) {
16+
$this->data[$var] = $value;
17+
}
18+
19+
}

models/REUApplicant.class.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
class REUApplicant extends Applicant {
3+
}

mvc.info

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name = Drupal MVC Module
2+
description = A module allowing mvc application development inside drupal
3+
package = ECOS
4+
version = 0.1
5+
core = 7.x

mvc.module

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/**
3+
* @author Samuel Leathers
4+
* @copyright Copyright (c) 2011, The Pennsylvania State University
5+
* version 0.1
6+
* mvc.module
7+
*
8+
* @package mvc
9+
*/
10+
11+
12+
// $Id$
13+
14+
/**
15+
* This module allows creating mvc applications from within drupal
16+
*
17+
*/
18+
/**
19+
* Instantiate class autoloader
20+
*/
21+
module_load_include('inc', 'mvc', 'autoload');
22+
23+
/**
24+
* Implementation of hook_init().
25+
*/
26+
function mvc_init() {
27+
global $mvc_modules;
28+
$mvc_modules = module_invoke_all('mvc_app');
29+
foreach($mvc_modules as $mod) {
30+
AutoLoader::addPath($mod['path']);
31+
}
32+
AutoLoader::addPath(drupal_get_path('module','mvc'));
33+
}
34+
35+
36+
/**
37+
* Sample Implementation of hook_mvc_app
38+
*
39+
* {{{
40+
* function mvc_mvc_app() {
41+
* $mvc = new ArrayObject();
42+
* $mvc['base'] = 'mvc';
43+
* $mvc['path'] = drupal_get_path('module','mvc');
44+
* return $mvc;
45+
* }
46+
* }}}
47+
*
48+
*/
49+
50+
51+
52+
/**
53+
* Implementation of hook_menu().
54+
*/
55+
function mvc_menu() {
56+
global $mvc_modules;
57+
$items = array();
58+
// Graduate Application Page
59+
foreach($mvc_modules as $mod) {
60+
$items[ $mod['base'].'/%/%'] = array(
61+
'page callback' => 'mvc_controller',
62+
'page arguments' => array($mod['path']),
63+
'access callback' => TRUE,
64+
'type' => MENU_CALLBACK
65+
);
66+
}
67+
return $items;
68+
}
69+
70+
/**
71+
* dispatcher function for controller
72+
* @return object View rendered by controller
73+
*/
74+
function mvc_controller($modpath) {
75+
global $controllerName, $method, $module_path;
76+
$module_path = $modpath;
77+
$controllerName = arg(1);
78+
$fullControllerName = $controllerName.'Controller';
79+
$method = arg(2);
80+
$params = array_merge(array_diff(arg(),array(arg(0),arg(1),arg(2))));
81+
$controller = new $fullControllerName;
82+
if($controller->access()) {
83+
if(method_exists($controller, $method)) {
84+
return $controller->$method($params);
85+
}
86+
else {
87+
drupal_not_found();
88+
}
89+
}
90+
else {
91+
drupal_access_denied();
92+
}
93+
}

views/hello/hello.html.php

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>Hi, My Name is <?php echo $name; ?></p>

views/hello/hello_guest.html.php

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>Hi Guest, My Name is <?php echo $name; ?></p>

0 commit comments

Comments
 (0)