/** * Build the scripts. * * @return void * @access public */ public function build() { $project = $this->getProject(); $manifest = $project->getManifestData(); $script_options = $manifest['scripts']; if (isset($script_options['libraries'])) { \Curator\Console::stdout(' Minifying libraries…'); foreach ($script_options['libraries'] as $filename) { $path = $project->getScriptsLibsDirPath() . DS . $filename; $rel_path = str_replace($project->getProjectDirPath() . DS, '', $path); $basename = pathinfo($filename, PATHINFO_FILENAME); $raw_size = filesize($path); $script_data = file_get_contents($path); $handler = HandlerFactory::getHandlerForMediaType('text/javascript'); $script_data = $handler->input($script_data); $hash = hash('sha256', $script_data); $out_path = $project->getPublicScriptsLibsDirPath() . DS . $basename . '-' . $hash . '.js'; $out_rel = str_replace($project->getProjectDirPath() . DS, '', $out_path); $out_url = str_replace($project->getPublicHtmlDirPath(), '', $out_path); if (!file_exists($out_path)) { touch($out_path); } if (!file_put_contents($out_path, $script_data)) { throw new \Exception('Could not save script library: ' . $out_path); } $output_size = filesize($out_path); $a = sprintf('%d', 100 * ($output_size / $raw_size)); \Curator\Console::stdout(' Wrote ' . $out_rel . ' (' . $a . '%)'); $lib_paths[$basename] = $out_url; } \Curator\TemplateData::setValue('scripts', 'libs', $lib_paths); \Curator\Console::stdout(''); } if (isset($script_options['combine'])) { \Curator\Console::stdout(' Minifying scripts…'); $script_data = null; foreach ($script_options['combine'] as $filename) { $path = $project->getScriptsDirPath() . DS . $filename; $rel_path = str_replace($project->getProjectDirPath() . DS, '', $path); $raw_size = filesize($path); $script_data = $script_data . NL . NL . NL . file_get_contents($path); } $handler = \Curator\Handler\Factory::getHandlerForMediaType('text/javascript'); $script_data = $handler->input($script_data); $hash = hash('sha256', $script_data); $out_path = $project->getPublicScriptsDirPath() . DS . 'combined-' . $hash . '.js'; $out_rel = str_replace($project->getProjectDirPath() . DS, '', $out_path); $out_url = str_replace($project->getPublicHtmlDirPath(), '', $out_path); if (!file_exists($out_path)) { touch($out_path); } if (!file_put_contents($out_path, $script_data)) { throw new \Exception('Could not save script file: ' . $out_path); } $output_size = filesize($out_path); $a = sprintf('%d', 100 * ($output_size / $raw_size)); \Curator\Console::stdout(' Wrote ' . $out_rel . ' (' . $a . '%)'); \Curator\TemplateData::setValue('scripts', 'combined', $out_url); } }
/** * Build the stylesheet files. * * @return void * @access public */ public function build() { $project = $this->project; $manifest = $project->getManifestData(); $style_options = $manifest['styles']; $css_data = ''; $raw_size = 0; if (is_array($style_options['combine'])) { foreach ($style_options['combine'] as $filename) { $filepath = $this->project->getStylesDirPath() . DS . $filename; $rel_path = str_replace($this->project->getProjectDirPath() . DS, '', $filepath); $raw_size = $raw_size + filesize($filepath); \Curator\Console::stdout(' Loading ' . $rel_path); $css_data = $css_data . NL . NL . NL . file_get_contents($filepath); } \Curator\Console::stdout(' Minimizing styles…'); $handler = \Curator\Handler\Factory::getHandlerForMediaType('text/css'); $css_data = $handler->input($css_data); $hash = hash('sha256', $css_data); $output_path = $project->getPublicStylesDirPath() . DS . 'combined-' . $hash . '.css'; $rel_output = str_replace($this->project->getProjectDirPath() . DS, '', $output_path); $url_output = str_replace($this->project->getPublicHtmlDirPath(), '', $output_path); if (!file_put_contents($output_path, $css_data)) { throw new \Exception('Could not write CSS to: ' . $output_path); } $output_size = filesize($output_path); $a = sprintf('%d', 100 * ($output_size / $raw_size)); \Curator\Console::stdout(' wrote ' . $rel_output . ' (' . $a . '%)'); \Curator\TemplateData::setValue('styles', 'combined', $url_output); } else { \Curator\Console::stdout(' Nothing to do.'); } }
/** * Handle $data, and return the results. * * @param string data The data to handle. * @return string * @access public */ public function input($data, $options = array()) { $result = null; try { if (strpos($data, NL) === false && is_file($data)) { $data = file_get_contents($data); if ($data === false) { throw new \Exception('Could not load data: ' . $data); } } $data_array = explode("\n\n---\n\n", $data, 2); $header_handler = \Curator\Handler\Factory::getHandlerForMediaType(\Curator\Handler\YAML::getMediaType()); $header_data = $header_handler->input($data_array[0]); $body_format = $header_data['format']; $body_handler = \Curator\Handler\Factory::getHandlerForMediaType($body_format); $body_data = $body_handler->input($data_array[1]); $result = array(); $result['header'] = $header_data; $result['body'] = $body_data; $result['body_raw'] = $data_array[1]; } catch (\Exception $e) { \Curator\Console::stderr('** Could not handle curd data:'); \Curator\Console::stderr(' ' . $e->getMessage()); } return $result; }
/** * Write $data to $output. * * @param array The data to write. * @param string The path to write to. * @return bool * @access public */ public function output($data, $output) { $handler = \Curator\Handler\Factory::getHandlerForMediaType(\Curator\Handler\YAML::getMediaType()); $config = $handler->input($data); return $config; }
/** * Clean the data files from public_html. * * @access public */ public function clean() { // Get our cast of characters. $data_dir = $this->project->getDataDirPath(); $data_files = \Curator\FileSystem::getDirectoryContents($data_dir); foreach ($data_files as $data_file) { // Gather our facts here. $data_info = pathinfo($data_file); $data_ext = $data_info['extension']; $data_rel = str_replace($this->project->getProjectDirPath() . DS, '', $data_file); // relative from the project dir. $data_media = \Curator\Handler\Factory::getMediaTypeForFileExtension($data_ext); // load the data file. $handler = \Curator\Handler\Factory::getHandlerForMediaType($data_media); $data = $handler->input($data_file); // See if the file specifies a special name, otherwise hack // together something resembling the data's filename. if (isset($data['header']['url'])) { $filename = $data['header']['url']; } else { $filename = pathinfo($data_file, PATHINFO_FILENAME) . '.html'; } $output_path = $this->project->getPublicHtmlDirPath() . DS . $filename; if (file_exists($output_path)) { \Curator\Console::stdout(' Deleting public_html' . DS . $filename); if (!unlink($output_path)) { throw new \Exception('Could not delete: ' . $output_path); } } } }
/** * Returns the projects manifest data. * * @return array The manifest data. * @access public */ public function getManifestData() { if ($this->manifest === null) { $manifest_path = $this->getProjectDirPath() . DS . 'manifest.yml'; $ext = \Curator\Handler\Factory::getMediaTypeForFileExtension(pathinfo($manifest_path, PATHINFO_EXTENSION)); $handler = \Curator\Handler\Factory::getHandlerForMediaType($ext); $manifest = $handler->input($manifest_path); $this->manifest = $manifest; } return $this->manifest; }