Exemplo n.º 1
0
 /**
  * Get assets code by type from an holder
  *
  * @param string		$extension
  * @param string		$name
  */
 public static function code($extension = null, $name = null)
 {
     $assets = static::get($extension, $name);
     // let packtacular handle css and less files
     if ($extension == 'css') {
         foreach (static::holder($name)->get('_packtacular') as $key => $pack) {
             if (!array_key_exists('less', $pack)) {
                 $pack['less'] = array();
             }
             if (!array_key_exists('css', $pack)) {
                 $pack['css'] = array();
             }
             $files = array_merge($pack['css'], $pack['less']);
             if (!empty($files)) {
                 CCArr::push(Packtacular::handle($files, basename(static::holder($name)->path) . '/' . $name . '/', $key . '_{time}.css'), $assets);
             }
         }
     } elseif ($extension == 'js') {
         foreach (static::holder($name)->get('_packtacular') as $key => $pack) {
             if (!array_key_exists('js', $pack)) {
                 $pack['js'] = array();
             }
             if (!empty($pack['js'])) {
                 CCArr::push(Packtacular::handle($pack['js'], basename(static::holder($name)->path) . '/' . $name . '/', $key . '_{time}.js'), $assets);
             }
         }
     }
     $buffer = "";
     foreach ($assets as $item) {
         $buffer .= call_user_func('CCAsset::' . $extension, $item, $name);
     }
     return $buffer;
 }
Exemplo n.º 2
0
 /**
  * Read the configuration data 
  * Get the configuration data and return them as array
  *
  * @param string		$name
  * @return array
  */
 public function read($name)
 {
     // check for absolut path
     if (substr($name, 0, 1) == '/' && file_exists($name)) {
         return $this->read_file($name);
     }
     $in_namespace = false;
     // split the namespace
     if (strpos($name, '::') !== false) {
         $conf = explode('::', $name);
         $in_namespace = $conf[0];
         $name = $conf[1];
         unset($conf);
     }
     // the two holders
     $main = $default = array();
     /*
      * App config
      * if app config try to load the equal core config as defaults
      */
     if (!$in_namespace) {
         $core_conf = $this->path(CCCORE_NAMESPACE . '::' . $name, true);
         $app_conf = $this->path($name, true);
         if (file_exists($core_conf)) {
             $default = $this->read_file($core_conf);
         }
         if (file_exists($app_conf)) {
             $main = $this->read_file($app_conf);
         }
     } elseif ($in_namespace) {
         $main_conf = $this->path($in_namespace . '::' . $name, true);
         $app_conf = $this->path($name, true);
         if (file_exists($main_conf)) {
             $default = $this->read_file($main_conf);
         }
         if (file_exists($app_conf)) {
             $main = $this->read_file($app_conf);
         }
     }
     // finally return the data
     return CCArr::merge($default, $main);
 }
Exemplo n.º 3
0
 /**
  * Add values to the insert
  *
  * @param array 		$values
  * @return void
  */
 public function values(array $values)
 {
     // do nothing if we get nothing
     if (empty($values)) {
         return $this;
     }
     // check if the the passed array is a collection.
     // because we want to be able to insert bulk values.
     if (!\CCArr::is_collection($values)) {
         $values = array($values);
     }
     // because we could recive the arrays in diffrent order
     // we have to sort them by their key.
     foreach ($values as $key => $value) {
         ksort($value);
         $values[$key] = $value;
     }
     // merge the new values with the existing ones.
     $this->values = array_merge($this->values, $values);
     // return self so we can continue running the next function
     return $this;
 }
Exemplo n.º 4
0
 /**
  * Revert the last migration
  *
  * @return void
  */
 public static function rollback()
 {
     // first of all we have to filter only the already migrated versions
     $available = static::available();
     foreach ($available as $key => $migrations) {
         foreach ($migrations as $time => $migration) {
             if ($time > static::$config->get($key . '.revision', 0)) {
                 unset($available[$key][$time]);
             }
         }
     }
     $revisions = array();
     foreach ($available as $key => $value) {
         if (empty($value)) {
             continue;
         }
         foreach ($value as $name => $path) {
             $revisions[$name . '::' . $key] = $path;
         }
     }
     // nothing to rollback?
     if (empty($revisions)) {
         if (\ClanCats::is_cli()) {
             \CCCli::warning('nothing to rollback to.');
         }
         return false;
     }
     ksort($revisions);
     end($revisions);
     list($time, $key) = explode('::', key($revisions));
     $migration = new static(array_pop($revisions));
     // rollback the migration
     $migration->down();
     // get the lastet migration from the group
     $others = \CCArr::get($key, $available);
     ksort($others);
     array_pop($others);
     end($others);
     // update the config
     static::$config->set($key . '.revision', key($others));
     static::$config->write();
     return true;
 }
