/** * execute tests */ public function test($action, $context) { $action = us($action); // create token generator object $generator = $context->createObject('simple', 'token_generator'); switch ($action) { case "simple_default": // create token generator object $token = $generator->generateToken(); $this->assertEquals(strlen($token), 40); $this->assertEquals(preg_match("/[^0-9a-zA-Z]+/", $token), false); echo "default token: {$token}"; break; case "simple_sha1": $config = new Charcoal_Config($this->getSandbox()->getEnvironment()); $config->set(s('algorithm'), 'sha1'); $generator->configure($config); $token = $generator->generateToken(); $this->assertEquals(strlen($token), 40); $this->assertEquals(preg_match("/[^0-9a-zA-Z]+/", $token), false); echo "sha1 token: {$token}"; break; case "simple_md5": $config = new Charcoal_Config($this->getSandbox()->getEnvironment()); $config->set(s('algorithm'), 'md5'); $generator->configure($config); $token = $generator->generateToken(); $this->assertEquals(strlen($token), 32); $this->assertEquals(preg_match("/[^0-9a-zA-Z]+/", $token), false); echo "md5 token: {$token}"; break; } }
/** * execute tests */ public function test($action, $context) { echo "action:{$action}" . PHP_EOL; $request = $context->getRequest(); $action = us($action); // Tidy $tidy = $context->getComponent('tidy@:html:repair:tidy'); $config = new Charcoal_Config(); $config->set('encoding', 'utf8'); $tidy->configure($config); switch ($action) { case "parse_string": $html1 = <<<HTMLDATA1 <html> <head> <title>My Title</name> </head> <BODY> <h1>Test Header</hh1> <form>Google</textarea> <textarea>http://google.com/</textarea></form> </company> HTMLDATA1; $tidy->parseString($html1); show_with_children($tidy->root()); return TRUE; } return FALSE; }
/** * execute tests */ public function test($action, $context) { $action = us($action); $sequence = new Charcoal_SequenceHolder(new Charcoal_Sequence(), new Charcoal_Sequence()); // form token component $form_token = $context->getComponent('form_token@:charcoal:form'); $config = new Charcoal_Config($this->getSandbox()->getEnvironment()); $config->set('token_key', 'foo'); $form_token->configure($config); switch ($action) { case "form_token1": $token = $form_token->generate($sequence); echo "token: {$token}" . PHP_EOL; $this->assertNotNull($token); $this->assertNotEmpty($token); break; case "form_token2": // save my ticket into sequence $token_list = $sequence->get('token_key'); $token_list[] = 'my-ticket'; $sequence->set('foo', $token_list); // validation token will success $form_token->validate($sequence, 'my-ticket'); break; case "form_token3": // save my ticket into sequence $token_list = $sequence->get('token_key'); $token_list[] = 'my-ticket'; $sequence->set('foo', $token_list); // validation token will fail $this->setExpectedException('Charcoal_FormTokenValidationException'); $form_token->validate($sequence, 'another-ticket'); break; } }
/** * set options * * @param Charcoal_Properties $options option set to apply */ public function setOptions($options) { // Charcoal_ParamTrait::validateProperties( 1, $options, TRUE ); if (is_array($options) || $options === NULL) { $options = new Charcoal_Config($this->getSandbox()->getEnvironment(), $options); } $this->debug = $options->getBoolean('debug', FALSE); }
/** * execute tests */ public function test($action, $context) { $request = $context->getRequest(); $action = us($action); // Qdmail $qdmail = $context->getComponent('qdmail@:qdmail'); $config = new Charcoal_Config($this->getSandbox()->getEnvironment()); $config->set('qdsmtp.host', 'localhost'); $config->set('qdsmtp.port', '25'); $config->set('qdsmtp.from', '*****@*****.**'); $config->set('qdsmtp.protocol', 'SMTP'); $config->set('qdsmtp.user', ''); $config->set('qdsmtp.pass', ''); $qdmail->configure($config); switch ($action) { // Send mail case "send_mail": $to = $request->get("to"); $from = "*****@*****.**"; $subject = "test"; $body = "test!!!"; echo "to:" . $to . eol(); $qdmail->sendMail($from, $to, $subject, $body); break; } }
/** * execute tests */ public function test($action, $context) { $request = $context->getRequest(); $action = us($action); // SimplePie $simplepie = $context->getComponent('simplepie@:rss:simplepie'); $config = new Charcoal_Config($this->getSandbox()->getEnvironment()); $config->set('enable_cahche', true); $config->set('cache_dir', CHARCOAL_CACHE_DIR . '/simplepie'); $config->set('duration', 1800); $simplepie->configure($config); switch ($action) { // Send mail case "get_feed": //$feed = $simplepie->getFeed( 'http://charcoalphp.org/test/rss/index.xml' ); $feed = $simplepie->getFeed('http://1000mg.jp/feed'); ad($feed); return TRUE; } return FALSE; }
/** * execute tests */ public function test($action, $context) { $action = us($action); $temp_dir = Charcoal_ResourceLocator::getApplicationPath(s('tmp')); switch ($action) { case "get_section": $ini_data = parse_ini_file($temp_dir . '/test.ini', true); $config = new Charcoal_Config($this->getSandbox()->getEnvironment(), $ini_data); $first_section = $config->getSection(s('first_section')); $second_section = $config->getSection(s('second_section')); $third_section = $config->getSection(s('third_section')); $this->assertEquals(1, $first_section['one']); $this->assertEquals(5, $first_section['five']); $this->assertEquals('BIRD', $first_section['animal']); $this->assertEquals("/usr/local/bin", $second_section['path']); $this->assertEquals("http://www.example.com/~username", $second_section['URL']); $this->assertEquals(array("5.0", "5.1", "5.2", "5.3"), $third_section['phpversion']); return TRUE; } return FALSE; }
/** * create class loader * * @param string $obj_path class loader's object path to create * * @return Charcoal_IClassLoader class loader object */ public function createClassLoader($obj_path) { // Charcoal_ParamTrait::validateStringOrObjectPath( 1, $obj_path ); $class_loader = NULL; try { $obj_path = is_string($obj_path) ? new Charcoal_ObjectPath($obj_path) : $obj_path; // Configをロード $config = Charcoal_ConfigLoader::loadConfig($this, $obj_path, 'class_loader'); $config = new Charcoal_Config($this->environment, $config); // クラス名を取得 $class_name = $config->getString('class_name'); if ($class_name === NULL) { _throw(new Charcoal_ClassLoaderConfigException($obj_path, 'class_name', 'mandatory')); } $class_name = us($class_name); // project directory $project_dir = Charcoal_ResourceLocator::getProjectPath(); // ソースの取り込み $source_path = $project_dir . '/app/' . CHARCOAL_APPLICATION . '/class/class_loader/' . $class_name . '.class.php'; if (is_readable($source_path)) { /** @noinspection PhpIncludeInspection */ include $source_path; } else { $source_path = $project_dir . '/class/class_loader/' . $class_name . '.class.php'; if (is_readable($source_path)) { /** @noinspection PhpIncludeInspection */ include $source_path; } } // クラスローダのインスタンス生成 $klass = new Charcoal_Class($class_name); /** @var Charcoal_IClassLoader $class_loader */ $class_loader = $klass->newInstance(); $class_loader->setSandbox($this); // インタフェース確認 $interface = new Charcoal_Interface('Charcoal_IClassLoader'); $interface->validateImplements($class_loader); // log_info( 'system', "factory", "クラスローダ[" . us($object_path) . "]を作成しました。" ); } catch (Exception $ex) { _catch($ex); _throw(new Charcoal_CreateClassLoaderException($obj_path, $ex)); } return $class_loader; }
/** * load component * * @param string $component_name component path * @param Charcoal_Vector $args constructor arguments */ public function loadComponent($component_name, $args = array()) { try { $component_name = us($component_name); // log_info( "system,container", "container", "Loading component: [$component_name]"); // コンポーネント設定ファイルの読み込み $obj_path = new Charcoal_ObjectPath($component_name); $config = Charcoal_ConfigLoader::loadConfig($this->sandbox, $obj_path, 'component'); $config = new Charcoal_Config($this->sandbox->getEnvironment(), $config); // キャッシュに保存 $this->component_configs[$component_name] = $config; // クラス名を取得 $class_name = $config->getString('class_name'); if ($class_name === NULL) { _throw(new Charcoal_ComponentConfigException($component_name, "class_name", "mandatory")); } // create class object $klass = new Charcoal_Class($class_name); // コンポーネントスコープを取得 $scope = $config->getString('scope', self::SCOPE_REQUEST); $scope = us($scope); // コンポーネントスコープによって生成方法を変更 $component = NULL; switch ($scope) { case self::SCOPE_SESSION: // コンポーネントのインスタンスをセッションから復元 $component = unserialize($_SESSION[$component_name]); // セッションになければ、インスタンスを生成 if ($component == NULL) { $component = $klass->newInstance($args); } break; case self::SCOPE_TRANSIENT: case self::SCOPE_REQUEST: // コンポーネントのインスタンス生成 $component = $klass->newInstance($args); break; default: // scopeに指定されたワードが不正 _throw(new Charcoal_ComponentConfigException($component_name, 'scope', "invalid scope value:{$scope}")); break; } // initialize component $component->setComponentName($component_name); $component->setSandbox($this->sandbox); // 生成したインスタンスがIComponentインタフェースを実装しているか確認 if (!$component instanceof Charcoal_IComponent) { // 実装例外 _throw(new Charcoal_InterfaceImplementException($class_name, "Charcoal_IComponent")); } // コンポーネントを初期化 // log_info( "system,container", "container", "configuring component: [$component_name]"); $component->configure($config); // コンポーネントを配列に登録 if ($scope == self::SCOPE_SESSION || $scope == self::SCOPE_REQUEST) { $this->components[$component_name] = $component; } // log_info( "system,container", "container", "loaded component: [$component_name]"); // ロードしたコンポーネントを返却 return $component; } catch (Exception $ex) { _catch($ex); // rethrow exception _throw(new Charcoal_ComponentLoadingException($component_name, $ex)); } }