/**
  * undocumented function
  *
  * @param string $field 
  * @param string $errorMessage 
  * @return void
  */
 public function add($field, $errorMessage = null)
 {
     if ($errorMessage === null) {
         $errorMessage = Inflector::humanize($field) . ' is invalid';
     }
     $this->_errors[$field] = $errorMessage;
 }
Exemple #2
0
	/**
	 * Creates a new \mongo_model\embedded\Many collection from MogoDB document data
	 *
	 * @param Base $parent  document who owns the collection
	 * @param string $name  name/class of embedded documents
	 * @param array $data  MongoDB data
	 * @return Many
	 */
	public static function fromMongoData(Base $parent, $name, $data) {
		$class = Inflector::classify($name);

		$objects = array_map(function($objectData) use ($class) {
			return $class::fromMongoData($objectData);
		}, $data);

		return new Many($parent, $objects);
	}
Exemple #3
0
	/**
	 * Returns the \MongoCollection for the model
	 *
	 * @return \MongoCollection
	 */
	public static function collection() {
		if (static::$_collection === null) {
			$collectionName = Inflector::tableize(get_called_class());
			$db = ConnectionManager::getDataSource(static::$_dataSourceName);
			$collection = $db->selectCollection($collectionName);
			static::$_collection = &$collection;
		}

		return static::$_collection;
	}
Exemple #4
0
 protected static function _scan()
 {
     $migrations = array();
     $files = glob(ROX_APP_PATH . '/config/migrations/*.php');
     foreach ($files as $file) {
         if (preg_match('/(?<version>[0-9]+)_(?<name>.*).php/', $file, $matches) == 1) {
             $migrations[$matches['version']] = array('file' => $file, 'name' => $matches['name'], 'class' => Inflector::camelize($matches['name']), 'version' => $matches['version']);
         }
     }
     ksort($migrations);
     return $migrations;
 }
Exemple #5
0
	public function __call($method, $args) {
		if (strpos($method, 'set') === 0) {
			$key = Inflector::underscore(substr($method, 3));
			return $this->setReference($key, $args[0]);
		}

		$key = Inflector::underscore($method);
		if (in_array($key, static::$_references)) {			
			return $this->getReference($key);
		}

		throw new Exception('Undefined method ' . get_called_class() . '::' . $method . '()');
	}
Exemple #6
0
 /**
  * Loads a helper
  *
  * @param string $name
  */
 protected static function _loadHelper($name)
 {
     $name = Inflector::camelize($name);
     $file = ROX_APP_PATH . "/helpers/{$name}Helper.php";
     if (file_exists($file)) {
         require_once $file;
         return $name . 'Helper';
     }
     $file = ROX_FRAMEWORK_PATH . "/template/helper/{$name}.php";
     if (file_exists($file)) {
         require_once $file;
         return '\\rox\\template\\helper\\' . $name;
     }
     throw new Exception("Helper '{$name}' not found");
 }
Exemple #7
0
 public function generate($name, $colDefs = array())
 {
     if (empty($colDefs)) {
         throw new Exception('Scaffold generator requires a list of columns/attributes');
     }
     $name = Inflector::pluralize($name);
     $migrationGen = new Migration($this->command);
     $migrationGen->generate("create_{$name}", $colDefs);
     $modelGen = new Model($this->command);
     $modelGen->generate($name);
     $controllerGen = new Controller($this->command);
     $controllerGen->generate($name);
     $viewsGen = new Views($this->command);
     $viewsGen->generate($name, $colDefs);
 }
Exemple #8
0
 public function generate($name, $colDefs = array())
 {
     $name = Inflector::underscore($name);
     $version = gmdate('YmdHis');
     foreach (glob(ROX_APP_PATH . '/config/migrations/*.php') as $file) {
         if (preg_match("/([0-9]+)_{$name}.php/", $file) == 1) {
             throw new Exception("A migration named {$name} already exists");
         }
     }
     $type = static::inferMigrationType($name);
     $class = Inflector::camelize($name);
     $table = static::inferTableName($name);
     $columns = static::parseColumnDefinitions($colDefs);
     $indexes = static::extractIndexes($columns);
     $vars = compact('type', 'class', 'table', 'columns', 'indexes');
     $data = $this->_renderTemplate('migration', $vars, true);
     $this->_writeFile("/config/migrations/{$version}_{$name}.php", $data);
 }
Exemple #9
0
 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);
     }
 }
Exemple #10
0
 /**
  * Mailer::send()
  * 
  * @param string $mailerAndEmail
  * @param ...
  * @return mixed
  */
 public static function send($mailerAndEmail)
 {
     if (strpos($mailerAndEmail, '.') == false) {
         throw new Exception('mailer and email should be separated by a period.');
     }
     list($mailer, $email) = explode('.', $mailerAndEmail);
     $mailerClass = Inflector::camelize($mailer . '_mailer');
     $emailMethod = Inflector::lowerCamelize($email);
     $args = array_slice(func_get_args(), 1);
     $mailerInstance = new $mailerClass();
     $mailerInstance->params = array('mailer' => $mailer, 'mailer_class' => $mailerClass, 'email' => $email, 'email_method' => $emailMethod);
     call_user_func_array(array($mailerInstance, $emailMethod), $args);
     return $mailerInstance->_send();
 }
 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;
 }