Exemplo n.º 5
0
 /**
  * CCMail::is_plaintext tests
  */
 public function test_is_plaintext()
 {
     $mail = CCMail::create();
     $mail->to('*****@*****.**');
     $mail->is_plaintext();
     $mail->send();
     $mail_data = CCArr::last(Mail\Transporter_Array::$store);
     $this->assertEquals(true, $mail_data['is_plaintext']);
 }
Exemplo n.º 6
0
 /**
  * Assign an model with some data
  *
  * @param array 			$data
  * @return CCModel
  */
 public static function assign($data)
 {
     // return null if we have no data
     if ($data === null || empty($data)) {
         return null;
     }
     // do we have a collection or a single result?
     if (CCArr::is_collection($data)) {
         $return = array();
         foreach ($data as $key => $item) {
             $return[$key] = static::create($item);
         }
         return $return;
     }
     return static::create($data);
 }
Exemplo n.º 7
0
 /**
  * custom setter with encode ability
  *
  * @param string 	$key
  * @param mixed		$value
  * @param mixed 		$param
  * @return void
  */
 public function set($key, $value, $param = null)
 {
     if ($param === true) {
         $value = CCStr::htmlentities($value);
     }
     return CCArr::set($key, $value, $this->_data);
 }
Exemplo n.º 8
0
 /**
  * CCRequest constructor
  * 
  * @param CCRoute			$route
  * @param CCIn_Instance 		$input
  */
 public function __construct($route, $input = null)
 {
     // assign the route and input
     $this->route = $route;
     $this->input = $input;
     // create an object with default arguments
     $this->args = CCArr::object(ClanCats::$config->get('controller.default_args'));
 }
Exemplo n.º 9
0
 /**
  * generates an file header string
  *
  * @param string		$title
  * @param array 		$data
  * @return string
  */
 public function make_comment_header($title, $data = array())
 {
     // get author
     $authors = \CCArr::get('authors', $data, \CCConfig::create('shipyard')->get('defaults.authors'));
     // author
     if (is_array($authors)) {
         foreach ($authors as $person) {
             $author_str .= $person['name'] . " ";
             if (array_key_exists('email', $person)) {
                 $author_str .= "<" . $person['email'] . ">";
             }
             $author_str .= ", ";
         }
         $author_str = substr($author_str, 0, -2);
     }
     return "{$title}\n" . "*\n" . "\n" . "@package       " . \CCArr::get('package', $data, \CCConfig::create('shipyard')->get('defaults.package')) . "\n" . "@author        " . $author_str . "\n" . "@version       " . \CCArr::get('version', $data, \CCConfig::create('shipyard')->get('defaults.version')) . "\n" . "@copyright     " . \CCArr::get('copyright', $data, \CCConfig::create('shipyard')->get('defaults.copyright')) . "\n";
 }
Exemplo n.º 10
0
 /**
  * Model finder 
  * This function allows you direct access to your records.
  *
  * @param mixed		$param
  * @param mixed		$param2
  * @return CCModel
  */
 public static function find($param = null, $param2 = null)
 {
     $settings = static::_model();
     $query = DB::select($settings['table']);
     // Do we have a find modifier?
     if (!is_null($settings['find_modifier'])) {
         $callbacks = $settings['find_modifier'];
         if (!\CCArr::is_collection($callbacks)) {
             $callbacks = array($callbacks);
         }
         foreach ($callbacks as $call) {
             if (is_callable($call)) {
                 call_user_func_array($call, array(&$query));
             } else {
                 throw new ModelException("Invalid Callback given to find modifiers.");
             }
         }
     }
     if (!is_null($param)) {
         // Check if paramert 1 is a valid callback and not a string.
         // Strings as function callback are not possible because
         // the user might want to search by key like:
         // Model::find( 'key', 'type' );
         if (is_callable($param) && !is_string($param)) {
             call_user_func_array($param, array(&$query));
         } elseif (is_null($param2)) {
             $query->where($settings['table'] . '.' . $settings['primary_key'], $param)->limit(1);
         } elseif (!is_null($param2)) {
             $query->where($param, $param2)->limit(1);
         }
     }
     // alway group the result
     $query->forward_key($settings['primary_key']);
     // and we have to fetch assoc
     $query->fetch_arguments = array('assoc');
     // and assign
     return static::assign($query->run());
 }
Exemplo n.º 11
0
 /**
  * Get the query log 
  * 
  * @return array
  */
 public static function last_query()
 {
     return \CCArr::last(static::$_query_log);
 }
Exemplo n.º 12
0
 /**
  * Garbage collection, delete all outdated sessions
  *
  * @return void
  */
 public function gc()
 {
     $lifetime = \CCArr::get('lifetime', $this->_config, \CCDate::minutes(5));
     if ($lifetime < ($min_lifetime = \CCArr::get('min_lifetime', $this->_config, \CCDate::minutes(5)))) {
         $lifetime = $min_lifetime;
     }
     $this->_driver->gc($lifetime);
 }
