Exemplo n.º 1
0
 private function _doSync()
 {
     $it = new DirectoryIterator(self::getCoreComponentsDir());
     foreach ($it as $dir) {
         if (!$dir->isDot() && $dir->isDir()) {
             $componentFile = new SplFileInfo($dir->getPathname() . DIRECTORY_SEPARATOR . 'Component' . DIRECTORY_SEPARATOR . 'Component.php');
             if ($componentFile->isFile()) {
                 try {
                     $class = new ReflectionClass('\\' . $dir->getFilename() . '\\Component\\Component');
                     if (!$class->isAbstract() && !$class->isInterface() && !$class->isTrait() && $class->implementsInterface('ModSync\\Component\\IsComponentInterface')) {
                         $o = $class->newInstance();
                         if ($o->isSyncable()) {
                             $o->sync();
                         }
                     }
                 } catch (LogicException $e) {
                     ModSync\Logger::warn($e->getMessage());
                 } catch (ReflectionException $e) {
                     ModSync\Logger::warn($e->getMessage());
                 } catch (\Exception $e) {
                     die($e->getMessage());
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Syncs category object with modx
  *
  * @param mixed
  */
 public final function sync()
 {
     if (!$this->isSyncable()) {
         return;
     }
     $parent = $this->getParent();
     /* @var $category \modCategory */
     $category = self::getModX()->getObject('modCategory', array('category' => $this->getName()));
     if (!$category) {
         ModSync\Logger::info('Inserting: ' . get_called_class());
         $category = self::getModX()->newObject('modCategory');
         $category->set('category', $this->getName());
         if ($parent) {
             $category->set('parent', $parent->getId());
         }
         $this->onInsert();
         $category->save();
     } else {
         if ($parent && $parent->getId() != $category->get('parent') || !$parent && intval($category->get('parent')) > 0) {
             ModSync\Logger::info('Updating: ' . get_called_class());
             if ($parent) {
                 $category->set('parent', intval($parent->getId()));
             } else {
                 $category->set('parent', 0);
             }
             $this->onUpdate();
             $category->save();
         } else {
             ModSync\Logger::debug('No changes to: ' . get_called_class());
         }
     }
     $this->_id = $category->get('id');
 }
Exemplo n.º 3
0
 /**
  * Report execution time
  */
 public final function reportProfiling()
 {
     if (null !== $this->_profiling && $this->_profiling->enabled) {
         $this->_profiling->endTime = microtime();
         $begin = explode(' ', $this->_profiling->beginTime);
         $end = explode(' ', $this->_profiling->endTime);
         $this->_profiling->duration = $end[1] + $end[0] - ($begin[1] + $begin[0]);
         ModSync\Logger::debug(sprintf(get_called_class() . ': %s (%1.3f seconds)', $this->getName(), $this->_profiling->duration));
         if ($this->_profiling->duration > $this->_profiling->threshold) {
             ModSync\Logger::warn(sprintf(get_called_class() . ': inefficient code - %s (%1.3f seconds)', $this->getName(), $this->_profiling->duration));
         }
     }
 }
Exemplo n.º 4
0
 /**
  * Sync context object
  */
 public final function sync()
 {
     if (!$this->isSyncable()) {
         return $this;
     }
     /* @var $context \modContext */
     $context = self::getModX()->getObject('modContext', array('key' => $this->getKey()));
     if ($context) {
         ModSync\Logger::debug('Already exists: ' . get_called_class());
         $this->onUpdate();
     } else {
         ModSync\Logger::info('Inserting: ' . get_called_class());
         $context = self::getModX()->newObject('modContext');
         $context->set('key', $this->getKey());
         $context->set('description', $this->getDescription());
         $context->set('rank', (int) $this->_rank);
         $this->onInsert();
         $context->save();
     }
 }
Exemplo n.º 5
0
 /**
  * Sync context object
  */
 public final function sync()
 {
     if (!$this->isSyncable()) {
         return;
     }
     /* @var $modxElement \modContextSetting */
     $modxElement = self::getModX()->getObject('modContextSetting', array('context_key' => $this->getContextKey(), 'key' => $this->getKey(), 'namespace' => $this->getNamespace()));
     if ($modxElement) {
         ModSync\Logger::debug('Already exists: ' . get_called_class());
     } else {
         ModSync\Logger::info('Inserting: ' . get_called_class());
         $modxElement = self::getModX()->newObject('modContextSetting');
         $modxElement->set('context_key', strtolower($this->getContextKey()));
         $modxElement->set('key', strtolower($this->getKey()));
         $modxElement->set('namespace', $this->getNamespace());
         $modxElement->set('value', $this->getValue());
         $modxElement->set('xtype', $this->_xtype);
         $modxElement->set('area', $this->getArea());
         $this->onInsert();
         $modxElement->save();
     }
 }
Exemplo n.º 6
0
 /**
  * Sync media source
  */
 public final function sync()
 {
     if (!$this->isSyncable()) {
         return;
     }
     /* @var $modElement \modMediaSource */
     $modElement = self::getModX()->getObject('sources.modMediaSource', array('name' => $this->getName()));
     if ($modElement) {
         ModSync\Logger::debug('Already exists: ' . get_called_class());
     } else {
         ModSync\Logger::info('Inserting: ' . get_called_class());
         $modElement = self::getModX()->newObject('sources.modMediaSource');
         $modElement->set('name', $this->getName());
         $modElement->set('description', $this->getDescription());
         $modElement->set('class_key', $this->getClassKey());
         $this->_properties['basePath']['value'] = 'assets' . DIRECTORY_SEPARATOR . $this->getUrl();
         $this->_properties['baseUrl']['value'] = ($this->_relative_url ? '' : '/') . 'assets/' . $this->getUrl();
         $this->_properties['baseUrlRelative']['value'] = $this->_relative_url;
         $modElement->setProperties($this->_properties);
         @mkdir(self::getAssetsDir() . DIRECTORY_SEPARATOR . trim($this->getUrl(), '/'), 0755, true);
         $this->onInsert();
         $modElement->save();
     }
 }
Exemplo n.º 7
0
 /**
  * Loops through elements to be synced
  *
  * @param string $path - path
  *
  * @todo Improve error handling of this method.
  */
 private function _syncFolder($path)
 {
     $dir = new \SplFileInfo($path);
     if (!$dir->isDir()) {
         return;
     }
     $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS));
     $filePath = '';
     while ($it->valid()) {
         /* @var $file SplFileInfo */
         $file = $it->current();
         if ($filePath != $file->getPathname() && !$it->isDir() && $it->getExtension() == 'php') {
             $filePath = $file->getPathname();
             $name = str_replace(DIRECTORY_SEPARATOR, '\\', substr($file->getPathname(), strlen(self::getCoreComponentsDir() . DIRECTORY_SEPARATOR), (strlen($file->getExtension()) + 1) * -1));
             try {
                 $class = new ReflectionClass($name);
                 if (!$class->isAbstract() && !$class->isInterface() && !$class->isTrait() && $class->implementsInterface('ModSync\\IsSyncableInterface')) {
                     /* @var $o ModSync\IsSyncableInterface */
                     $o = $class->newInstance();
                     if ($o->isSyncable()) {
                         if ($class->implementsInterface('ModSync\\Element\\Category\\HasCategoryInterface')) {
                             if (!$o->hasCategory() && $this->hasCategory()) {
                                 $o->setCategory($this->getCategory());
                             }
                         }
                         $o->sync();
                     }
                 }
             } catch (LogicException $e) {
                 ModSync\Logger::warn($e->getMessage());
             } catch (ReflectionException $e) {
                 ModSync\Logger::warn($e->getMessage());
             } catch (\Exception $e) {
                 die($e->getMessage());
             }
         }
         $it->next();
     }
 }
Exemplo n.º 8
0
 /**
  * Syncs an element with modx
  *
  * @param string $name Element type
  * @param array $array Primary key
  * @return \modElement|false
  */
 protected final function _sync($name, $array)
 {
     if (!$this->isSyncable()) {
         return false;
     }
     /* @var $modElement \modElement */
     $modElement = self::getModX()->getObject($name, $array);
     if (!$modElement) {
         $modElement = self::getModX()->newObject($name, $array);
         $this->onInsert();
         ModSync\Logger::info('Inserting: ' . get_called_class());
     } else {
         if (!$this->_isSyncAllowed($modElement)) {
             ModSync\Logger::debug('Syncing disabled by user: '******'No changes to: ' . get_called_class());
             return false;
         }
         $this->onUpdate();
         ModSync\Logger::info('Updating: ' . get_called_class());
     }
     if (null === $modElement) {
         throw new Exception('Trying to sync with null element');
     }
     if ($this instanceof \ModSync\HasContentInterface) {
         $modElement->setContent($this->getContent());
     }
     if ($this->hasCategory()) {
         $modElement->set('category', $this->getCategory()->getId());
     }
     $modElement->set('description', $this->getDescription());
     $modElement->set('locked', intval($this->_locked));
     $modElement->setProperties($this->_properties, true);
     if ($this->onBeforeSave($modElement) !== false) {
         $modElement->save();
     }
     return $modElement;
 }
Exemplo n.º 9
0
 /**
  * This is where the magic happens... modx snippet should always call this
  * method to execute the snippet.
  *
  * @return string
  */
 public final function run()
 {
     try {
         $this->beginProfiling();
         $this->defineParams();
         /* @var $param ModSync\Element\Parameter\ParameterAbstract */
         foreach ($this->getParams() as $param) {
             if (isset($this->_args['properties'][strtolower($param->getName())]) && !empty($this->_args['properties'][strtolower($param->getName())])) {
                 $param->setValue($this->_args['properties'][strtolower($param->getName())]);
             } else {
                 if ($param->isRequired()) {
                     throw new ModSync\Element\Parameter\Exception('Parameter (' . $param->getName() . ') is required');
                 }
             }
         }
         $this->beforeDispatch();
         $this->_output = $this->dispatch();
         $this->afterDispatch();
         $this->reportProfiling();
         return $this->_output;
     } catch (ModSync\Exception $e) {
         $msg = 'Snippet (' . $this->getName() . ') failed: ' . $e->getMessage();
         ModSync\Logger::error($msg);
         die($msg);
     }
 }
Exemplo n.º 10
0
 /**
  * This method will be called by modx event trigger.
  * This is where we call the current event handler.
  * 
  * @return mixed
  */
 public final function run()
 {
     $this->beginProfiling();
     $event_name = $this->getEvent()->name;
     ModSync\Logger::info(get_called_class() . ' - ' . $event_name);
     $return = $this->{$event_name}($this->getEvent()->params);
     $this->reportProfiling();
     return $return;
 }