Beispiel #1
0
 /**
  * Module initialization stage.
  *
  * @see ModuleConnector::init()
  *
  * @param array $params Initialization parameters
  *
  * @return bool True if resource successfully initialized
  */
 public function init(array $params = array())
 {
     // Subscribe to core template rendering event
     Event::subscribe('core.rendered', [$this, 'renderTemplate']);
     Event::subscribe(Compressor::E_CREATE_RESOURCE_LIST, [$this, 'getResources']);
     // Set default dependency as local file manager
     $this->fileManager = $this->fileManager ?: new LocalFileManager();
     $this->resourceManager = new ResourceManager($this->fileManager);
     ResourceManager::$cacheRoot = $this->cache_path;
     ResourceManager::$webRoot = getcwd();
     ResourceManager::$projectRoot = dirname(ResourceManager::$webRoot) . '/';
     // Get loaded modules
     $moduleList = $this->system->getContainer()->getServices('module');
     // Event for modification of resource list
     Event::fire(self::E_MODULES, [&$moduleList]);
     $appResourcePaths = $this->getAssets($moduleList);
     // Get assets
     $this->resources = $this->resourceManager->manage($appResourcePaths);
     // Fire completion event
     Event::fire(self::E_FINISHED, [&$this->resources]);
     // Get asset URLs
     $this->resourceUrls = array_map([$this, 'getAssetCachedUrl'], $this->resources);
     // Continue parent initialization
     return parent::init($params);
 }
Beispiel #2
0
 /** Module preparation stage handler */
 public function prepare(array $params = [])
 {
     // Subscribe for CSS handling
     Event::subscribe(Router::E_RESOURCE_COMPILE, [$this, 'compile']);
     Event::subscribe(Compressor::E_RESOURCE_COMPRESS, [$this, 'deCompile']);
     return parent::prepare($params);
 }
Beispiel #3
0
 /**
  * Render group of elements
  * @param $groups
  * @return string
  */
 public function renderGroup($groups)
 {
     // Iterate groups
     $html = '';
     foreach ($groups as $groupName => $groupContent) {
         // Get view by group
         $html .= $this->renderer->view($this->groupView)->group($groupName)->content($groupContent)->output();
     }
     return $html;
 }
Beispiel #4
0
 /** SamsonFramework load preparation stage handler */
 public function prepare(array $params = [])
 {
     $moduleCachePath = array_key_exists('cachePath', $params) ? $params['cachePath'] : $this->cache_path;
     $this->dependencyCache = $moduleCachePath . self::DEPENDENCY_CACHE;
     // Load file manager
     $this->fileManager = array_key_exists('fileManager', $params) ? $params['fileManager'] : new LocalFileManager();
     // Read previous cache file
     if ($this->fileManager->exists($this->dependencyCache)) {
         $this->dependencies = unserialize($this->fileManager->read($this->dependencyCache));
     }
     $this->less = new \lessc();
     Event::subscribe(Router::E_RESOURCE_COMPILE, [$this, 'compiler']);
     Event::subscribe(Router::E_FINISHED, [$this, 'cacheDependencies']);
     return parent::prepare();
 }
Beispiel #5
0
 /**	@see ModuleConnector::init() */
 public function init(array $params = array())
 {
     // Pointer to resourcerouter
     $rr = m('resourcer');
     // If CSS resource has been updated
     if (isset($rr->updated['css'])) {
         try {
             $less = new \lessc();
             // Read updated CSS resource file and compile it
             $css = $less->compile(file_get_contents($rr->updated['css']));
             // Write to the same place
             file_put_contents($rr->updated['css'], $css);
         } catch (Exception $e) {
             e('Ошибка обработки CSS: ' . $e->getMessage());
         }
     }
     // Вызовем родительский метод
     parent::init($params);
 }
