Example #1
0
 /**
  * Create and save a new item
  * @param  array $data
  * @return Comment
  */
 public static function create(array $data)
 {
     $item = new static();
     // Check required fields
     foreach (self::$requiredFields as $field) {
         if (!isset($data[$field])) {
             throw new Exception("Required field {$field} not specified.");
         }
     }
     // Set field values
     foreach ($data as $key => $val) {
         if ($item->exists($key)) {
             if (empty($val)) {
                 $val = null;
             }
             $item->set($key, $val);
         }
     }
     // Set auto values if they exist
     if ($item->exists("created_date") && !isset($data["created_date"])) {
         $item->set("created_date", date("Y-m-d H:i:s"));
     }
     $item->save();
     return $item;
 }
 /**
  * @param $filepath
  *
  * @return static
  */
 public static function create($filepath)
 {
     $dotenv = new static($filepath);
     if (!$dotenv->exists()) {
         touch($filepath);
     }
     return $dotenv;
 }
 /**
  * 根据key查找payload
  * @param $key
  * @return StoredFileContract | null
  */
 public static function find($key)
 {
     $instance = new static($key);
     if ($instance->exists()) {
         return $instance;
     } else {
         return null;
     }
 }
Example #4
0
 /**
  * notify.
  *
  * @param object $user
  * @param string $action
  * @param object $model
  * @param array|object $metadata
  *
  * @return bool
  */
 public static function notify($user, $action, $model = null, $metadata = null)
 {
     if ($model instanceof self) {
         return false;
     }
     $log = new static();
     $log->setFromNotify($user, $action, $model, $metadata);
     $log->save();
     return $log->exists();
 }
Example #5
0
 public function getParent()
 {
     if ($this->hasParent()) {
         $parent = new static($this->parent_fk);
         if ($parent->exists()) {
             return $parent;
         }
     }
     return false;
 }
 /**
  * Create a new instance, including the file and parent directories.
  *
  * @param $path
  *
  * @return static
  */
 public static function create($path)
 {
     $file = new static($path);
     if (!is_dir(dirname($path))) {
         mkdir(dirname($path), 0755, true);
     }
     if (!$file->exists()) {
         touch($path);
     }
     return $file;
 }
Example #7
0
 /**
  * Método estético para verificar el arreglo.
  *
  * Se debe pasar el arreglo del usuario, si el usuario es válido retorna un nuevo token.
  *
  * @param array $dataUser Contiene el arreglo con el que trabajará.
  *
  * @return string
  *
  */
 public static function verify($dataUser = null)
 {
     $instance = new static($dataUser);
     if (!$instance->checkStructure()) {
         return false;
     }
     if (!$instance->onTime()) {
         return false;
     }
     if (!$instance->exists()) {
         return false;
     }
     return TokenFromUser::getToken($instance->dataUser);
 }
 /**
  * Find one entity by id
  *
  * @param $id
  *
  * @return null|static
  */
 public static function findById($id)
 {
     $obj = new static($id);
     if ($obj->exists()) {
         return $obj;
     }
     return null;
 }
 /**
  * Installs this component from a zip file (if available)
  * @todo DB stuff (structure and data)
  * @todo check dependency versions
  * @todo activate templates
  */
 public function install()
 {
     // Check (not already installed (different version), all dependencies installed)
     if (!$this->packageFile) {
         throw new SystemComponentException('Package file not available');
     }
     if (!file_exists(ASCMS_APP_CACHE_FOLDER . '/meta.yml')) {
         throw new ReflectionComponentException('Invalid package file');
     }
     if ($this->exists()) {
         throw new SystemComponentException('Component is already installed');
     }
     $websitePath = \Env::get('cx')->getWebsiteDocumentRootPath();
     // Read meta file
     $yaml = new \Symfony\Component\Yaml\Yaml();
     $content = file_get_contents(ASCMS_APP_CACHE_FOLDER . '/meta.yml');
     $meta = $yaml->load($content);
     // Check dependencies
     echo "Checking  dependencies ... ";
     foreach ($meta['DlcInfo']['dependencies'] as $dependencyInfo) {
         $dependency = new static($dependencyInfo['name'], $dependencyInfo['type']);
         if (!$dependency->exists()) {
             throw new SystemComponentException('Dependency "' . $dependency->getName() . '" not met');
         }
     }
     echo "Done \n";
     // Copy ZIP contents
     echo "Copying files to installation ... ";
     $filesystem = new \Cx\Lib\FileSystem\FileSystem();
     $filesystem->copyDir(ASCMS_APP_CACHE_FOLDER . '/DLC_FILES', ASCMS_APP_CACHE_FOLDER_WEB_PATH . '/DLC_FILES', '', $websitePath, '', '', true);
     echo "Done \n";
     // Activate (if type is system or application)
     // TODO: templates need to be activated too!
     if ($this->componentType != 'core' && $this->componentType != 'core_module' && $this->componentType != 'module') {
         return;
     }
     // Copy ZIP contents (also copy meta.yml into component folder if type is system or application)
     try {
         $objFile = new \Cx\Lib\FileSystem\File(ASCMS_APP_CACHE_FOLDER . '/meta.yml');
         $objFile->copy($this->getDirectory(false) . '/meta.yml');
     } catch (\Cx\Lib\FileSystem\FileSystemException $e) {
         \DBG::msg($e->getMessage());
     }
     echo "Importing component data (structure & data) ... ";
     if (!file_exists($this->getDirectory(false) . "/Model/Yaml")) {
         $this->importStructureFromSql();
     } else {
         $this->createTablesFromYaml();
     }
     $this->importDataFromSql();
     echo "Done \n";
     // Activate this component
     echo "Activating component ... ";
     $this->activate();
     echo "Done \n";
 }
