public function testSingularize() { $result = Inflector::singularize('presidents'); $this->assertEquals('president', $result); $result = Inflector::singularize('tomatoes'); $this->assertEquals('tomato', $result); $result = Inflector::singularize('equipment'); $this->assertEquals('equipment', $result); }
public function generate($name, $colDefs = array()) { if (empty($colDefs)) { $tableName = Inflector::tableize($name); $datasource = ConnectionManager::getDataSource(); $attributes = $datasource->generateAttributeMapFromTable($tableName); } else { $columns = Migration::parseColumnDefinitions($colDefs); $names = array_map(function ($col) { return $col['name']; }, $columns); $types = array_map(function ($col) { return $col['type']; }, $columns); $attributes = array_combine($names, $types); } $templates = array('add', 'edit', 'index', 'view'); $vars = array('attributes' => $attributes, 'friendlyModelName' => Inflector::humanize(Inflector::classify($name)), 'modelVarName' => Inflector::lowerCamelize(Inflector::classify(Inflector::singularize($name))), 'pluralModelVarName' => Inflector::lowerCamelize(Inflector::pluralize($name)), 'controller' => Inflector::tableize($name)); foreach ($templates as $template) { $data = $this->_renderTemplate("views/{$template}", $vars, true); $folder = Inflector::tableize($name); $this->_writeFile("/views/{$folder}/{$template}.html.tpl", $data); } }
public function generate($name) { $vars = array('controller_name' => Inflector::tableize($name), 'controller_class' => Inflector::camelize(Inflector::tableize($name) . '_controller'), 'model_name' => Inflector::underscore(Inflector::singularize($name)), 'model_class' => Inflector::classify($name), 'model_var_name' => Inflector::lowerCamelize(Inflector::classify($name)), 'model_var_plural_name' => Inflector::lowerCamelize(Inflector::tableize($name)), 'friendly_model_name' => Inflector::humanize(Inflector::singularize($name)), 'friendly_controller_name' => Inflector::humanize(Inflector::tableize($name)), 'package_name' => 'App', 'year' => date('Y')); $data = $this->_renderTemplate('controller', $vars); $this->_writeFile('/controllers/' . $vars['controller_class'] . '.php', $data); }
/** * Creates restful routes for controller * * @param string $name controller name * @param array $options * @return void */ public static function resource($name, $options = array()) { $defaults = array('namespace' => false, 'only' => array(), 'except' => array()); $options = array_merge($defaults, $options); $whitelist = empty($options['only']) ? array('index', 'view', 'add', 'edit', 'delete') : (array) $options['only']; if (!empty($options['except'])) { $whitelist = array_diff($whitelist, (array) $options['except']); } $whitelist = array_flip($whitelist); if (strpos($name, '.') !== false) { $resource = array(); if ($options['namespace'] !== false) { $resource[] = $options['namespace']; } $controllers = explode('.', $name); $lastController = array_pop($controllers); foreach ($controllers as $key => $value) { $resource[] = $value; $resource[] = ':' . Inflector::singularize($value) . '_id'; } $resource[] = $lastController; $resource = implode('/', $resource); $controller = $options['namespace'] === false ? $lastController : "{$options['namespace']}_{$lastController}"; } else { $resource = $options['namespace'] === false ? $name : "{$options['namespace']}/{$name}"; $controller = $options['namespace'] === false ? $name : "{$options['namespace']}_{$name}"; } $map = array('add' => array(array('method' => 'GET', 'template' => "/{$resource}/new"), array('method' => 'POST', 'template' => "/{$resource}")), 'index' => array(array('method' => 'GET', 'template' => "/{$resource}")), 'edit' => array(array('method' => 'GET', 'template' => "/{$resource}/:id/edit"), array('method' => 'PUT', 'template' => "/{$resource}/:id")), 'delete' => array(array('method' => 'DELETE', 'template' => "/{$resource}/:id")), 'view' => array(array('method' => 'GET', 'template' => "/{$resource}/:id"))); foreach ($map as $action => $specs) { if (isset($whitelist[$action])) { foreach ($specs as $spec) { static::on($spec['method'], $spec['template'], array('controller' => $controller, 'action' => $action, 'namespace' => $options['namespace'])); } } } }
private function _handleThrough($through, $model, $scope) { $model_var = Inflector::singularize(Inflector::tableize($model)); $assoc = array(); foreach ($through as $item) { $assoc[] = $model::find($item->{$model_var . '_id'}); } return $assoc; }
protected function _generateViews($name) { $tableName = Inflector::tableize($name); $datasource = ConnectionManager::getDataSource(); $attributes = $datasource->generateAttributeMapFromTable($tableName); $templates = array('add', 'edit', 'index', 'view'); $vars = array( 'attributes' => $attributes, 'friendlyModelName' => Inflector::humanize(Inflector::classify($name)), 'modelVarName' => Inflector::lowerCamelize(Inflector::classify(Inflector::singularize($name))), 'pluralModelVarName' => Inflector::lowerCamelize(Inflector::pluralize($name)), 'controller' => Inflector::tableize($name) ); foreach ($templates as $template) { $data = $this->_renderTemplate("views/{$template}", $vars, true); $folder = Inflector::tableize($name); $this->_writeFile("/views/{$folder}/{$template}.html.tpl", $data); } }
/** * Creates restful routes for controller * * @param string $name controller name * @param array $options * @return void */ public static function resource($name, $options = array()) { $defaults = array('namespace' => false, 'only' => array(), 'except' => array()); $options = array_merge($defaults, $options); $whitelist = empty($options['only']) ? array('index', 'view', 'add', 'edit', 'delete') : $options['only']; if (!empty($options['except'])) { $whitelist = array_diff($whitelist, (array)$options['except']); } $whitelist = array_flip($whitelist); if (strpos($name, '.') !== false) { $resource = array(); if ($options['namespace'] !== false) { $resource[] = $options['namespace']; } $controllers = explode('.', $name); $lastController = array_pop($controllers); foreach ($controllers as $key => $value) { $resource[] = $value; $resource[] = ':' . Inflector::singularize($value) . '_id'; } $resource[] = $lastController; $resource = implode('/', $resource); $controller = ($options['namespace'] === false) ? $lastController : "{$options['namespace']}_{$lastController}"; } else { $resource = ($options['namespace'] === false) ? $name : "{$options['namespace']}/{$name}"; $controller = ($options['namespace'] === false) ? $name : "{$options['namespace']}_{$name}"; } if (isset($whitelist['add'])) { self::connect("/{$resource}", array( 'controller' => $controller, 'action' => 'add', 'namespace' => $options['namespace'] ), array( 'via' => 'POST')); self::connect("/{$resource}/new", array( 'controller' => $controller, 'action' => 'add', 'namespace' => $options['namespace'] ), array('via' => 'GET')); } if (isset($whitelist['index'])) { self::connect("/{$resource}", array( 'controller' => $controller, 'action' => 'index', 'namespace' => $options['namespace'] )); } if (isset($whitelist['edit'])) { self::connect("/{$resource}/:id/edit", array( 'controller' => $controller, 'action' => 'edit', 'namespace' => $options['namespace'] )); self::connect("/{$resource}/:id", array( 'controller' => $controller, 'action' => 'edit', 'namespace' => $options['namespace'] ), array('via' => 'PUT')); } if (isset($whitelist['delete'])) { self::connect("/{$resource}/:id", array( 'controller' => $controller, 'action' => 'delete', 'namespace' => $options['namespace'] ), array('via' => 'DELETE')); } if (isset($whitelist['view'])) { self::connect("/{$resource}/:id", array( 'controller' => $controller, 'action' => 'view', 'namespace' => $options['namespace'] )); } }