/** * Constructor, called by the front controller. * * @param A_Locator $locator */ public function __construct($locator = null) { if ($locator) { $this->locator = $locator; $this->request = $locator->get('Request'); $this->response = $locator->get('Response'); } }
function testLocatorClassLoader() { $locator = new A_Locator(); $dir = dirname(__FILE__); // set directory for classes that use PEAR Foo_Bar style naming $locator->setDir("{$dir}/../include/", 'Foo'); $result = $locator->loadClass('Foo_Bar'); $this->assertTrue($result); $this->assertTrue(class_exists('Foo_Bar', false)); $locator->setDir("{$dir}/../include/Foo", '/^Foo.*/'); $result = $locator->loadClass('FooBar'); }
function testController_ActionNotNull() { $locator = new A_Locator(); $base_path = ''; $default_action = null; $mapper = new A_Controller_Mapper($base_path, $default_action); $locator->set('Mapper', $mapper); $Controller_Action = new A_Controller_Action($locator); $result = true; $this->assertTrue($result); $this->assertFalse(!$result); }
public function render() { // if no value and addtype set then use it if ($this->addtype && $this->value == '') { $savetype = $this->type; $this->type = $this->addtype; } else { $savetype = null; } if (isset($this->type['renderer'])) { if (!isset($this->renderer)) { $this->renderer = $this->type['renderer']; unset($this->type['renderer']); } } // string is name of class with underscores in loadable convention if (is_string($this->renderer)) { // load locator if not loaded if (A_Locator::loadClass($this->renderer)) { // instantiate render passing the array of field $this->renderer = new $this->renderer(); } } if (isset($this->renderer) && method_exists($this->renderer, 'render')) { // set name and value in array passed to renderer $this->type['name'] = $this->name; $this->type['value'] = $this->value; $this->renderer->import($this->type); return $this->renderer->render(); } // restore type if ($savetype) { $this->type = $savetype; } return $this->value; }
<?php /** * This is a simple socket server setup for WebSockets */ $ConfigArray = array('PATH' => dirname(__FILE__) . '/', 'APP' => dirname(__FILE__) . '/app', 'SOCKET' => array('host' => 'localhost', 'port' => '9091', 'class-client' => 'A_Socket_Client_Websocket', 'class-message' => 'A_Socket_Message_Json', 'message-connect' => '{"type":{"module":"","controller":"connect","action":"user"},"data":null}', 'message-disconnect' => '{"type":{"module":"","controller":"disconnect","action":"user"},"data":null}'), 'DEFAULT_ACTION' => array('', 'main', 'main'), 'ERROR_ACTION' => array('', 'main', 'main')); include $ConfigArray['PATH'] . '../../A/Locator.php'; $Locator = new A_Locator(); $Locator->autoload(); $Config = new A_Config_Php(); $Config->import($ConfigArray); $Locator->set('Config', $Config); $EventListener = new A_Socket_Eventlistener_Frontcontroller($Locator); $EventManager = new A_Event_Manager(); $EventManager->addEventListener($EventListener); $Server = new A_Socket_Server($EventManager); $Server->run($ConfigArray['SOCKET']);
<?php /** * This is the simplest bootstrap and controller. * No Response object is used and controllers echo their output. * URLs are in the form index.php?module=&controller=home&action=index (but params are defaults so not necessary) */ $ConfigArray['PATH'] = dirname(__FILE__) . '/'; include $ConfigArray['PATH'] . '../../A/Locator.php'; $Locator = new A_Locator(); $Locator->autoload(); $Request = new A_Http_Request(); $Locator->set('Request', $Request); $Front = new A_Controller_Front($ConfigArray['PATH'], array('', 'home', ''), array('', 'home', '')); $Front->run($Locator);
<?php require_once 'config.php'; require_once dirname(__FILE__) . '/../../A/Locator.php'; #require_once('A/Http/Request.php'); #require_once('A/Http/Response.php'); $Locator = new A_Locator(); $Locator->autoload(); #$Response = new A_Http_Response(); $Locator->set('Request', new A_Http_Request()); #$Locator->set('Response', $Response); require_once 'controllers/Form1.php'; $Controller = new Form1(); $Controller->index($Locator); #$Response->out();
<?php #include_once 'A/Db/Pdo.php'; #include_once 'A/Locator.php'; $locator = new A_Locator(); $locator->register(array('A_Db_Pdo' => array('__construct' => array($config['db'])))); #$db = $locator->get('DB', 'A_Db_Pdo') or die ('Error: could not connect to DB'); $db = new A_Db_Pdo($config['db']) or die('Error: could not connect to DB');
<?php error_reporting(E_ALL); require 'config.php'; require dirname(__FILE__) . '/../../A/Locator.php'; #require_once('A/Http/Request.php'); #require_once('A/Http/Response.php'); #require_once('A/Controller/Front.php'); #require_once('A/Controller/Mapper.php'); // create Registry/Loader and initialize autoloading $Locator = new A_Locator(); $Locator->autoload(); $Response = new A_Http_Response(); $Locator->set('Request', new A_Http_Request()); $Locator->set('Response', $Response); $DefaultAction = array('', 'home', 'index'); $ErrorAction = 'error'; $Mapper = new A_Controller_Mapper('', $DefaultAction); #$Mapper->setParams('action', ''); // add this line to run 0.3.x code #$Mapper->setDefaultMethod('execute'); // add this line to run 0.4.x and 0.3.x code #$Mapper->setDefaultMethod('run'); // add this line to run 0.7.x code #$Mapper->setDefaultDir('default'); // add this for a default module directory $Controller = new A_Controller_Front($Mapper, $ErrorAction); #$Controller = new A_Controller_Front('', $ErrorAction, $DefaultAction); // have FC create Mapper $Controller->run($Locator); $Response->out(); if ($Controller->isError()) { echo '<br/><br/>Front Controller errors: ' . $Controller->getErrorMsg(); }
<?php require 'config.php'; require $ConfigArray['LIB'] . 'A/Locator.php'; $Locator = new A_Locator(); $Locator->autoload(); // initializing autoloading $Config = new A_Collection($ConfigArray); $Request = new A_Http_Request(); $Response = new A_Http_Response(); $Response->setTemplate('layouts/main.php'); $Response->set('BASE', $ConfigArray['BASE']); $Response->set('title', $ConfigArray['TITLE']); $Response->set('head', ''); $Session = new A_Session(); $UserSession = new A_User_Session($Session); $Locator->set('Config', $Config); $Locator->set('Request', $Request); $Locator->set('Response', $Response); $Locator->set('Session', $Session); $Locator->set('UserSession', $UserSession); $Router = new A_Http_Pathinfo(); $Router->run($Request); $Mapper = new A_Controller_Mapper($ConfigArray['APP'], array('', 'home', '')); $Controller = new A_Controller_Front($Mapper, array('', 'error', '')); $Controller->addPreFilter(new A_User_Prefilter_Group($Session, array('', 'user', 'login'))); $Controller->run($Locator); $Response->run($Locator); echo $Response->render(); echo $Controller->getErrorMsg(', ');
<?php require_once 'config.php'; require_once $ConfigArray['LIB'] . 'A/Locator.php'; #require_once 'A/DataContainer.php'; #require_once 'A/Session.php'; #require_once 'A/Locator.php'; #require_once 'A/Http/Request.php'; #require_once 'A/Http/Response.php'; #require_once 'A/Http/Pathinfo.php'; #require_once 'A/Controller/Front.php'; #require_once 'A/Controller/Mapper.php'; #require_once 'A/Template/Strreplace.php'; $Locator = new A_Locator(); $Locator->autoload(); // initialize autoloading $Request = new A_Http_Request(); $Response = new A_Http_Response(); $Session = new A_Session(); $Locator->set('Config', new A_Collection($ConfigArray)); $Locator->set('Request', $Request); $Locator->set('Response', $Response); $Locator->set('Session', $Session); $Mapper = new A_Http_Pathinfo(array('' => array('controller', 'action', 'id'))); // array('' => array('class', 'method'))); $Mapper->run($Request); // copies clean URL values into the Request based on the map $Action = array('', 'home', 'index'); $Mapper = new A_Controller_Mapper($ConfigArray['APP'], $Action); // action controllers in default 'controller' directory #$Mapper->setDefaultMethod('execute'); // uncomment to make compatable with pre 0.4.x
{ echo "CALL FUNCTION setDb()<br/>\n"; $this->db = $db; } } class FooModel extends BaseModel { } class BarModel extends BaseModel { } // create a config object to show how injected data can come from a registered container $ConfigObj = new Config(); $ConfigObj->set('db', $config['db']); // create Locator which is Registry + Loader + DI $locator = new A_Locator(); $locator->set('Config', $ConfigObj); // register dependency for database connector to inject config array into contructor // future calls to $locator->get('', 'A_Db_Pdo') will pass array to constructor when instantiating $locator->register(array('A_Db_Pdo' => array('__construct' => array(array('A_Locator' => 'container', 'name' => 'Config', 'class' => '', 'key' => 'db'))))); // register dependencies for classes that will have A_Db_Pdo object injected // Note that A_Db_Pdo object is put in Registry with name 'DB' so later call will just get object from Registry $locator->register(array('BaseModel' => array('__construct' => array(array('A_Locator' => 'get', 'name' => 'DB', 'class' => 'A_Db_Pdo')), 'set' => array('base', 'Data injected into set(base, )')), 'FooModel' => array('__construct' => array(array('A_Locator' => 'get', 'name' => 'DB', 'class' => 'A_Db_Pdo'))), 'BarModel' => array('set' => array('bar', 'Data injected into set(bar, )'), 'setDb' => array(array('A_Locator' => 'get', 'name' => 'DB', 'class' => 'A_Db_Pdo'))))); ?> <html> <body> <?php $FooModel = $locator->get('', 'FooModel', 'BaseModel'); dump($FooModel, 'FooModel: '); $BarModel = $locator->get('', 'BarModel'); dump($BarModel, 'BarModel: ');
<html> <body> <?php include '../../A/Locator.php'; $locator = new A_Locator(); $locator->autoload(); echo "A_Locator autoload()<br/>"; /* $duration = new A_Datetime_Duration(); if ($duration) echo "A_Datetime_Duration autoloaded<br/>"; $bar = new Bar(); $foobar = new Foo_Bar(); */ $bar = new Bar(); $foobar = new Foo_Bar(); $alocator = new A_Locator(); if (class_exists('A_Locator')) { echo "A_Locator autoloaded<br/>"; } ?> </body> </html>
<?php // Hangman game example ported from WACT hangman example and refactored to // follow MVC pattern. require 'config.php'; require dirname(__FILE__) . '/../../A/Locator.php'; #include_once 'A/Http/Request.php'; #include_once 'A/Http/Response.php'; #include_once 'A/Template/Strreplace.php'; #include_once 'A/Rule/Abstract.php'; #include_once 'A/Rule/Notnull.php'; #include_once 'A/Filter/Regexp.php'; #include_once 'A/Controller/Input.php'; #include_once 'A/Controller/App.php'; // create Registry/Loader and initialize autoloading $Locator = new A_Locator(); $Locator->autoload(); //----------------------------------------------------------------------------- class HangmanGame { public $word; public $level; public $levels; public $guesses = ''; public $misses = 0; public $feedback = ''; public $availableLetters; function HangmanGame() { $this->levels = $this->getLevels(); }
<html> <body> <?php include '../../A/Locator.php'; $path = dirname(__FILE__) . '/includes'; echo "path={$path}<br/>"; $locator = new A_Locator(); $locator->autoload(); echo "A_Locator autoload()<br/>"; $locator->setDir($path, 'Foo'); echo "A_Locator setDir() to load classes in namespace Foo_ or \\Foo\\ from {$path}/<br/>"; $regex = '/^Foo.*/'; $locator->setDir("{$path}/Foo", $regex); echo "A_Locator setDir() to load classes in matching regex '{$regex}' from {$path}/Foo/<br/>"; /* $duration = new A_Datetime_Duration(); if ($duration) echo "A_Datetime_Duration autoloaded<br/>"; $bar = new Bar(); $foobar = new Foo_Bar(); */ $classes = array('FooBar', 'Foo_Bar', '\\Foo\\BarNS', '\\Foo\\Bar\\BazNS'); foreach ($classes as $class) { echo "Instantiate {$class}<br/>"; $foobar = new $class(); if (class_exists($class)) { echo "{$class} autoloaded<br/>"; } } ?> </body> </html>
<?php require_once 'config.php'; // Init autoload using Locator require dirname(__FILE__) . '/../../A/Locator.php'; $Locator = new A_Locator(); $Locator->autoload(); $Request = new A_Http_Request(); $Response = new A_Http_Response(); $Session = new A_Session(); $Session->start(); $UserSession = new A_User_Session($Session); $UserSession->getAuth(); $UserSession->setTimestamp(time()); $Locator->set('Request', $Request); $Locator->set('Response', $Response); $Locator->set('Session', $Session); $Locator->set('UserSession', $UserSession); #$UserSession->login('test', 'member|editor'); #$UserSession->logout(); #$rule = new A_Rule_UserIsLevel(15); $UserAccess = new A_User_Access($UserSession); $UserAccess->addRule(new A_User_Rule_Isloggedin(array('', 'login', 'index'))); #$UserAccess->addRule(new A_User_Rule_Ingroup(array('manager'), 'error')); $UserAccess->run($Locator); #dump($UserAccess); #dump($Locator); $Mapper = new A_Controller_Mapper('', array('', 'example', 'index')); // dump($Mapper); //dump2($Request); $Controller = new A_Controller_Front($Mapper, array('', 'error', 'index'));
/** * Start dispatch process * * @param A_Locator $locator * @return string */ public function run($locator = null) { if (!$locator) { $locator = new A_Locator(); } if (!$locator->has('Request')) { $locator->set('Request', new A_Http_Request()); } if ($locator->has('Response')) { // make sure that Response has Locator set so it can use loading and DI $locator->get('Response')->setLocator($locator); } $mapper = $this->getMapper(); $locator->set('Mapper', $mapper); // set mapper in registry for mvc loader to use $this->locator = $locator; $route = $mapper->getRoute($locator->get('Request')); $error_route = $this->errorRoute; $n = -1; while ($route) { $error = self::NO_ERROR; $mapper->setRoute($route); // set dir/class/method $n++; $class = $mapper->getFormattedClass(); $method = $mapper->getFormattedMethod(); $dir = $mapper->getPath(); $this->routeHistory[] = $route; // save history of routes $route = null; $result = $locator->loadClass($class, $dir); if ($locator->isError()) { $this->errorMsg = array_merge($this->errorMsg, $locator->getErrorMsg()); } if ($result) { $class = str_replace('-', '_', $class); $controller = new $class($locator); $this->controllerHistory[] = $controller; // save history of controller if ($this->preFilters) { // run pre filtes and check if forward $change_route = $this->runFilters($controller, $this->preFilters); if ($change_route !== null) { // if filter forwarded then set new route and reloop for mapping $route = $change_route; continue; } } if (method_exists($controller, $this->dispatchMethod)) { $route = $controller->{$this->dispatchMethod}($locator, $method); } else { if (method_exists($controller, $method)) { $route = $controller->{$method}($locator); } else { $method = $mapper->getDefaultMethod(); if (method_exists($controller, $method)) { $route = $controller->{$method}($locator); } else { $error = self::NO_METHOD . ': ' . $method; // no known method to dispatch } } } if ($this->postFilters) { $change_route = $this->runFilters($controller, $this->postFilters); if ($change_route !== null) { // if filter forwarded then set route to loop again $route = $change_route; } } } elseif ($error_route) { $route = $error_route; $error_route = null; $error = self::NO_CLASS . ": {$class}. Using error route: " . (is_array($route) ? implode('/', $route) : $route) . '.'; // cannot load class and not error route } elseif ($n == 0) { $error = self::NO_CLASS . ": {$class}."; // cannot load class and not error route } if ($error) { $this->errorMsg[] = $error; } } return $error; }
<?php error_reporting(E_ALL); require 'config.php'; require dirname(__FILE__) . '/../../A/Locator.php'; require 'controllers/upload_files.php'; $Locator = new A_Locator(); $Locator->autoload(); $Request = new A_Http_Request(); $Response = new A_Http_Response(); $Locator->set('Request', $Request); $Locator->set('Response', $Response); $Controller = new upload_files($Locator); $Controller->index($Locator); $Response->out();
/** * Run the form as a Command object passed a Registry containing the Request * * @param A_Locator $locator * @return bool True if error occured */ public function run($locator) { $request = $locator->get('Request'); $this->isValid($request); return $this->error; }