<commit_before><?php

/**
 * Copyright tureki.org [<EMAIL>]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace tureki;

/**
 * PHP Closure Compiler
 * A PHP Library to use Google Closure Compiler compress Javascript
 *
 * @copyright tureki.org
 * @author tureki
 * 
 **/
class PhpCc {
	
	static public $VERSION = "v1.0.0";

	public function __construct($options) 
	{

		$this->options = array(
			'java_file'    => 'java',
			'jar_file'     => __DIR__.'/vendor/closure-compiler/compiler.jar', 
			'output_path'  => '/',
			'optimization' => 'SIMPLE_OPTIMIZATIONS',
			'charset'      => 'utf-8'
		);

		$this->options = array_merge($this->options,$options);
		
		$this->reset();

	}
	
	/**
	 * Add javascript file to compiler list
	 * 
	 * @param string $file 
	 * @return self
	 */
	public function add($file)
	{

		if(is_array($file)){

			$this->js_files = array_merge($this->js_files,$file);

		}else{

			$this->js_files[] = $file;

		}

		return $this;

	}

	/**
	 * Execute compiler.
	 * 
	 * @param string $filename 
	 * @return self
	 */
	public function exec($filename = 'all.min.js') 
	{
		
		$str_file = '';

		if(substr($this->js_dir, -1)=="/")
		{
			$this->js_dir = substr($this->js_dir,0, strlen($this->js_dir)-1);
		}

		if($this->bol_single){

			$ary_result = array();
			
			$num_js     = count($this->js_files);

			for ($i=0; $i < $num_js; $i++) { 
		
				$str_file     = ' --js ' . $this->js_files[$i];
				
				$filename     = basename($this->js_files[$i]);
				
				$ary_result[] = $this->_get_argv($str_file,$filename);

			}

			$num_js = count($this->js_files_dir);

			for ($i=0; $i < $num_js; $i++) { 
		
				$str_file     = ' --js ' . $this->js_files_dir[$i];

				$filename     = str_replace($this->js_dir, '', $this->js_files_dir[$i]);

				$ary_result[] = $this->_get_argv($str_file,$filename);

			}


			return $ary_result;

		}else{

			if(count($this->js_files_dir)>0){

				$this->js_files = array_merge($this->js_files,$this->js_files_dir);
				
			}

			$this->js_files = array_unique($this->js_files);
			
			sort($this->js_files);
			
			$num_js         = count($this->js_files);

			for ($i=0; $i < $num_js; $i++) { 
		
				$str_file .= ' --js ' . $this->js_files[$i];

			}	

			return $this->_get_argv($str_file,$filename);

		}

	}

	/**
	 * Help method will return "Closure Compiler --help" when setting success 
	 * <commit_msg>Added 'sort' option to disable sorting of js files<commit_after><?php

/**
 * Copyright tureki.<EMAIL> [<EMAIL>]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace tureki;

/**
 * PHP Closure Compiler
 * A PHP Library to use Google Closure Compiler compress Javascript
 *
 * @copyright tureki.org
 * @author tureki
 * 
 **/
class PhpCc {
	
	static public $VERSION = "v1.0.0";

	public function __construct($options) 
	{

		$this->options = array(
			'java_file'    => 'java',
			'jar_file'     => __DIR__.'/vendor/closure-compiler/compiler.jar', 
			'output_path'  => '/',
			'optimization' => 'SIMPLE_OPTIMIZATIONS',
			'charset'      => 'utf-8',
			'sort'         => true,
		);

		$this->options = array_merge($this->options,$options);
		
		$this->reset();

	}
	
	/**
	 * Add javascript file to compiler list
	 * 
	 * @param string $file 
	 * @return self
	 */
	public function add($file)
	{

		if(is_array($file)){

			$this->js_files = array_merge($this->js_files,$file);

		}else{

			$this->js_files[] = $file;

		}

		return $this;

	}

	/**
	 * Execute compiler.
	 * 
	 * @param string $filename 
	 * @return self
	 */
	public function exec($filename = 'all.min.js') 
	{
		
		$str_file = '';

		if(substr($this->js_dir, -1)=="/")
		{
			$this->js_dir = substr($this->js_dir,0, strlen($this->js_dir)-1);
		}

		if($this->bol_single){

			$ary_result = array();
			
			$num_js     = count($this->js_files);

			for ($i=0; $i < $num_js; $i++) { 
		
				$str_file     = ' --js ' . $this->js_files[$i];
				
				$filename     = basename($this->js_files[$i]);
				
				$ary_result[] = $this->_get_argv($str_file,$filename);

			}

			$num_js = count($this->js_files_dir);

			for ($i=0; $i < $num_js; $i++) { 
		
				$str_file     = ' --js ' . $this->js_files_dir[$i];

				$filename     = str_replace($this->js_dir, '', $this->js_files_dir[$i]);

				$ary_result[] = $this->_get_argv($str_file,$filename);

			}


			return $ary_result;

		}else{

			if(count($this->js_files_dir)>0){

				$this->js_files = array_merge($this->js_files,$this->js_files_dir);
				
			}

			$this->js_files = array_unique($this->js_files);
			
			$opt        = $this->_get_options();

            if ($opt['sort']) {
    			sort($this->js_files);
            }
			
			$num_js         = count($this->js_files);

			for ($i=0; $i < $num_js; $i++) { 
		
				$str_file .= ' --js ' . $this->js_files[$i];

			}	

			return $this->_get_argv($str_file,$filename);

		}

	}

	/**
	 * Help method will return "Closure Compiler --help" when setting success 
	 * 
{'fraction_non_alphanumeric': 0.17108797850906649, 'fraction_numerical': 0.005036937541974479, 'mean_word_length': 2.848191214470284, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2808549', 'n_tokens_mistral': 2439, 'n_tokens_neox': 2051, 'n_words': 466}