Beispiel #6
0
 /**	@see ModuleConnector::init() */
 public function init(array $params = array())
 {
     // Pointer to resourcerouter
     $rr = m('resourcer');
     try {
         if (isset($rr->updated['js'])) {
             // Запишем новое содержание JS
             file_put_contents($rr->updated['js'], JSMin::minify(file_get_contents($rr->updated['js'])));
         }
         if (isset($rr->updated['css'])) {
             // Запишем новое содержание JS
             file_put_contents($rr->updated['css'], CSSMin::process(file_get_contents($rr->updated['css'])));
         }
     } catch (Exception $e) {
         e('Error minifying resource: ' . $e->getMessage());
     }
     // Вызовем родительский метод
     parent::init($params);
 }
 /**	@see ModuleConnector::init() */
 public function init(array $params = array())
 {
     // Pointer to resourcerouter
     $rr = m('resourcer');
     // If we have coffee resource in project
     if (isset($rr->cached['coffee'])) {
         // Change coffee file to js and store it as current js resource
         $newJS = str_replace('.coffee', '.js', str_replace(__SAMSON_PUBLIC_PATH, '', $rr->cached['coffee']));
         // If .coffee resource has been updated
         $file =& $rr->updated['coffee'];
         if (isset($file)) {
             try {
                 // Read coffee file
                 $coffee = file_get_contents($file);
                 // Initialize coffee compiler
                 \CoffeeScript\Init::load();
                 // Read updated .coffee resource file and compile it
                 $js = \CoffeeScript\Compiler::compile($coffee, array('filename' => $file));
                 // Compile coffee script to js and save to the same location
                 file_put_contents($file, $js);
             } catch (Exception $e) {
                 e('Ошибка обработки CoffeeScript: ' . $e->getMessage());
             }
         }
         // If regular JS has been updated or coffee script has been updated
         if (isset($rr->updated['js']) || isset($rr->updated['coffee'])) {
             // Read gathered js
             $oldJS = file_get_contents(str_replace(__SAMSON_PUBLIC_PATH, '', $rr->cached['js']));
             // Read gathered coffee
             $coffee = file_get_contents(str_replace(__SAMSON_PUBLIC_PATH, '', $rr->cached['coffee']));
             // Concatenate regular js and compiled coffee script js to a new javascript file
             file_put_contents($newJS, $oldJS . $coffee);
         }
         // Change old js resource to new one
         //$rr->cached['js'] = $newJS;
     }
     // Call parent method
     parent::init($params);
 }
 /**    @see ModuleConnector::init() */
 public function init(array $params = array())
 {
     parent::init($params);
     // Создадим имя файла содержащего пути к модулям
     $map_file = md5(implode('', array_keys(s()->module_stack))) . '.map';
     // Если такого файла нет
     if ($this->cache_refresh($map_file)) {
         // Fill in routes collection
         foreach ($this->system->module_stack as $id => $module) {
             self::$routes[$id] = $module->path();
         }
         // Save routes to file
         file_put_contents($map_file, serialize(self::$routes));
     }
     $moduleList = $this->system->module_stack;
     Event::fire(self::EVENT_START_GENERATE_RESOURCES, array(&$moduleList));
     //trace(array_keys($moduleList));die;
     $this->generateResources($moduleList);
     //$this->generateResources($this->system->module_stack);
     // Subscribe to core rendered event
     s()->subscribe('core.rendered', array($this, 'renderer'));
 }
Beispiel #9
0
 /**
  * Module preparation stage.
  * This function called after module instance creation but before
  * initialization stage.
  *
  * @param array $params Preparation stage parameters
  *
  * @return bool|void Preparation stage result
  */
 public function prepare(array $params = array())
 {
     $this->generator->scan(__SAMSON_CWD__ . '/src');
     //$this->generator->scan(__SAMSON_CWD__.'/app');
     $signature = $this->generator->hash();
     if ($this->cache_refresh($signature)) {
         $this->generator->generate($this->cache_path, array($this, 'viewHandler'));
         // Store cache file
         file_put_contents($signature, '');
     }
     // Add system static variable to all classes
     require_once 'View.php';
     View::$system =& $this->system;
     // Continue parent logic
     return parent::prepare($params);
 }
