/** * Get view from class * * @param $name * @param array $config * @return mixed */ public function getView($name, $config = []) { $Cyan = \Cyan::initialize(); if (strpos($name, ':') === false) { $prefix = [ucfirst($this->getComponentName()), ucfirst($Cyan->getContainer('application')->getName())]; $sufix = ucfirst($name); } else { $parse = parse_url($name); $parts = explode(':', $parse['path']); $component = array_shift($parts); $name = empty($parts) ? $component : end($parts); $prefix = [ucfirst($component), ucfirst($Cyan->getContainer('application')->getName())]; $sufix = ucfirst($name); } if (!isset($config['layout'])) { $config['layout'] = 'page.' . $name . '.index'; } $class_name = sprintf('%sView%s', implode($prefix), $sufix); if (!class_exists($class_name)) { array_pop($prefix); $class_name = sprintf('%sView%s', implode($prefix), $sufix); } $view = $this->getClass($class_name, 'Cyan\\CMS\\View', [$config], function ($config) use($class_name) { return new $class_name($config); }); if (!$view->getLayout()->hasContainer('application')) { $view->getLayout()->setContainer('application', $this->getContainer('application')); if (!$view->getLayout()->hasContainer('factory_plugin')) { $view->getLayout()->setContainer('factory_plugin', $this->getContainer('application')->getContainer('factory_plugin')); } } Layout::addIncludePath($view->getBasePath() . DIRECTORY_SEPARATOR . 'layouts'); Layout::addIncludePath($view->getBasePath() . DIRECTORY_SEPARATOR . strtolower($Cyan->getContainer('application')->getName()) . DIRECTORY_SEPARATOR . 'layouts'); return $view; }
/** * Return array of options * * @return array * * @since 1.0.0 */ public function getOptions() { $table_name = $this->getAttribute('table'); $option_value = $this->getAttribute('option-value'); $option_text = $this->getAttribute('option-text'); $fields = [$option_value . ' AS value', $option_text . ' AS text']; $this->unsetAttributes(['table', 'option-value', 'option-text']); $id = $value = $this->getValue(); $Cyan = \Cyan::initialize(); // get current application $App = $Cyan->getContainer('application'); /** @var Database $Dbo */ $Dbo = $App->Database->connect(); $query = $Dbo->getDatabaseQuery()->from($table_name)->select(implode(',', array_filter($fields))); $sth = $Dbo->prepare($query); $sth->execute($query->getParameters()); $rows = $sth->fetchAll(\PDO::FETCH_OBJ); if (!empty($rows)) { foreach ($rows as $row) { $node = new XmlElement('<option name="' . $row->text . '" value="' . $row->value . '"></option>'); $this->options[] = new FormFieldOption($node); } unset($node); } return $this->options; }
public static function send($app, $config) { /** @var Cyan $Cyan */ $Cyan = \Cyan::initialize(); $mail = new \PHPMailer(true); $phpMailerIdentifier = $app->getName() . ':config.phpmailer'; $phpMailerConfig = $Cyan->Finder->getIdentifier($phpMailerIdentifier, []); if (empty($phpMailerConfig)) { throw new EmailException(sprintf('Config not found: %s', $Cyan->Finder->getPath($phpMailerIdentifier))); } $config = array_filter(array_merge_recursive($config, $phpMailerConfig)); foreach ($config as $key => $value) { if (property_exists($mail, $key)) { $mail->{$key} = $value; } elseif (method_exists($mail, $key)) { if (is_array($value)) { switch (count($value)) { case 1: $mail->{$key}($value[0]); break; case 2: $mail->{$key}($value[0], $value[1]); break; case 3: $mail->{$key}($value[0], $value[1], $value[2]); break; } } else { $mail->{$key}($value); } } } return $mail->send(); }
/** * Register plugins */ public function register($path) { if (!file_exists($path) && !is_dir($path)) { throw new ExtensionException(sprintf('path %s not exists', $path)); } $Cyan = \Cyan::initialize(); $App = $Cyan->getContainer('application'); $plugin_manager = $App->getContainer('factory_plugin'); $plugin_types = glob($path . '/*', GLOB_ONLYDIR); foreach ($plugin_types as $plugin_type) { $plugin_paths = glob($plugin_type . '/*', GLOB_ONLYDIR); foreach ($plugin_paths as $plugin_path) { $class_name = sprintf('Plugin%s%s', ucfirst(strtolower(basename($plugin_type))), ucfirst(strtolower(basename($plugin_path)))); $file_path = $plugin_path . DIRECTORY_SEPARATOR . basename($plugin_path) . '.php'; if (file_exists($file_path)) { $plugin_callback = (require_once $file_path); if (is_callable($plugin_callback)) { $type = basename($plugin_type); $name = basename($plugin_path); $plugin_manager->create($type, $name, $plugin_callback); } elseif (class_exists($class_name)) { $type = basename($plugin_type); $name = basename($plugin_path); $reflection_class = new ReflectionClass($class_name); if (!in_array('Cyan\\Framework\\TraitSingleton', $reflection_class->getTraitNames())) { throw new FactoryException(sprintf('%s class must use Cyan\\Trait\\Singleton', $class_name)); } unset($reflection_class); $plugin_manager->create($type, $name, $class_name::getInstance()); } } } } }
/** * Controller Constructor * * @param $controller_name * @param array $controller_settings * @param $closure * * @since 1.0.0 */ public function __construct($controller_name = '', array $controller_settings = [], \Closure $closure = null) { $this->Cyan = \Cyan::initialize(); $this->name = $controller_name; $this->settings = $controller_settings; if (!empty($closure) && is_callable($closure)) { call_user_func($closure->bindTo($this, $this)); } }
/** * */ public static function initialize() { $Cyan = \Cyan::initialize(); if (empty(self::$extension_manifests)) { foreach (glob($Cyan->Finder->getResource('app') . DIRECTORY_SEPARATOR . '*/extension.xml') as $extension_manifest) { self::$extension_manifests[basename(dirname($extension_manifest))] = $extension_manifest; } ksort(self::$extension_manifests); } }
/** * Table constructor. * @param string $table_name * @param string $table_key * @param array $config */ public function __construct($table_name, $table_key = 'id', array $config = []) { $this->Cyan = \Cyan::initialize(); $this->table_key = $table_key; $this->config = Config::getInstance($table_name); if (!empty($config)) { $this->config->loadArray($config); } parent::__construct($this->Cyan->getContainer('application')->Database->connect(), $table_name); }
/** * Get Form * * @param string $name * @param string|null $control_name * * @return Form * * @since 1.0.0 */ public function getForm($name, $control_name = null) { $Cyan = \Cyan::initialize(); if (strpos($name, ':') === false) { $form_identifier = sprintf('components:%s.form.%s', $this->getComponentName(), $name); $field_identifier = sprintf('components:%s.form.fields', $this->getComponentName()); Form::addFieldPath($Cyan->Finder->getPath($field_identifier)); } else { $form_identifier = $name; } $form = Form::getInstance($form_identifier, $control_name); return $form; }
/** * Render a layout * * @return string * * @since 1.0.0 */ public function display($layout = null) { $layout = !empty($layout) ? $layout : $this->layout; if (!$this->layout instanceof Layout) { $this->setLayout($layout); } if (!$this->layout->exists('component')) { $this->set('component', $this->getComponentName()); } if (!$this->layout->exists('view')) { $this->set('view', $this->getName()); } $Cyan = \Cyan::initialize(); $this->buffer_content = $this->layout->render(); $this->trigger('Render', $this); return $this->buffer_content; }
/** * ApplicationWeb constructor. * * @since 1.0.0 */ public function __construct() { $args = func_get_args(); switch (count($args)) { case 2: if (!is_string($args[0]) && !is_callable($args[1])) { throw new ApplicationException('Invalid argument orders. (String, Closure) given (%s,%s).', gettype($args[0]), gettype($args[1])); } $name = $args[0]; $initialize = $args[1]; break; case 1: if (is_string($args[0])) { $name = $args[0]; } elseif (is_callable($args[0])) { $initialize = $args[0]; } else { throw new ApplicationException('Invalid argument type! String|Closure, "%s" given.', gettype($args[0])); } break; case 0: break; default: throw new ApplicationException('Invalid arguments. Spected (String, Closure).'); break; } //create default name if (empty($this->name) && !isset($name)) { $reflection_class = new ReflectionClass($this); if (__CLASS__ != get_class($this) && strpos(get_class($this), 'Application') !== false) { $name_parts = array_filter(explode('Application', $reflection_class->getShortName())); $name = strtolower($name_parts[0]); } else { throw new ApplicationException('You must send a name'); } } if (empty($this->name)) { $this->name = $name; } $this->Cyan = \Cyan::initialize(); if (isset($initialize) && is_callable($initialize)) { $this->__initializer = $initialize->bindTo($this, $this); $this->__initializer(); } }
public function getTable($name) { $Cyan = \Cyan::initialize(); if (strpos($name, ':') === false) { $prefix = [ucfirst($this->getComponentName()), ucfirst($Cyan->getContainer('application')->getName())]; $sufix = ucfirst($name); } else { $parse = parse_url($name); $parts = explode(':', $parse['path']); $component = array_shift($parts); $name = end($parts); $prefix = [ucfirst($component), ucfirst($Cyan->getContainer('application')->getName())]; $sufix = ucfirst($name); } $class_name = sprintf('%sTable%s', implode($prefix), $sufix); if (!class_exists($class_name)) { array_pop($prefix); $class_name = sprintf('%sTable%s', implode($prefix), $sufix); } return $this->getClass($class_name, 'Cyan\\CMS\\Table'); }
/** * @param XmlElement $menu_node * @return \stdClass */ private static function getMenuManifestNode(XmlElement $menu_node) { $Cyan = \Cyan::initialize(); $App = $Cyan->getContainer('application'); $User = $App->getContainer('user'); $acl_menu_permission = $menu_node->getAttribute('acl_check'); if ($acl_menu_permission) { $acl_check = explode(',', $acl_menu_permission); $continue = false; foreach ($acl_check as $acl_permission) { if (!$continue && $User->can($acl_permission)) { $continue = true; break; } } if (!$continue) { return []; } } $route_name = $menu_node->getAttribute('route_name'); $route_params = json_decode($menu_node->getAttribute('route_params', '{}'), true); if (!is_array($route_params)) { $route_params = []; } $menu = new \stdClass(); $menu->icon = $menu_node->getAttribute('icon', 'fa fa-circle-o'); $menu->title = $menu_node->getAttribute('title'); $menu->link = !empty($route_name) ? $App->Router->generate($route_name, $route_params) : '#'; if (isset($menu_node->submenu) && $menu_node->submenu->menu->count()) { $menu->items = []; foreach ($menu_node->submenu->menu as $submenu_node) { $submenu_items = self::getMenuManifestNode($submenu_node); if (!empty($submenu_items)) { $menu->items[] = $submenu_items; } } } return $menu; }
/** * Render layout * * @param null $layout * @param array $data * @param array $options * * @return string * * @since 1.0.0 */ public function render($layout = null, array $data = [], array $options = []) { if (!empty($layout)) { $newLayout = self::display($layout, $data, $options); foreach ($this->getContainers() as $container_name) { if (!$newLayout->hasContainer($container_name)) { $newLayout->setContainer($container_name, $this->getContainer($container_name)); if ($container_name == 'view' && $this->getContainer('view')->hasContainer('application') && !$newLayout->hasContainer('application')) { $newLayout->setContainer('application', $this->getContainer('view')->getContainer('application')); } } } return $newLayout->render(); } $output = ''; $layout_path = str_replace('.', DIRECTORY_SEPARATOR, $this->layout) . '.php'; if ($file = FilesystemPath::find(self::addIncludePath(), $layout_path)) { $Cyan = \Cyan::initialize(); ob_start(); include $file; $output = ob_get_clean(); } return $output; }
/** * @param array $types * @param $base_path */ private function registerFiles(array $types, $base_path) { $App = $this->getContainer('application'); $Cyan = \Cyan::initialize(); foreach ($types as $type) { $file_path = FilesystemPath::find(self::addIncludePath(), $type . '.php'); if ($file_path) { $clean_path = str_replace('.' . pathinfo($file_path, PATHINFO_EXTENSION), null, $file_path); $class_name_parts = array_map('ucfirst', array_filter(explode(DIRECTORY_SEPARATOR, str_replace('com_', '', str_replace($base_path, null, $clean_path))))); array_unshift($class_name_parts, ucfirst($this->component_name)); $class_name = implode($class_name_parts); $Cyan->Autoload->registerClass($class_name, $file_path); } foreach (self::addIncludePath() as $search_path) { foreach (glob($search_path . DIRECTORY_SEPARATOR . $type . DIRECTORY_SEPARATOR . '*.php') as $file_path) { $clean_path = str_replace('.' . pathinfo($file_path, PATHINFO_EXTENSION), null, $file_path); $class_name_parts = array_map('ucfirst', array_filter(explode(DIRECTORY_SEPARATOR, str_replace('com_', '', str_replace($base_path, null, $clean_path))))); array_unshift($class_name_parts, ucfirst($this->component_name)); $class_name = implode($class_name_parts); $Cyan->Autoload->registerClass($class_name, $file_path); } } } }
/** * Render a layout * * @return string * * @since 1.0.0 */ public function display($layout = null) { $layout = !empty($layout) ? $layout : $this->layout; if (!$this->layout instanceof Layout) { $this->setLayout($layout); } if (!$this->layout->hasContainer('view')) { $this->layout->setContainer('view', $this); } if (!$this->layout->hasContainer('application') && $this->hasContainer('application')) { $this->layout->setContainer('application', $this->getContainer('application')); } $Cyan = \Cyan::initialize(); $this->buffer_content = $this->layout->render(); $this->trigger('Render', $this); return $this->buffer_content; }
/** * Set xml form * * @param $xml * * @since 1.0.0 */ private function setXML($xml) { if ($xml instanceof XmlElement) { /** @var \Cyan $Cyan */ $Cyan = \Cyan::initialize(); $group_name = (string) $xml['name']; if (!empty($group_name)) { $this->fields[$group_name] = $xml; } else { $this->fields['_default'] = $xml; } if ($path_identifier = $xml->getAttribute('addpath')) { self::addFieldPath(str_replace('/', DIRECTORY_SEPARATOR, $Cyan->Finder->getPath($path_identifier))); } } }
/** * User constructor. */ public function __construct() { $this->Cyan = \Cyan::initialize(); }
/** * Assign component routes * * @param $path */ public function loadComponents($path) { $app_config = $this->getConfig(); if (isset($app_config['sef'])) { $this->Router->setSef($app_config['sef']); } else { $Cyan = \Cyan::initialize(); $this->Router->setSef($Cyan->Router->getSef()); } $components_path = glob($path . '/*', GLOB_ONLYDIR); /** @var ExtensionTypeComponent $componentArchitecture */ $componentArchitecture = Extension::get('component'); $componentArchitecture->setContainer('application', $this); foreach ($components_path as $component_path) { $componentArchitecture->resetPath(); $componentArchitecture->addPath($component_path . DIRECTORY_SEPARATOR . $this->name); $componentArchitecture->addPath($component_path); $componentArchitecture->register($component_path); } }
/** * Return a Object * * @example type:path.name * * @param $identifier * @param $arguments * @param $default * * @since 1.0.0 */ public function getIdentifier($identifier, array $arguments = [], $default = null) { if (isset($this->cache[$identifier])) { return $this->cache[$identifier]; } $parse = parse_url($identifier); if (!isset($this->resources[$parse['scheme']]) && !isset($this->alias[$parse['scheme']])) { if (!isset($this->resources[$parse['scheme']])) { return $default; } } if (isset($this->alias[$parse['scheme']])) { $base_path = $this->getPath($this->alias[$parse['scheme']]); } else { $base_path = $this->resources[$parse['scheme']]; } $file_path = $base_path . DIRECTORY_SEPARATOR . str_replace('.', DIRECTORY_SEPARATOR, $parse['path']); $file_path = strtolower($file_path) . '.php'; if (!file_exists($file_path)) { return $default; } $Cyan = \Cyan::initialize(); $class_name = ucfirst($parse['scheme']) . implode(array_map('ucfirst', explode('.', $parse['path']))); if (!class_exists($class_name, false)) { $return = (require $file_path); // no cache for string response if (is_string($return)) { return $return; } elseif ($return instanceof \stdClass || is_callable($return) || is_array($return)) { $config = Config::getInstance($identifier); if (is_array($return)) { $config->loadArray($return); $return = $config; } elseif ($return instanceof \stdClass) { $config->loadObject($return); $return = $config; } $this->cache[$identifier] = $return; } else { foreach ($this->callbacks as $callback) { if ($return = $callback($identifier, $this, $return, $parse, $class_name)) { $this->cache[$identifier] = $return; return $return; } } if (!class_exists($class_name, false)) { throw new FinderException(sprintf('Undefined "%s" class in path "%s".', $class_name, $file_path)); } $class = new ReflectionClass($class_name); $instance = $class->newInstanceArgs($arguments); $this->cache[$identifier] = $instance; } } else { $class = new ReflectionClass($class_name); $instance = $class->newInstanceArgs($arguments); $this->cache[$identifier] = $instance; } if (isset($this->cache[$identifier])) { return $this->cache[$identifier]; } throw new FinderException(sprintf('Identifier "%s" not found', $identifier)); }
/** * Initialize Model */ public function initialize() { $factory_plugin = $this->getContainer('factory_plugin'); $factory_plugin->assign('model', $this); $this->Cyan = \Cyan::initialize(); }