Exemple #12
0
 /**
  * Renders the current action
  */
 public function render()
 {
     $this->set('rox_page_title', $this->pageTitle);
     foreach ($this->helpers as $helper) {
         $helperName = Inflector::lowerCamelize($helper);
         $this->set($helperName, Rox::getHelper($helper));
     }
     $viewPath = $this->params['controller'];
     if (!empty($this->params['namespace'])) {
         $simpleControllerName = substr($this->params['controller'], strlen($this->params['namespace']) + 1);
         $viewPath = $this->params['namespace'] . '/' . $simpleControllerName;
     }
     $viewName = $this->params['action'];
     $view = new \rox\template\View($this->_viewVars);
     $view->response = $this->response;
     $view->params = $this->params;
     $this->response->body = $view->render($viewPath, $viewName, $this->layout);
 }
Exemple #13
0
	/**
	 * Returns the controller name for a given ActiveModel instance.
	 *
	 * @param ActiveModel $object 
	 * @return string
	 */
	protected static function _controllerNameFromModel(ActiveModel $object) {
		static $results = array();
		$class = get_class($object);
		if (!isset($results[$class])) {
			$results[$class] = Inflector::underscore(Inflector::pluralize($class));
		}
		return $results[$class];
	}
Exemple #14
0
	/**
	 * undocumented function
	 *
	 * @return array
	 */
	protected static function _normalizeAssociations() {
		$associations = array('belongs_to' => array(), 'has_many' => array(), 'has_one' => array());
		$rels = array('belongs_to' => '_belongsTo', 'has_many' => '_hasMany', 'has_one' => '_hasOne');
		foreach ($rels as $type => $property) {
			foreach (static::$$property as $name => $options) {
				if (is_int($name) && is_string($options)) {
					$name = $options;
					$options = array();
				}

				$keyClass = ($type == 'belongs_to') ? $name : get_called_class();
				$defaults = array(
					'class' => Inflector::classify($name),
					'key' => Inflector::underscore($keyClass) . '_id'
				);

				$options += $defaults;
				$associations[$type][$name] = $options;
			}
		}

		return $associations;
	}
Exemple #15
0
	protected function _generateMigration($name) {
		$name = Inflector::underscore($name);

		foreach (glob(ROX_APP_PATH . '/config/migrations/*.php') as $file) {
			if (preg_match("/([0-9]+)_{$name}.php/", $file) == 1) {
				throw new Exception("A migration named {$name} already exists");
			}
		}

		$version = gmdate('YmdHis');

		$data = $this->_renderTemplate('migration', array(
			'class_name' => Inflector::camelize($name),
			'year' => date('Y')
		));

		$this->_writeFile("/config/migrations/{$version}_{$name}.php", $data);
	}
Exemple #16
0
 public function setModel($model)
 {
     $validModel = is_string($model) || is_object($model) && $model instanceof ActiveModel;
     if (!$validModel) {
         throw new Exception('Model should be string or a \\rox\\ActiveModel');
     }
     $modelName = Inflector::underscore(is_object($model) ? get_class($model) : $model);
     $this->_currentModel = $modelName;
     if (is_object($model)) {
         $this->_data = array_merge($this->_data, array($modelName => $model->getData()));
         $this->_validationErrors[$modelName] = $model->getValidationErrors();
     }
 }
 public function testClassify()
 {
     $result = Inflector::classify('user_accounts');
     $this->assertEquals('UserAccount', $result);
 }
Exemple #18
0
 public static function parseUrl($url, Request $request = null)
 {
     if ($params = self::_parseUrl($url, $request)) {
         $defaults = array('action' => 'index', 'extension' => 'html', 'namespace' => false, 'args' => array());
         $params += $defaults;
         $params['action_method'] = Inflector::lowerCamelize($params['action']) . 'Action';
         $params['controller_class'] = Inflector::camelize($params['controller']) . 'Controller';
         return $params;
     }
     return $params;
 }
Exemple #19
0
/**
 * RoxPHP
 *
 * Copyright (C) 2008 - 2011 Ramon Torres
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright Copyright (c) 2008 - 2011 Ramon Torres
 * @package Rox
 * @license The MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
namespace rox\console;

use rox\Inflector;
use rox\Exception;
require_once dirname(dirname(dirname(__DIR__))) . '/app/config/bootstrap.php';
if ($argc == 1) {
    echo 'usage: rox <command> ...';
    exit(1);
}
try {
    $class = '\\rox\\console\\command\\' . Inflector::camelize($argv[1]);
    $command = new $class();
    $command->header();
    $command->run($argc, $argv);
    exit(0);
} catch (Exception $e) {
    echo sprintf("E: %s (%d)", $e->getMessage(), $e->getCode());
    exit(1);
}
Exemple #20
0
 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);
 }
Exemple #21
0
 public function generate($name)
 {
     $vars = array('class_name' => Inflector::classify($name), 'friendly_model_name' => Inflector::humanize(Inflector::classify($name)), 'package_name' => 'App', 'year' => date('Y'));
     $data = $this->_renderTemplate('model', $vars);
     $this->_writeFile('/models/' . Inflector::classify($name) . '.php', $data);
 }