Beispiel #10
0
 /**	@see ModuleConnector::init() */
 public function init(array $params = array())
 {
     // Subscribe to core rendered event
     s()->subscribe('core.rendered', array($this, 'renderer'));
     // Вызовем родительский метод
     parent::init($params);
 }
Beispiel #11
0
 /**	@see ModuleConnector::init() */
 public function init(array $params = array())
 {
     parent::init($params);
     // Создадим имя файла содержащего пути к модулям
     $map_file = md5(implode('', array_keys(s()->module_stack))) . '.map';
     // Если такого файла нет
     if ($this->cache_refresh($map_file)) {
         // Fill in routes collection
         foreach (s()->module_stack as $id => $module) {
             self::$routes[$id] = $module->path();
         }
         // Save routes to file
         file_put_contents($map_file, serialize(self::$routes));
     }
     // Cache main web resources
     foreach (array(array('js'), array('css', 'less'), array('coffee')) as $rts) {
         // Get first resource type as extension
         $rt = $rts[0];
         $hash_name = '';
         // Iterate gathered namespaces for their resources
         foreach (s()->load_stack as $ns => &$data) {
             // If necessary resources has been collected
             foreach ($rts as $_rt) {
                 if (isset($data['resources'][$_rt])) {
                     foreach ($data['resources'][$_rt] as &$resource) {
                         // Created string with last resource modification time
                         $hash_name .= filemtime($resource);
                     }
                 }
             }
         }
         // Get hash that's describes resource status
         $hash_name = md5($hash_name) . '.' . $rt;
         $path = $hash_name;
         // Check if cache file has to be updated
         if ($this->cache_refresh($path)) {
             // Read content of resource files
             $content = '';
             foreach (s()->load_module_stack as $id => $data) {
                 $this->c_module =& m($id);
                 // If this ns has resources of specified type
                 foreach ($rts as $_rt) {
                     if (isset($data['resources'][$_rt])) {
                         //TODO: If you will remove & from iterator - system will fail at last element
                         foreach ($data['resources'][$_rt] as &$resource) {
                             // Store current processing resource
                             $this->cResource = $resource;
                             // Read resource file
                             $c = file_get_contents($resource);
                             // Rewrite url in css
                             if ($rt == 'css') {
                                 $c = preg_replace_callback('/url\\s*\\(\\s*(\'|\\")?([^\\)\\s\'\\"]+)(\'|\\")?\\s*\\)/i', array($this, 'src_replace_callback'), $c);
                             }
                             // Gather processed resource text together
                             $content .= "\n\r" . $c;
                         }
                     }
                 }
             }
             // Fix updated resource file with new path to it
             $this->updated[$rt] = $path;
             // Запишем содержание нового "собранного" ресурса
             file_put_contents($path, $content);
         }
         // Save path to resource cache
         $this->cached[$rt] = __SAMSON_CACHE_PATH . $this->id . '/' . $hash_name;
     }
     // Subscribe to core rendered event
     s()->subscribe('core.rendered', array($this, 'renderer'));
     // Register view renderer
     //s()->renderer( array( $this, 'renderer') );
 }
Beispiel #12
0
 /**
  * VirtualModule constructor.
  *
  * @param string             $path
  * @param ResourcesInterface $resources
  * @param SystemInterface    $system
  * @param null               $moduleId
  */
 public function __construct($path, ResourcesInterface $resources, SystemInterface $system, $moduleId = null)
 {
     $this->id = $moduleId;
     parent::__construct($path, $resources, $system);
 }
Beispiel #13
0
 /** Deserialization */
 public function __wakeup()
 {
     parent::__wakeup();
     self::$factory[self::getName(get_class($this))] = $this;
 }
Beispiel #14
0
 /**
  * Module preparation stage.
  *
  * @return bool Preparation stage result
  */
 public function prepare()
 {
     Event::subscribe(Router::EVENT_CREATED, array($this, 'renderer'));
     return parent::prepare();
 }