1: <?php
2: /*
3: * Simpletools Framework.
4: * Copyright (c) 2009, Marcin Rosinski. (https://www.getsimpletools.com/)
5: * All rights reserved.
6: *
7: * LICENCE
8: *
9: * Redistribution and use in source and binary forms, with or without modification,
10: * are permitted provided that the following conditions are met:
11: *
12: * - Redistributions of source code must retain the above copyright notice,
13: * this list of conditions and the following disclaimer.
14: *
15: * - Redistributions in binary form must reproduce the above copyright notice,
16: * this list of conditions and the following disclaimer in the documentation and/or other
17: * materials provided with the distribution.
18: *
19: * - Neither the name of the Simpletools nor the names of its contributors may be used to
20: * endorse or promote products derived from this software without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
23: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
28: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29: * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30: *
31: * @framework Simpletools
32: * @description MVC framework
33: * @copyright Copyright (c) 2009 Marcin Rosinski. (https://www.getsimpletools.com/)
34: * @license (BSD)
35: * @version Ver: 2.0.13 2014-11-30 11:19
36: *
37: */
38:
39: namespace Simpletools\Mvc;
40:
41: /**
42: * MVC Model
43: */
44: class Model
45: {
46: protected $_appDir = '';
47: private static $_instance = null;
48: protected $_activeRoutingNamespace = '';
49:
50: public function __construct($appDir,$_activeRoutingNamespace='')
51: {
52: self::$_instance = $this;
53: self::$_instance->_appDir = $appDir;
54: self::$_instance->objects = array();
55: self::$_instance->_activeRoutingNamespace = $_activeRoutingNamespace;
56: }
57:
58: public static function settings(array $settings)
59: {
60: if(empty(self::$_instance))
61: new \Simpletools\Mvc\Model($settings['application_dir']);
62: }
63:
64: public static function getModelDir()
65: {
66: return self::$_instance->_appDir;
67: }
68:
69: public static function setActiveRoutingNamespace($ns)
70: {
71: $ns = str_replace('/','\\',$ns);
72: self::$_instance->_activeRoutingNamespace = $ns;
73: }
74:
75: public static function __callStatic($name, $args)
76: {
77: array_unshift($args, $name);
78: return call_user_func_array(array('self','getInstance'), $args);
79: }
80:
81: public static function of($model)
82: {
83: $args = func_get_args();
84: return call_user_func_array(array('self','getInstance'), $args);
85: }
86:
87: public static function getInstance($model)
88: {
89: $initArgs = func_get_args();
90: array_shift($initArgs);
91:
92: if(!isset(self::$_instance))
93: throw new \Exception('\Simpletools\Mvc\Model: There are no settings provided.');
94:
95: if(!isset($model) OR $model == '')
96: throw new \Exception('\Simpletools\Mvc\Model: Model name required.');
97:
98: //namespacing
99: //$model = str_replace('/','\\',$model);
100:
101: $namespace = self::$_instance->_activeRoutingNamespace;
102: $orgModel = $model;
103:
104: $n = substr($model,0,1);
105: if($n=='\\' OR $n == '/')
106: {
107: $model = trim(str_replace('/','\\',$model),'\\');
108: $_path = explode('\\',$model);
109: $model = array_pop($_path);
110: $namespace = implode('\\',$_path);
111: }
112:
113: $class = (!$namespace) ? $model.'Model' : $namespace."\\".$model.'Model';
114:
115: if(!isset(self::$_instance->objects[$class]))
116: {
117: if(!class_exists($class))
118: {
119: $p = str_replace('\\',DIRECTORY_SEPARATOR,$namespace).'/'.$model.'Model.php';
120: $p = realpath(self::$_instance->_appDir.'/models/'.$p);
121:
122: if($p===false)
123: {
124: $p = str_replace('\\',DIRECTORY_SEPARATOR,$namespace).'/'.$model.'.php';
125: $p = realpath(self::$_instance->_appDir.'/models/'.$p);
126:
127: if($p===false)
128: {
129: throw new \Exception('SimpleModel: Couldn\'t find '.$model.' model in '.self::$_instance->_appDir.'/models/');
130: }
131: }
132:
133: require($p);
134: }
135:
136: $obj = new $class();
137:
138: if($obj instanceof \Simpletools\Db\Mysql\Model)
139: {
140: unset($obj);
141:
142: $obj = \Simpletools\Db\Mysql\Client::getInstance();
143:
144: self::$_instance->objects[$class] = $obj->getInstanceOfModel($model,$initArgs,$namespace);
145: }
146: else
147: {
148: if(is_callable(array($obj,'init')))
149: {
150: call_user_func_array(array($obj,'init'),$initArgs);
151: }
152:
153: self::$_instance->objects[$class] = $obj;
154: }
155:
156: if(is_callable(array(self::$_instance->objects[$class],'setActiveRoutingNamespace')))
157: {
158: self::$_instance->objects[$class]->setActiveRoutingNamespace(self::$_instance->_activeRoutingNamespace);
159: }
160: }
161:
162: return self::$_instance->objects[$class];
163: }
164: }
165:
166: ?>