/** * Construct association object * * @param string $assocName * @param array $options */ public function __construct($assocName, $options, Mad_Model_Base $model) { $valid = array('className', 'foreignKey', 'associationForeignKey', 'primaryKey', 'associationPrimaryKey', 'include', 'select', 'conditions', 'order', 'finderSql', 'through', 'dependent' => 'nullify'); $this->_options = Mad_Support_Base::assertValidKeys($options, $valid); $this->_assocName = $assocName; $this->_model = $model; $this->_conn = $model->connection(); // throw fatal error if through option is invalid $this->_throughClass = Mad_Support_Inflector::classify($this->_options['through']); class_exists($this->_throughClass); // get inflections $toMethod = Mad_Support_Inflector::camelize($this->_assocName, 'lower'); $toMethod = str_replace('/', '_', $toMethod); $singular = Mad_Support_Inflector::singularize($toMethod); $toClass = ucfirst($singular); $this->_methods = array($toMethod => 'getObjects', $singular . 'Ids' => 'getObjectIds', $singular . 'Count' => 'getObjectCount', 'add' . $toClass => 'addObject', Mad_Support_Inflector::pluralize('delete' . $toClass) => 'deleteObjects', Mad_Support_Inflector::pluralize('clear' . $toClass) => 'clearObjects', Mad_Support_Inflector::pluralize('find' . $toClass) => 'findObjects'); }
/** * Check if request path matches any Routes to get the controller * * @return Mad_Controller_Base * @throws Mad_Controller_Exception */ public function recognize($request) { // pass a subset of the request environment // horde_routes_mapper for route matching $environ = array('REQUEST_METHOD' => $request->getMethod()); foreach (array('HTTP_HOST', 'SERVER_NAME', 'HTTPS') as $k) { $environ[$k] = $request->getServer($k); } $this->_mapper->environ = $environ; $path = $request->getPath(); if (substr($path, 0, 1) != '/') { $path = "/{$path}"; } $matchdata = $this->_mapper->match($path); if ($matchdata) { $hash = $this->formatMatchdata($matchdata); } if (empty($hash) || !isset($hash[':controller'])) { $msg = 'No routes in config/routes.php match the path: "' . $request->getPath() . '"'; throw new Mad_Controller_Exception($msg); return false; } $request->setPathParams($hash); // try to load the class $controllerName = $hash[':controller']; if (!class_exists($controllerName, false)) { $path = MAD_ROOT . '/app/controllers/' . $controllerName . '.php'; if (file_exists($path)) { require_once $path; } else { $msg = "The Controller \"{$controllerName}\" does not exist at " . $path; throw new Mad_Controller_Exception($msg); } } $controllerClassName = Mad_Support_Inflector::classify($controllerName); return new $controllerClassName(); }
/** * Generate helper class stubs */ private function _generateHelperStubs() { $name = !empty($this->_args) ? array_shift($this->_args) : null; if (!$name) { $this->_exit("You did not specify the name of the Helper to generate"); } // strip off controller if it's there $name = str_replace('Helper', '', $name); // CREATE FILES $class = Mad_Support_Inflector::classify($name); $helperName = $class . 'Helper'; // template files & common vars $this->_tpl->author = $this->_author; $this->_tpl->className = $class; $this->_tpl->helperName = $helperName; // create Helper stub $content = $this->_tpl->render('helper.php'); $this->_createFile(MAD_ROOT . "/app/helpers/{$helperName}.php", $content); }
/** * Get the join table for hasAndBelongsToMany associations. Default to * tablename1_tablename2 by alpha (briefcases_documents) * * Example: * <code> * class Binder * ... * $this->hasAndBelongsToMany('Documents'); * ... * // returns 'briefcases_documents' (by default) * </code> * * @return string */ public function getJoinTable() { if (!isset($this->_joinTable)) { $macro = $this->getMacro(); // join table was given in options if (!empty($this->_options['joinTable'])) { $this->_joinTable = $this->_options['joinTable']; // join table from through association } elseif (!empty($this->_options['through'])) { $class = Mad_Support_Inflector::classify($this->_options['through']); $model = new $class(); $this->_joinTable = $model->tableName(); // determine table name by convention from DO data } elseif ($macro == 'hasAndBelongsToMany') { $tbls = array($this->_model->tableName(), $this->getAssocTable()); sort($tbls); $this->_joinTable = implode('_', $tbls); // no join table } else { $this->_joinTable = null; } } return $this->_joinTable; }
/** * Load fixture(s) data into the database. * * <code> * <?php * ... * // single * $this->fixtures('briefcases'); * * // multiple * $this->fixtures(array('briefcases', 'md_metadata')); * * // 'only' for given test methods * $this->fixtures('briefcases', array('only' => array('testMethod1', 'testMethod2'))); * * // all test methods 'except' given * $this->fixtures('briefcases', array('except' => array('testMethod1', 'testMethod2'))); * ... * ?> * </code> * * @param string|array $ymlFiles * @param array $options */ public function fixtures($args) { $ymlFiles = func_get_args(); $last = end($ymlFiles); $options = is_array($last) ? array_pop($ymlFiles) : array(); // don't load fixtures for these methods if (isset($options['except'])) { if (in_array($this->getName(), $options['except'])) { return; } } // only load fixtures for these methods if (isset($options['only'])) { if (!in_array($this->getName(), $options['only'])) { return; } } // Add fixtures to the existing fixtures when called more than once if (empty($this->_fixtures)) { $this->_fixtures = new Mad_Test_Fixture_Collection($this->_conn, $ymlFiles); } else { $this->_fixtures->addFixture($ymlFiles); } // Build models from fixture records foreach ($this->_fixtures->getFixtures() as $fixture) { $name = $fixture->getYmlName(); if (isset($this->_fixtureClassNames[$name])) { $class = $this->_fixtureClassNames[$name]; } else { $table = $fixture->getTableName(); $class = Mad_Support_Inflector::classify($table); } // skip building model if class doesn't exist if (!Mad_Support_Base::modelExists($class)) { break; } $model = new $class(); $this->_records[$name] = array(); foreach ($fixture->getRecords() as $recordName => $attrs) { $this->_records[$name][$recordName] = $model->instantiate($attrs); } } // @deprecated - assign public properties based on fixture names foreach ($this->_fixtures->getRecords() as $recordName => $values) { if (isset($this->{$recordName})) { $this->{$recordName} = array_merge($this->{$recordName}, $values); } else { $this->{$recordName} = $values; } // make all values strings foreach ($this->{$recordName} as &$value) { $value = (string) $value; } } }
public function testClassify() { $this->assertEquals('Derek', Mad_Support_Inflector::classify('derek')); $this->assertEquals('DereksTest', Mad_Support_Inflector::classify('dereks_test')); // not as common $this->assertEquals('Derek', Mad_Support_Inflector::classify('Derek')); $this->assertEquals('Derek', Mad_Support_Inflector::classify('Dereks')); $this->assertEquals('DereksTest', Mad_Support_Inflector::classify('dereksTest')); $this->assertEquals('DereksTest', Mad_Support_Inflector::classify('DereksTest')); $this->assertEquals('Dereks_Test', Mad_Support_Inflector::classify('Dereks_Test')); }
/** * Initialize the default helpers for use in the views */ private function _initViewHelpers() { $controllerHelper = Mad_Support_Inflector::classify($this->_shortName . 'Helper'); $this->_view->addHelper(new $controllerHelper($this->_view)); }