Example #10
0
 /**
  * Checks whether if an item exists on the queried path or not
  * @param string $query
  * @return bool
  */
 public function exists($query)
 {
     if (empty($query)) {
         throw new \InvalidArgumentException('Argument 1 should not be empty.');
     }
     $keyExists = 'exists' . $this->type;
     $key = '';
     $result = null;
     while (strlen($key .= $this->shiftKey($query)) > 0 and $result === null) {
         $realKey = $this->parseKey($key);
         if ($this->{$keyExists}($realKey)) {
             switch ($this->type) {
                 case static::TYPE_ARRAY:
                     $result =& $this->object[$realKey];
                     break;
                 case static::TYPE_ACCESS:
                     if ($key[0] !== '@' and $key[0] !== '!' and $this->object->offsetExists($realKey)) {
                         $result =& $this->object->offsetGet($realKey);
                         break;
                     }
                 case static::TYPE_OBJECT:
                     if ($key[0] === '@') {
                         $call = $this->object[$realKey];
                         if (!$call instanceof Provider) {
                             throw new \RuntimeException('"' . $key . '" is not a Provider. Using "@" (service-getter) prefix only allowed on Provider item.');
                         }
                         $result = $call($this->object);
                     } else {
                         if ($key[0] == '!') {
                             $call = $this->object[$realKey];
                             if (!$call instanceof Provider) {
                                 throw new \RuntimeException('"' . $key . '" is not a Provider. Using "!" (mutation-getter) prefix only allowed on Provider item.');
                             }
                             if (!$call->isMutated()) {
                                 throw new \RuntimeException('Provider "' . $key . '" is not mutated.');
                             }
                             $result =& $call->getMutatedItem();
                         } else {
                             $result =& $this->object->{$realKey};
                         }
                     }
                     break;
             }
             if ($query !== '' and (is_array($result) or is_object($result))) {
                 $q = new static($result);
                 if ($q->exists($query)) {
                     $result =& QueryParser::Create($result)->find($query);
                 } else {
                     return false;
                 }
             }
         } else {
             if ($query === '') {
                 return false;
             }
         }
     }
     return true;
 }
Example #11
0
 /**
  * @ignore
  */
 public static function __callStatic($name, $args)
 {
     $model = new static();
     if (!empty($args) && strpos($name, 'getAllBy') === 0) {
         $name = substr_replace($name, 'loadAllBy', 0, 8);
         return call_user_func_array(array($model, $name), $args);
     } elseif (!empty($args) && strpos($name, 'getBy') === 0) {
         $name = substr_replace($name, 'loadBy', 0, 5);
         return call_user_func_array(array($model, $name), $args);
     } elseif (!empty($args) && strpos($name, 'updateBy') === 0) {
         $method = substr_replace($name, 'loadBy', 0, 8);
         $model->{$method}($args[0]);
         if ($model->exists()) {
             $model->update($args[1]);
             return $model;
         }
         return false;
     } elseif (!empty($args) && strpos($name, 'updateAllBy') === 0) {
         $method = substr_replace($name, 'loadAllBy', 0, 11);
         $models = $model->{$method}($args[0]);
         $affected = 0;
         foreach ($models as $mod) {
             $mod->update($args[1]);
             $affected++;
         }
         return $affected;
     }
 }