public function test_should_generate_controller_and_auxiliary_files() { $files = array(AkConfig::getDir('controllers') . DS . 'credit_card_controller.php', AkConfig::getDir('helpers') . DS . 'credit_card_helper.php', AkConfig::getDir('test') . DS . 'functional' . DS . 'controllers' . DS . 'credit_card_controller_test.php', AkConfig::getDir('test') . DS . 'unit' . DS . 'helpers' . DS . 'credit_card_helper_test.php', AkConfig::getDir('views') . DS . 'credit_card' . DS . 'open.html.tpl', AkConfig::getDir('views') . DS . 'credit_card' . DS . 'debit.html.tpl', AkConfig::getDir('views') . DS . 'credit_card' . DS . 'credit.html.tpl', AkConfig::getDir('views') . DS . 'credit_card' . DS . 'close.html.tpl'); clearstatcache(); foreach ($files as $file) { file_exists($file) && unlink($file); $this->assertFalse(file_exists($file)); } AkFileSystem::file_put_contents(AkConfig::getDir('views') . DS . 'credit_card' . DS . 'credit.html.tpl', 'foo', array('base_path' => AK_FRAMEWORK_DIR)); clearstatcache(); $this->assertPattern('/collisions/', $this->runGeneratorCommand('controller CreditCard open debit credit close')); AkFileSystem::file_delete(AkConfig::getDir('views') . DS . 'credit_card' . DS . 'credit.html.tpl', array('base_path' => AK_FRAMEWORK_DIR)); clearstatcache(); foreach ($files as $file) { $this->assertFalse(file_exists($file)); } $this->assertPattern('/ files have been created/', $this->runGeneratorCommand('controller CreditCard open debit credit close')); clearstatcache(); foreach ($files as $file) { $this->assertTrue(file_exists($file)); if (!file_exists($file)) { AkDebug::trace($file); } @unlink($file); } }
public function _run_from_file($file_name, $all_in_one_test = true) { $multiple_expected_php = $multiple_sintags = ''; $tests = explode('===================================', file_get_contents(AkConfig::getDir('fixtures') . DS . $file_name)); foreach ($tests as $test) { list($sintags, $php) = explode('-----------------------------------', $test); $sintags = trim($sintags); $expected_php = trim($php); if (empty($sintags)) { return; } else { $multiple_sintags .= $sintags; $multiple_expected_php .= $expected_php; } $AkSintags = new AkSintagsParser(); $php = $AkSintags->parse($sintags); if ($php != $expected_php) { AkDebug::trace("GENERATED: \n" . $php); AkDebug::trace("EXPECTED: \n" . $expected_php); AkDebug::trace("SINTAGS: \n" . $sintags); } $this->assertEqual($php, $expected_php); } if ($all_in_one_test) { $AkSintags = new AkSintagsParser(); $php = $AkSintags->parse($multiple_sintags); if ($php != $multiple_expected_php) { AkDebug::trace("GENERATED: \n" . $php); AkDebug::trace("EXPECTED: \n" . $expected_php); AkDebug::trace("SINTAGS: \n" . $sintags); } $this->assertEqual($php, $multiple_expected_php); } }
public function index() { $this->base_dir = AK_BASE_DIR; $this->akelos_dir = AK_FRAMEWORK_DIR; $this->tasks_dir = AK_TASKS_DIR; $this->has_configuration = file_exists(AkConfig::getDir('config') . DS . 'config.php'); $this->has_routes = file_exists(AkConfig::getDir('config') . DS . 'routes.php'); $this->has_database = file_exists(AkConfig::getDir('config') . DS . 'database.yml'); $this->using_root_path = $this->Request->getPath() == '/'; $this->new_install = !$this->has_configuration || !$this->has_routes || $this->using_root_path; $this->environment = AK_ENVIRONMENT; $this->memcached_on = AkMemcache::isServerUp(); $this->constants = AkDebug::get_constants(); $this->langs = Ak::langs(); $this->database_settings = Ak::getSettings('database', false); $this->server_user = trim(AK_WIN ? `ECHO %USERNAME%` : `whoami`); $this->local_ips = AkConfig::getOption('local_ips', array('localhost', '127.0.0.1', '::1')); $paths = array(AK_APP_DIR . DS . 'locales'); $this->invalid_permissions = array(); foreach ($paths as $path) { if (is_dir($path) && !@file_put_contents($path . DS . '__test_file')) { $this->invalid_permissions[] = $path; } else { @unlink($path . DS . '__test_file'); } } }
public function log_memory($reset = false, $vervose = false) { if ($reset || empty($this->initial)) { $this->initial = memory_get_usage(); } $this->current = memory_get_usage(); $this->difference = $this->current - $this->initial; $this->difference && $vervose && AkDebug::trace($this->difference / 1048576 . ' MB increased'); return $this->difference; }
/** * Defines the column name for use with single table inheritance. Can be overridden in subclasses. */ public function setInheritanceColumn($column_name) { if (!$this->_ActiveRecord->hasColumn($column_name)) { trigger_error(Ak::t('Could not set "%column_name" as the inheritance column as this column is not available on the database.', array('%column_name' => $column_name)) . ' ' . AkDebug::getFileAndNumberTextForError(1), E_USER_NOTICE); return false; } elseif ($this->_ActiveRecord->getColumnType($column_name) != 'string') { trigger_error(Ak::t('Could not set %column_name as the inheritance column as this column type is "%column_type" instead of "string".', array('%column_name' => $column_name, '%column_type' => $this->_ActiveRecord->getColumnType($column_name))) . ' ' . AkDebug::getFileAndNumberTextForError(1), E_USER_NOTICE); return false; } else { $this->_ActiveRecord->_inheritanceColumn = $column_name; return true; } }
public function dispatchAppServer($context) { $_ENV = $_SERVER = $context['env']; @parse_str($_ENV['QUERY_STRING'], $_GET); $_GET['ak'] = $_ENV['PATH_INFO']; Ak::unsetStaticVar('AkRequestSingleton'); Ak::unsetStaticVar('AkRouterSingleton'); Ak::unsetStaticVar('AkUrlWriterSingleton'); AkConfig::setOption('Request.remote_ip', '127.0.0.1'); try { $time_start = microtime(true); AK_ENABLE_PROFILER && AkDebug::profile(__CLASS__ . '::' . __FUNCTION__ . '() call'); $this->Request = AkRequest::getInstance(); $this->Response = new AkResponse(); $path = ltrim(str_replace('..', '.', $context['env']['REQUEST_URI']), '/. '); if (empty($path) && file_exists(AK_PUBLIC_DIR . DS . 'index.html')) { $Controller = new AkActionController(); $Controller->Response = $this->Response; $Controller->renderText(file_get_contents(AK_PUBLIC_DIR . DS . 'index.html')); return $Controller->Response; } elseif (!empty($path) && file_exists(AK_PUBLIC_DIR . DS . $path)) { $Controller = new AkActionController(); $Controller->Response = $this->Response; $Controller->sendFile(AK_PUBLIC_DIR . DS . $path, array('stream' => false)); return $Controller->Response; } else { if ($this->Controller = $this->Request->recognize()) { $this->Controller->ak_time_start = $time_start; AK_ENABLE_PROFILER && AkDebug::profile('Request::recognize() completed'); $this->Controller->process($this->Request, $this->Response); } return $this->Response; } } catch (Exception $e) { if (isset($this->Controller) && method_exists($this->Controller, 'render_error')) { $this->Controller->render_error($e); } else { $ExceptionDispatcher = new AkExceptionDispatcher(); $ExceptionDispatcher->renderException($e); } } }
public function init($options = array()) { $default_options = array('servers' => array('localhost:11211'), 'lifeTime' => 0); $options = array_merge($default_options, $options); $this->_lifeTime = $options['lifeTime']; if (empty($options['servers'])) { trigger_error('Need to provide at least 1 server', E_USER_ERROR); return false; } $this->_memcache = new MemCachedClient(is_array($options['servers']) ? $options['servers'] : array($options['servers'])); $ping = $this->_memcache->get('ping'); if (!$ping) { if ($this->_memcache->errno == ERR_NO_SOCKET) { if (empty($options['silent_mode'])) { trigger_error("Could not connect to MemCache daemon. " . AkDebug::getFileAndNumberTextForError(1), E_USER_WARNING); } return false; } $this->_memcache->set('ping', 1); } return true; }
protected function _validateCalculationOptions($options = array()) { $invalid_options = array_diff(array_keys($options), $this->_calculation_options); if (!empty($invalid_options)) { trigger_error(Ak::t('%options are not valid calculation options.', array('%options' => join(', ', $invalid_options))) . AkDebug::getFileAndNumberTextForError(1), E_USER_ERROR); } }
/** * Finder methods must instantiate through this method to work with the single-table inheritance model and * eager loading associations. * That makes it possible to create objects of different types from the same table. */ public function &instantiate($record, $set_as_new = true, $call_after_instantiate = true) { $inheritance_column = $this->_ActiveRecord->getInheritanceColumn(); if (!empty($record[$inheritance_column])) { $inheritance_column = $record[$inheritance_column]; $inheritance_model_name = AkInflector::camelize($inheritance_column); @(require_once AkInflector::toModelFilename($inheritance_model_name)); if (!class_exists($inheritance_model_name)) { trigger_error($this->_ActiveRecord->t("The single-table inheritance mechanism failed to locate the subclass: '%class_name'. " . "This error is raised because the column '%column' is reserved for storing the class in case of inheritance. " . "Please rename this column if you didn't intend it to be used for storing the inheritance class " . "or overwrite #{self.to_s}.inheritance_column to use another column for that information.", array('%class_name' => $inheritance_model_name, '%column' => $this->_ActiveRecord->getInheritanceColumn())) . AkDebug::getFileAndNumberTextForError(1), E_USER_ERROR); } } $model_name = isset($inheritance_model_name) ? $inheritance_model_name : $this->_ActiveRecord->getModelName(); $object = new $model_name(array('init' => false)); $object->_newRecord = $set_as_new; $object->setConnection($this->_ActiveRecord->getConnection()); $object->beforeInstantiate($record); $object->init(array('attributes', $record)); if ($call_after_instantiate) { $object->afterInstantiate(); $object->notifyObservers('afterInstantiate'); } return $object; }
public function performActionWithFilters($method = '') { if ($this->beforeAction($method) !== false && !empty($this->_FilteredObject) && method_exists($this->_FilteredObject, 'hasPerformed') && !$this->_FilteredObject->hasPerformed()) { if (AK_ENABLE_PROFILER) { AkDebug::profile("Called {$method} before filters"); } $this->_FilteredObject->performActionWithoutFilters($method); if (AK_ENABLE_PROFILER) { AkDebug::profile("Performed {$method} action"); } $this->afterAction($method); if (AK_ENABLE_PROFILER) { AkDebug::profile("Called {$method} after filters"); } return true; } return false; }
public function &_getActAsInstance($class_name, $options) { if (!class_exists($class_name)) { if (substr($class_name, 0, 2) == 'Ak') { include_once AK_ACTIVE_RECORD_DIR . DS . 'behaviours' . DS . AkInflector::underscore(substr($class_name, 2)) . '.php'; } else { include_once AK_APP_PLUGINS_DIR . DS . AkInflector::underscore($class_name) . DS . 'lib' . DS . $class_name . '.php'; } } if (!class_exists($class_name)) { trigger_error(Ak::t('The class %class used for handling an "act_as %class" does not exist', array('%class' => $class_name)) . AkDebug::getFileAndNumberTextForError(1), E_USER_ERROR); $false = false; return $false; } else { $ActAsInstance = new $class_name($this, $options); return $ActAsInstance; } }
public function writeCache($config, $namespace, $environment = AK_ENVIRONMENT, $force = false) { if (!$force && !$this->_useWriteCache($environment)) { return false; } $key = $this->_getCacheKey($namespace, $environment); Ak::setStaticVar($key, $config); $var_export = var_export($config, true); $cache = <<<CACHE <?php /** * Auto-generated config cache from {$namespace} in environment {$environment} */ \$config = {$var_export}; return \$config; CACHE; $cache_file_name = $this->getCacheFileName($namespace, $environment); if (!AkFileSystem::file_put_contents($cache_file_name, $cache, array('base_path' => AkConfig::getCacheBasePath()))) { trigger_error(Ak::t('Could not create config cache file %file', array('%file' => $cache_file_name)) . ' ' . AkDebug::getFileAndNumberTextForError(1), E_USER_ERROR); return false; } else { return true; } }
public function debug($data = 'active_record_class', $_functions = 0) { if (!AK_DEBUG && !AK_DEV_MODE) { return; } $data = $data == 'active_record_class' ? clone $this->_Model : $data; if ($_functions != 0) { $sf = 1; } else { $sf = 0; } if (isset($data)) { if (is_array($data) || is_object($data)) { if (count($data)) { echo AK_CLI ? "/--\n" : "<ol>\n"; while (list($key, $value) = each($data)) { if ($key[0] == '_') { continue; } $type = gettype($value); if ($type == "array") { AK_CLI ? printf("\t* (%s) %s:\n", $type, $key) : printf("<li>(%s) <b>%s</b>:\n", $type, $key); ob_start(); AkDebug::debug($value, $sf); $lines = explode("\n", ob_get_clean() . "\n"); foreach ($lines as $line) { echo "\t" . $line . "\n"; } } elseif ($type == "object") { if (method_exists($value, 'hasColumn') && $value->hasColumn($key)) { $value->toString(true); AK_CLI ? printf("\t* (%s) %s:\n", $type, $key) : printf("<li>(%s) <b>%s</b>:\n", $type, $key); ob_start(); AkDebug::debug($value, $sf); $lines = explode("\n", ob_get_clean() . "\n"); foreach ($lines as $line) { echo "\t" . $line . "\n"; } } } elseif (stristr($type, "function")) { if ($sf) { AK_CLI ? printf("\t* (%s) %s:\n", $type, $key, $value) : printf("<li>(%s) <b>%s</b> </li>\n", $type, $key, $value); } } else { if (!$value) { $value = "(none)"; } AK_CLI ? printf("\t* (%s) %s = %s\n", $type, $key, $value) : printf("<li>(%s) <b>%s</b> = %s</li>\n", $type, $key, $value); } } echo AK_CLI ? "\n--/\n" : "</ol>fin.\n"; } else { echo "(empty)"; } } } }
public function handleDisplayMessage($error_level, $message, $parameters = array()) { if (AkConfig::getOption('logger.display_message', true)) { if (!empty($this->options['print'])) { list($file, $line, $method) = AkDebug::getLastFileAndLineAndMethod(false, 3); AkDebug::trace("<strong>[{$error_level}]</strong> - " . AkTextHelper::html_escape($message), $line, $file, $method, false); if (!empty($parameters)) { AkDebug::trace($parameters, $line, $file, $method); } } } }
/** * Validates that the specified attribute matches the length restrictions supplied. Only one option can be used at a time: * * class Person extends ActiveRecord * { * public function validate() * { * $this->validatesLengthOf('first_name', array('maximum'=>30)); * $this->validatesLengthOf('last_name', array('maximum'=>30,'message'=> "less than %d if you don't mind")); * $this->validatesLengthOf('last_name', array('within'=>array(7, 32))); * $this->validatesLengthOf('last_name', array('in'=>array(6, 20), 'too_long' => "pick a shorter name", 'too_short' => "pick a longer name")); * $this->validatesLengthOf('fav_bra_size', array('minimum'=>1, 'too_short'=>"please enter at least %d character")); * $this->validatesLengthOf('smurf_leader', array('is'=>4, 'message'=>"papa is spelled with %d characters... don't play me.")); * } * } * * NOTE: Be aware that $this->validatesLengthOf('field', array('is'=>5)); Will match a string containing 5 characters (Ie. "Spain"), an integer 5, and an array with 5 elements. You must supply additional checking to check for appropriate types. * * Configuration options: * <tt>minimum</tt> - The minimum size of the attribute * <tt>maximum</tt> - The maximum size of the attribute * <tt>is</tt> - The exact size of the attribute * <tt>within</tt> - A range specifying the minimum and maximum size of the attribute * <tt>in</tt> - A synonym(or alias) for :within * <tt>allow_null</tt> - Attribute may be null; skip validation. * * <tt>too_long</tt> - The error message if the attribute goes over the maximum (default "is" "is too long (max is %d characters)") * <tt>too_short</tt> - The error message if the attribute goes under the minimum (default "is" "is too short (min is %d characters)") * <tt>wrong_length</tt> - The error message if using the "is" method and the attribute is the wrong size (default "is" "is the wrong length (should be %d characters)") * <tt>message</tt> - The error message to use for a "minimum", "maximum", or "is" violation. An alias of the appropriate too_long/too_short/wrong_length message */ public function validatesLengthOf($attribute_names, $options = array()) { // Merge given options with defaults. $default_options = array('too_long' => $this->_Model->getDefaultErrorMessageFor('too_long'), 'too_short' => $this->_Model->getDefaultErrorMessageFor('too_short'), 'wrong_length' => $this->_Model->getDefaultErrorMessageFor('wrong_length'), 'allow_null' => false); $range_options = array(); foreach ($options as $k => $v) { if (in_array($k, array('minimum', 'maximum', 'is', 'in', 'within'))) { $range_options[$k] = $v; $option = $k; $option_value = $v; } } // Ensure that one and only one range option is specified. switch (count($range_options)) { case 0: trigger_error(Ak::t('Range unspecified. Specify the "within", "maximum", "minimum, or "is" option.') . AkDebug::getFileAndNumberTextForError(1), E_USER_ERROR); return false; break; case 1: $options = array_merge($default_options, $options); break; default: trigger_error(Ak::t('Too many range options specified. Choose only one.') . AkDebug::getFileAndNumberTextForError(1), E_USER_ERROR); return false; break; } switch ($option) { case 'within': case 'in': if (empty($option_value) || !is_array($option_value) || count($option_value) != 2 || !is_numeric($option_value[0]) || !is_numeric($option_value[1])) { trigger_error(Ak::t('%option must be a Range (array(min, max))', array('%option', $option)) . AkDebug::getFileAndNumberTextForError(1), E_USER_ERROR); return false; } $attribute_names = Ak::toArray($attribute_names); foreach ($attribute_names as $attribute_name) { $value = @$this->_Model->{$attribute_name}; if (!empty($option['allow_null']) && is_null($value) || Ak::size($value) < $option_value[0]) { $this->_Model->addError($attribute_name, sprintf($options['too_short'], $option_value[0])); } elseif (!empty($option['allow_null']) && is_null($value) || Ak::size($value) > $option_value[1]) { $this->_Model->addError($attribute_name, sprintf($options['too_long'], $option_value[1])); } } break; case 'is': case 'minimum': case 'maximum': if (empty($option_value) || !is_numeric($option_value) || $option_value <= 0) { trigger_error(Ak::t('%option must be a nonnegative Integer', array('%option', $option_value)) . AkDebug::getFileAndNumberTextForError(1), E_USER_ERROR); return false; } // Declare different validations per option. $validity_checks = array('is' => '==', 'minimum' => '>=', 'maximum' => '<='); $message_options = array('is' => 'wrong_length', 'minimum' => 'too_short', 'maximum' => 'too_long'); $message = sprintf(!empty($options['message']) ? $options['message'] : $options[$message_options[$option]], $option_value); $attribute_names = Ak::toArray($attribute_names); foreach ($attribute_names as $attribute_name) { $value = @$this->_Model->{$attribute_name}; if (!$options['allow_null'] && is_null($value) || eval('return !(' . Ak::size($value) . " {$validity_checks[$option]} {$option_value});")) { $this->_Model->addError($attribute_name, $message); } } break; default: break; } return true; }
public function process(&$Request, &$Response, $options = array()) { if (AK_ENABLE_PROFILER) { AkDebug::profile('AkActionController::process() start'); } $this->setRequestAndResponse($Request, $Response); if (AK_LOG_EVENTS) { $this->_logRequestParams($this->params); } if (AK_ENABLE_PROFILER) { AkDebug::profile('Got request paramenters'); } $actionExists = $this->_ensureActionExists(); if (!$actionExists) { $this->handleResponse(); return false; } AkConfig::getLocalesReady(); if ($this->_high_load_mode !== true) { if (!empty($this->_auto_instantiate_models)) { $this->instantiateIncludedModelClasses(); if (AK_ENABLE_PROFILER) { AkDebug::profile('Instantiated models'); } } if (!empty($this->_enable_plugins)) { $this->loadPlugins(); if (AK_ENABLE_PROFILER) { AkDebug::profile('Instantiated plugins'); } } } $this->_ensureProperProtocol(); $this->_lazy_loading_options = $options; //$this->init($options); // After filters //$this->isFilteringActive() && $this->afterFilter('_handleFlashAttribute'); $this->_loadActionView(); if (isset($this->api)) { $this->aroundFilter(new AkActionWebService($this)); } $this->_identifyRequest(); if ($this->isFilteringActive()) { $this->performActionWithFilters($this->_action_name); } else { $this->performActionWithoutFilters($this->_action_name); } return $this->handleResponse(); }
public function Test_get_this_object_attributes() { $expected_attributes = array('child_var' => null, 'child_var_null' => null, 'child_var_string' => 'abc', 'child_var_int' => 123); $resulting_attributes = AkDebug::get_this_object_attributes($this->AkTestingObjectInspectionChildInstance); $this->assertEqual($expected_attributes, $resulting_attributes); }
public function failTransaction() { if (AK_DEBUG && !empty($this->connection->debug) && function_exists('ak_backtrace')) { AkDebug::trace(ak_backtrace(), null, null, null, false); } return $this->connection->FailTrans(); }
public function addCombinedAttributeConfiguration($attribute) { $args = is_array($attribute) ? $attribute : func_get_args(); $columns = array_slice($args, 2); $invalid_columns = array(); foreach ($columns as $colum) { if (!$this->_ActiveRecord->hasAttribute($colum)) { $invalid_columns[] = $colum; } } if (!empty($invalid_columns)) { trigger_error(Ak::t('There was an error while setting the composed field "%field_name", the following mapping column/s "%columns" do not exist', array('%field_name' => $args[0], '%columns' => join(', ', $invalid_columns))) . AkDebug::getFileAndNumberTextForError(1), E_USER_ERROR); } else { $attribute = array_shift($args); $this->_ActiveRecord->_combinedAttributes[$attribute] = $args; $this->composeCombinedAttribute($attribute); } }
/** * @deprecated * @uses AkDebug::profile */ static function profile($message = '') { Ak::deprecateMethod(__METHOD__, 'AkConfig::profile()'); return AkDebug::profile($message); }