Exemplo n.º 13
0
 /**
  * delete data from our array
  *
  * @param string 	$key
  * @return mixed
  */
 public function delete($key)
 {
     CCArr::delete($key, $this->_data);
 }
Exemplo n.º 14
0
 /**
  * install an orbit module
  *
  * @param array 		$params 
  */
 public function action_install($params)
 {
     $path = $params[0];
     // get target directory
     $target_dir = \CCArr::get('target', $params, ORBITPATH);
     if (empty($path)) {
         CCCli::line('no ship path given.', 'red');
         return;
     }
     /*
      * direct install if starting with /
      */
     if (substr($path, 0, 1) == '/') {
         // fix path
         if (substr($path, -1) != '/') {
             $path .= '/';
         }
         // is directory
         if (!is_dir($path)) {
             CCCli::line('could not find a ship at path: ' . $path, 'red');
             return;
         }
         // define the target
         $target = $target_dir . basename($path) . '/';
         // check if we already have an directory with the same name
         if ($target != $path && is_dir($target)) {
             if (!CCCli::confirm("there is already a ship with this name. do you want to overwrite?", true)) {
                 return;
             }
         }
         // are ya serius..
         if (!CCCli::confirm("are you sure you want to install this ship?", true)) {
             return;
         }
         // move the directory
         if ($target != $path) {
             rename($path, $target);
         }
         // run the installer
         try {
             \CCOrbit::install($target);
         } catch (\Exception $e) {
             CCCli::line($e->getMessage(), 'red');
             CCCli::line('ship installation failure.', 'red');
             return;
         }
         // we are done
         CCCli::line('ship installation succeeded', 'green');
         return;
     }
     // check if the module is in our orbit path
     if (is_dir(ORBITPATH . $path)) {
         // there is a ship yay
         CCCli::line('found ship at path: ' . ORBITPATH . $path, 'green');
         return static::action_install(array(ORBITPATH . $path, 'target' => \CCArr::get('target', $params)));
     }
     // check if the module is in CCF dir
     if (is_dir(CCROOT . $path)) {
         // there is a ship yay
         CCCli::line('found ship at path: ' . CCROOT . $path, 'green');
         return static::action_install(array(CCROOT . $path, 'target' => \CCArr::get('target', $params)));
     }
     // search the repository for this ship
     CCCli::line('searching the repositories for: ' . $path . ' ...', 'cyan');
 }
Exemplo n.º 15
0
 /**
  * Test Query_Select::last
  */
 public function test_last()
 {
     $result = DB::select('people')->last();
     $person = CCArr::last(static::people_provider());
     $person = $person[0];
     foreach ($person as $key => $value) {
         $this->assertEquals($value, $result->{$key});
     }
 }
Exemplo n.º 16
0
 /**
  * Transporter instance constructor
  *
  * @param string 		$name
  * @param array 			$config
  * @return void
  */
 public function __construct($name, $config = null)
 {
     if (is_null($config)) {
         $config = \CCConfig::create('mail')->get('transporter.' . $name);
         // check for an alias. If you set a string
         // in your config file we use the config
         // with the passed key.
         if (is_string($config)) {
             $config = \CCConfig::create('mail')->get('transporter.' . $config);
         }
     }
     if (!is_array($config)) {
         throw new Exception("Auth\\Handler::create - Invalid auth handler (" . $name . ").");
     }
     // also don't forget to set the name manager name becaue we need him later.
     $this->name = $name;
     // assign defaults and create the configuration object
     $this->config = \CCDataObject::assign(\CCArr::merge(array('driver' => 'sendmail'), $config));
     // load the driver
     $driver_class = __NAMESPACE__ . '\\Transporter_' . ucfirst($this->config->driver);
     if (!class_exists($driver_class)) {
         throw new Exception("Invalid mail driver '" . $this->config->driver . "'");
     }
     $this->driver = new $driver_class($this->config);
 }
Exemplo n.º 17
0
 /**
  * Cookie driver constructor
  */
 public function __construct($name, $conf)
 {
     $this->cookie_suffix = \CCArr::get('cookie_suffix', $conf, '-session-store');
     $this->crypt_salt = \CCArr::get('crypt_salt', $conf);
 }
Exemplo n.º 18
0
 /**
  * test the CCArr isset
  */
 public function testArrayHasItem()
 {
     /*
      * get string
      */
     $this->assertTrue(CCArr::has('string', $this->test_array));
     /*
      * get not existing
      */
     $this->assertFalse(CCArr::has('not.existing', $this->test_array));
 }
