For example, given the following:
class Bar extends \lithium\core\Object {
protected $_autoConfig = array('foo');
protected $_foo;
}
$instance = new Bar(array('foo' => 'value'));
The $_foo property of $instance would automatically be set to 'value'. If $_foo was
an array, $_autoConfig could be set to array('foo' => 'merge'), and the constructor value
of 'foo' would be merged with the default value of $_foo and assigned to it.
/** * Initializes default rules to use. * * @return void */ protected function _init() { parent::_init(); $this->_rules += array( 'allowAll' => function() { return true; }, 'denyAll' => function() { return false; }, 'allowAnyUser' => function($user) { return $user ? true : false; }, 'allowIp' => function($user, $request, $options) { $options += array('ip' => false); if (is_string($options['ip']) && strpos($options['ip'], '/') === 0) { return (boolean) preg_match($options['ip'], $request->env('REMOTE_ADDR')); } if (is_array($options['ip'])) { return in_array($request->env('REMOTE_ADDR'), $options['ip']); } return $request->env('REMOTE_ADDR') == $options['ip']; } ); }
protected function _init() { parent::_init(); if ($this->_config['autoConnect']) { $this->connect(); } }
protected function _init() { parent::_init(); unset($this->_config['type']); foreach ($this->_config as $key => $val) { if (method_exists($this, $key) && $val !== null) { $this->_config[$key] = is_array($this->_config[$key]) ? array() : null; $this->{$key}($val); } } if ($list = $this->_config['whitelist']) { $this->_config['whitelist'] = array_combine($list, $list); } if ($this->_config['with']) { $this->_associate($this->_config['with']); } $joins = $this->_config['joins']; $this->_config['joins'] = array(); foreach ($joins as $i => $join) { $this->join($i, $join); } if ($this->_entity && !$this->_config['model']) { $this->model($this->_entity->model()); } unset($this->_config['entity'], $this->_config['init'], $this->_config['with']); }
protected function _init() { parent::_init(); $this->_methods += array('GET' => array('view' => 'id', 'index' => null), 'POST' => array('edit' => 'id', 'add' => null), 'PUT' => array('edit' => 'id'), 'PATCH' => array('edit' => 'id'), 'DELETE' => array('delete' => 'id')); $this->_classes += array('entity' => 'lithium\\data\\Entity', 'response' => 'lithium\\action\\Response', 'resources' => 'li3_resources\\net\\http\\Resources', 'responder' => 'li3_resources\\action\\resource\\Responder'); $this->_responder = $this->_responder ?: $this->_instance('responder'); }
/** * Perform initialization. * * @return void */ protected function _init() { Object::_init(); $type = isset($this->_config['type']) ? $this->_config['type'] : null; if ($type === 'text') { $h = function ($data) { return $data; }; } else { $encoding = 'UTF-8'; if ($this->_message) { $encoding =& $this->_message->charset; } $h = function ($data) use(&$encoding) { return htmlspecialchars((string) $data, ENT_QUOTES, $encoding); }; } $this->outputFilters += compact('h') + $this->_config['outputFilters']; foreach (array('loader', 'renderer') as $key) { if (is_object($this->_config[$key])) { $this->{'_' . $key} = $this->_config[$key]; continue; } $class = $this->_config[$key]; $config = array('view' => $this) + $this->_config; $path = 'adapter.template.mail'; $instance = Libraries::instance($path, $class, $config); $this->{'_' . $key} = $instance; } }
/** * Initializer function called by the constructor unless the constructor * * @see lithium\core\Object::_init() * @throws ConfigException */ protected function _init() { parent::_init(); if (!$this->_connection) { throw new ConfigException("The `'connection'` option must be set."); } }
/** * Initialization of the cookie adapter. * * @return void */ protected function _init() { parent::_init(); if (!$this->_config['name']) { $this->_config['name'] = Inflector::slug(basename(LITHIUM_APP_PATH)) . 'cookie'; } }
protected function _init() { parent::_init(); $class = Libraries::locate('socket.util', $this->_classes['socket']); if (is_string($class)) { $this->_connection = new $class($this->_config); } }
protected function _init() { parent::_init(); $this->_classes += array('router' => 'lithium\\net\\http\\Router', 'resources' => 'li3_resources\\action\\Resources'); $classes = $this->_classes; $this->_fields += array('$links.self' => function ($entity) use($classes) { }); }
/** * Connect to FTP server on initialization */ protected function _init() { parent::_init(); if ($this->_config['url'] && substr($this->_config['url'], -1) === '/') { $this->_config['url'] = rtrim($this->_config['url'], '/'); } $this->_connect(); }
/** * Imports local string definitions into rendering context. * * @return void */ protected function _init() { parent::_init(); if (!$this->_context) { return; } $this->_context->strings($this->_strings); if ($this->_config['handlers']) { $this->_context->handlers($this->_config['handlers']); } }
/** * undocumented function * * @return void */ protected function _init() { parent::_init(); if (empty($this->_file)) { $this->_file = LITHIUM_APP_PATH . '/resources/oauth/storage/oauth.json'; return; } if ($this->_file[0] !== '/') { $this->_file = LITHIUM_APP_PATH . '/resources/oauth/' . $this->_file; } }
/** * Sets the default output handlers for string template inputs. * * Please note: skips lithium\template\view\Renderer::_init() * to skip setting handlers. * * @return void */ protected function _init() { Object::_init(); $classes =& $this->_classes; $message =& $this->_message; if (!$this->_request && $message) { $media = $classes['media']; $this->_request = $media::invokeMethod('_request', array($message)); } $request =& $this->_request; $context =& $this->_context; $h = $this->_view ? $this->_view->outputFilters['h'] : null; $this->_handlers += array('url' => function ($url, $ref, array $options = array()) use($classes, &$request, $h) { $router = $classes['router']; $options += array('absolute' => true); $url = $router::match($url ?: '', $request, $options); return $h ? str_replace('&', '&', $h($url)) : $url; }, 'path' => function ($path, $ref, array $options = array()) use($classes, &$request, &$message) { $embed = isset($options['embed']) && $options['embed']; unset($options['embed']); if ($embed) { return 'cid:' . $message->embed($path, $options); } else { $type = 'generic'; if (is_array($ref) && $ref[0] && $ref[1]) { list($helper, $methodRef) = $ref; list($class, $method) = explode('::', $methodRef); $type = $helper->contentMap[$method]; } $httpMedia = $classes['http_media']; $base = $request ? $request->env('base') : ''; $options += compact('base'); $path = $httpMedia::asset($path, $type, $options); if ($path[0] === '/') { $host = ''; if ($request) { $https = $request->env('HTTPS') ? 's' : ''; $host .= "http{$https}://"; $host .= $request->env('HTTP_HOST'); } $path = $host . $path; } return $path; } }, 'options' => '_attributes', 'title' => 'escape', 'scripts' => function ($scripts) use(&$context) { return "\n\t" . join("\n\t", $context['scripts']) . "\n"; }, 'styles' => function ($styles) use(&$context) { return "\n\t" . join("\n\t", $context['styles']) . "\n"; }, 'head' => function ($head) use(&$context) { return "\n\t" . join("\n\t", $context['head']) . "\n"; }); unset($this->_config['view']); }
protected function _init() { parent::_init(); $model = $this->_config['model']; $behavior = $this; $this->_config = static::_config($model, $behavior, $this->_config, static::$_defaults); static::_filters($model, $behavior); static::_finders($model, $behavior); if ($methods = static::_methods($model, $behavior)) { $model::instanceMethods($methods); } }
/** * Initializer. Populates the `response` property with a new instance of the `Response` * class passing it configuration and assigns the values from named parameters of the * request (if applicable) to properties of the command. * * @return void */ protected function _init() { parent::_init(); $this->request = $this->_config['request']; $this->response = new $this->_classes['response']($this->_config['response']); if (!empty($this->request->params)) { $params = (array) array_diff_key($this->request->params, array('command' => null, 'action' => null, 'args' => null)); foreach ($params as $key => $param) { $this->{$key} = $param; } } }
/** * Initialize request object * * @return void */ protected function _init() { $this->_env += (array) $_SERVER + (array) $_ENV; $this->_env['working'] = getcwd() ?: null; $argv = (array) $this->env('argv'); $this->_env['script'] = array_shift($argv); $this->argv += $argv + (array) $this->_config['args']; $this->input = $this->_config['input']; if (!is_resource($this->_config['input'])) { $this->input = fopen('php://stdin', 'r'); } parent::_init(); }
protected function _init() { parent::_init(); foreach ($this->_fields as $key => $type) { if (is_string($type)) { $this->_fields[$key] = compact('type'); continue; } if (isset($this->_fields[$key][0]) && !isset($this->_fields[$key]['type'])) { $this->_fields[$key]['type'] = $this->_fields[$key][0]; unset($this->_fields[$key][0]); } } }
/** * Initializer. Populates the `response` property with a new instance of the `Response` * class passing it configuration and assigns the values from named parameters of the * request (if applicable) to properties of the command. * * @return void */ protected function _init() { parent::_init(); $this->request = $this->_config['request']; $resp = $this->_config['response']; $this->response = is_object($resp) ? $resp : $this->_instance('response', $resp); if (!is_object($this->request) || !$this->request->params) { return; } $default = array('command' => null, 'action' => null, 'args' => null); $params = array_diff_key((array) $this->request->params, $default); foreach ($params as $key => $param) { $this->{$key} = $param; } }
protected function _init() { parent::_init(); foreach (array('loader', 'renderer') as $key) { if (is_object($this->_config[$key])) { $this->{'_' . $key} = $this->_config[$key]; continue; } if (!($class = Libraries::locate('adapter.template.view', $this->_config[$key]))) { throw new RuntimeException("Template adapter {$this->_config[$key]} not found"); } $this->{'_' . $key} = new $class(array('view' => $this) + $this->_config); } $h = function ($data) use(&$h) { return is_array($data) ? array_map($h, $data) : htmlspecialchars((string) $data); }; $this->outputFilters += compact('h') + $this->_config['outputFilters']; }
protected function _init() { parent::_init(); $config = $this->_config; $singularName = $config['name']; if ($config['type'] == 'hasMany') { $singularName = Inflector::singularize($config['name']); } if (!$config['to']) { $assoc = preg_replace("/\\w+\$/", "", $config['from']) . $singularName; $config['to'] = class_exists($assoc) ? $assoc : Libraries::locate('models', $assoc); } if (!$config['fieldName']) { $config['fieldName'] = lcfirst($config['name']); } $config['keys'] = $this->_keys($config['keys'], $config); $this->_config = $config; }
/** * Initializes default rules to use. */ protected function _init() { parent::_init(); $this->_rules += ['allowAll' => ['rule' => function () { return true; }], 'denyAll' => ['rule' => function () { return false; }], 'allowAnyUser' => ['message' => 'You must be logged in.', 'rule' => function ($user) { return $user ? true : false; }], 'allowIp' => ['message' => 'Your IP is not allowed to access this area.', 'rule' => function ($user, $request, $options) { $options += ['ip' => false]; if (is_string($options['ip']) && strpos($options['ip'], '/') === 0) { return (bool) preg_match($options['ip'], $request->env('REMOTE_ADDR')); } if (is_array($options['ip'])) { return in_array($request->env('REMOTE_ADDR'), $options['ip']); } return $request->env('REMOTE_ADDR') === $options['ip']; }]]; }
/** * Initialization of the session. * Sets the session save handlers to this adapters' corresponding methods. * * @return void */ protected function _init() { parent::_init(); if (!$this->_config['model']) { $message = "A valid model is required to use the Model session adapter."; static::log($message, \Zend_Log::CRIT); throw new ConfigException($message); } foreach ($this->_defaults as $key => $config) { if (isset($this->_config[$key])) { if (ini_set("session.{$key}", $this->_config[$key]) === false) { static::log("Failed to initialize the session", \Zend_Log::CRIT); throw new ConfigException("Could not initialize the session."); } } } $this->_model = Libraries::locate('models', $this->_config['model']); session_set_save_handler(array(&$this, '_open'), array(&$this, '_close'), array(&$this, '_read'), array(&$this, '_write'), array(&$this, '_destroy'), array(&$this, '_gc')); register_shutdown_function('session_write_close'); $this->_startup(); }
/** * Initializer. Eager loads the first result. * * @return void */ protected function _init() { parent::_init(); $this->next(); }
/** * Initializes values configured in the constructor. * * @return void */ protected function _init() { parent::_init(); foreach ($this->_fields as $key => $val) { if (is_int($key)) { unset($this->_fields[$key]); $this->_fields[$val] = $val; } } $this->_model = Libraries::locate('models', $this->_model); }
/** * Populates the `$response` property with a new instance of the `Response` class passing it * configuration, and sets some rendering options, depending on the incoming request. * * @return void */ protected function _init() { parent::_init(); $this->request = $this->request ?: $this->_config['request']; $this->response = $this->_instance('response', $this->_config['response']); if (!$this->request || $this->_render['type']) { return; } if ($this->_render['negotiate']) { $this->_render['type'] = $this->request->accepts(); return; } $this->_render['type'] = $this->request->get('params:type') ?: 'html'; }
protected function _init() { parent::_init(); if (!$this->_config['continue']) { $this->_params += array('action' => 'index'); } if (!$this->_config['pattern']) { $this->compile(); } if ($isKey = isset($this->_keys['controller']) || isset($this->_params['controller'])) { $this->_persist = $this->_persist ?: array('controller'); } }
/** * Perform initialization of the View. * * @return void */ protected function _init() { parent::_init(); $encoding = 'UTF-8'; if ($this->_response) { $encoding =& $this->_response->encoding; } $h = function($data) use (&$encoding) { return htmlspecialchars((string) $data, ENT_QUOTES, $encoding); }; $this->outputFilters += compact('h') + $this->_config['outputFilters']; foreach (array('loader', 'renderer') as $key) { if (is_object($this->_config[$key])) { $this->{'_' . $key} = $this->_config[$key]; continue; } $class = $this->_config[$key]; $config = array('view' => $this) + $this->_config; $this->{'_' . $key} = Libraries::instance('adapter.template.view', $class, $config); } }
protected function _init() { parent::_init(); unset($this->_config['type']); $keys = array_keys($this->_config); foreach ($this->_initializers as $key) { $val = $this->_config[$key]; if ($val !== null) { $this->_config[$key] = is_array($val) ? array() : null; $this->{$key}($val); } } if ($list = $this->_config['whitelist']) { $this->_config['whitelist'] = array_combine($list, $list); } if ($this->_entity && !$this->_config['model']) { $this->model($this->_entity->model()); } if ($this->_config['with']) { if (!($model = $this->model())) { throw new ConfigException("The `'with'` option needs a valid binded model."); } $this->_config['with'] = Set::normalize($this->_config['with']); } if ($model = $this->model()) { $this->alias($this->_config['alias'] ?: $model::meta('name')); } $this->fields($this->_config['fields']); unset($this->_config['entity'], $this->_config['init']); }
/** * Initializes the collection object by merging in collection items and removing redundant * object properties. * * @return void */ protected function _init() { parent::_init(); unset($this->_config['data']); }
protected function _init() { parent::_init(); $this->_updated = $this->_data; }