Exemplo n.º 19
0
 /**
  * get the path of an uplaoded file
  *
  * @param string	$key
  * @return string|false
  */
 public static function upload_path($key)
 {
     return CCArr::get('tmp_name', CCIn::file($key, array('tmp_name' => false)));
 }
Exemplo n.º 20
0
 /**
  * Auth instance constructor
  *
  * @param string 		$name
  * @param array 			$config
  * @return void
  */
 public function __construct($name, $config = null)
 {
     if (is_null($config)) {
         $config = \CCConfig::create('auth')->get($name);
         // check for an alias. If you set a string
         // in your config file we use the config
         // with the passed key.
         if (is_string($config)) {
             $config = \CCConfig::create('auth')->get($config);
         }
     }
     if (!is_array($config)) {
         throw new Exception("Auth\\Handler::create - Invalid auth handler (" . $name . ").");
     }
     // also don't forget to set the name manager name becaue we need him later.
     $this->name = $name;
     // assign defaults and create the configuration object
     $this->config = \CCDataObject::assign(\CCArr::merge(array('session_manager' => null, 'session_key' => 'user_id', 'user_key' => 'id', 'user_model' => "\\Auth\\User", 'identifiers' => array('email'), 'logins' => array('handler' => null, 'table' => 'auth_logins'), 'restore' => array('id_cookie' => 'ccauth-restore-id', 'token_cookie' => 'ccauth-restore-token', 'lifetime' => \CCDate::months(1))), $config));
     // set the session handler
     $this->session = \CCSession::manager($this->config->session_manager);
     $user_model = $this->config->user_model;
     // set a empty default user object to avoid
     // on a non object errors
     $this->user = new $user_model();
     // do we already have a user id means are we
     // logged in?
     if (!is_null($session_key = $this->session_user_id())) {
         if ($user = $user_model::find($this->config->user_key, $session_key)) {
             $this->user = $user;
             return $this->authenticated = true;
         }
     } else {
         $restore_id_cookie = $this->config->get('restore.id_cookie');
         $restore_token_cookie = $this->config->get('restore.token_cookie');
         if (CCCookie::has($restore_id_cookie) && CCCookie::has($restore_token_cookie)) {
             // get the restore cookies
             $restore_id = CCCookie::get($restore_id_cookie);
             $restore_token = CCCookie::get($restore_token_cookie);
             // get the restore login
             $login = $this->select_logins()->where('restore_id', $restore_id)->where('restore_token', $restore_token)->limit(1);
             // if no login found kill the cookies and return
             if (!($login = $login->run())) {
                 $this->kill_restore();
                 return $this->authenticated = false;
             }
             // Invalid user? kill the cookies and return
             if (!($user = $user_model::find($this->config->user_key, $restore_id))) {
                 $this->kill_restore();
                 return $this->authenticated = false;
             }
             // validate the restore key if invalid
             // once again kill the cookies and return
             if ($login->restore_token != $this->restore_key($user)) {
                 $this->kill_restore();
                 return $this->authenticated = false;
             }
             // If everything is fine sign the user in and
             // update the restore keys
             $this->sign_in($user, true);
             return $this->authenticated = true;
         }
     }
     return $this->authenticated = false;
 }
Exemplo n.º 21
0
 /**
  * get the average of the items 
  *
  * @param array[array]	$arr
  * @param string			$key
  */
 public static function average($arr, $key = null)
 {
     if (!is_array($arr)) {
         throw new \InvalidArgumentException('CCArr::average - first argunent has to be an array.');
     }
     if (is_string($key) && CCArr::is_multi($arr)) {
         $arr = CCArr::pick($key, $arr);
     }
     return static::sum($arr) / count($arr);
 }
Exemplo n.º 22
0
 /**
  * Cookie driver constructor
  *
  * @param string		$name
  * @param array 		$conf
  */
 public function __construct($name, $conf)
 {
     $this->database = \CCArr::get('database', $conf);
     $this->table = \CCArr::get('table', $conf, 'sessions');
     $this->index_fields = array_merge(\CCArr::get('index_fields', $conf, array()), array_keys(Manager::default_data_provider()));
 }
Exemplo n.º 23
0
 /**
  * packs all responses into one array
  *
  * @param array		$responses
  * @return array
  */
 protected static function pack($responses)
 {
     return array_merge(CCArr::get('before', $responses, array()), CCArr::get('main', $responses, array()), CCArr::get('after', $responses, array()));
 }
Exemplo n.º 24
0
 /**
  * CCModel::__set
  *
  * @dataProvider people_provider
  */
 public function test_set($person)
 {
     $person_model = CCUnit\Model_Person::create($person);
     $person_model->age = mt_rand(-20, 20);
     $this->assertTrue($person_model->age >= 18);
     $this->assertTrue(CCArr::get('age', $person_model->raw()) >= 18);
 }