/** * Command handler * * @param string $name The command name * @param mixed $args The command arguments * * @return boolean */ public function execute($name, $args) { $parts = explode('.', $name); $event = 'on' . KInflector::implode($parts); $dispatcher = KFactory::get('lib.koowa.event.dispatcher'); return $dispatcher->dispatch($event, $args); }
/** * Get the classname based on an identifier. * * @param mixed An identifier object - koowa:[path].name * * @return string|false Return object on success, returns FALSE on failure */ public function findClass(KServiceIdentifier $identifier) { $classname = 'Tmpl' . ucfirst($identifier->package) . KInflector::implode($identifier->path) . ucfirst($identifier->name); if (!$this->getService('koowa:loader')->loadClass($classname, $identifier->basepath)) { $classname = AnServiceClass::findDefaultClass($identifier); if (!$classname) { //$path = KInflector::implode($identifier->path); $classpath = $identifier->path; $classtype = !empty($classpath) ? array_shift($classpath) : ''; //Create the fallback path and make an exception for views $path = $classtype != 'view' ? ucfirst($classtype) . KInflector::camelize(implode('_', $classpath)) : ucfirst($classtype); $classes[] = 'Tmpl' . ucfirst($identifier->package) . $path . ucfirst($identifier->name); $classes[] = 'Tmpl' . ucfirst($identifier->package) . $path . 'Default'; $classes[] = 'ComApplication' . $path . ucfirst($identifier->name); $classes[] = 'ComApplication' . $path . 'Default'; $classes[] = 'LibApplication' . $path . ucfirst($identifier->name); $classes[] = 'LibApplication' . $path . 'Default'; $classes[] = 'LibBase' . $path . ucfirst($identifier->name); $classes[] = 'LibBase' . $path . 'Default'; $classes[] = 'K' . $path . ucfirst($identifier->name); $classes[] = 'K' . $path . 'Default'; foreach ($classes as $class) { if ($this->getService('koowa:loader')->loadClass($class, $identifier->basepath)) { $classname = $class; break; } } if ($classname) { AnServiceClass::setDefaultClass($identifier, $classname); } } } return $classname; }
/** * Get the classname based on an identifier * * @param mixed An identifier object - koowa:[path].name * @return string|false Return object on success, returns FALSE on failure */ public function findClass(KServiceIdentifier $identifier) { $classname = 'K' . ucfirst($identifier->package) . KInflector::implode($identifier->path) . ucfirst($identifier->name); if (!class_exists($classname)) { // use default class instead $classname = 'K' . ucfirst($identifier->package) . KInflector::implode($identifier->path) . 'Default'; if (!class_exists($classname)) { $classname = false; } } return $classname; }
/** * Command handler * * @param string The command name * @param object The command context * @return boolean Always returns true */ public function execute($name, KCommandContext $context) { $type = ''; if ($context->caller) { $identifier = clone $context->caller->getIdentifier(); if ($identifier->path) { $type = array_shift($identifier->path); } else { $type = $identifier->name; } } $parts = explode('.', $name); $event = 'on' . ucfirst(array_shift($parts)) . ucfirst($type) . KInflector::implode($parts); $this->_dispatcher->dispatchEvent($event, clone $context); return true; }
/** * Create an instance of a class based on a class identifier * * @param mixed Identifier or Identifier object - lib.koowa.[.path].name * @param object An optional KConfig object with configuration options * @return object|false Return object on success, returns FALSE on failure */ public function instantiate($identifier, KConfig $config) { $classname = false; if ($identifier->type == 'lib' && $identifier->package == 'koowa') { $classname = 'K' . KInflector::implode($identifier->path) . ucfirst($identifier->name); $filepath = KLoader::path($identifier); if (!class_exists($classname)) { // use default class instead $classname = 'K' . KInflector::implode($identifier->path) . 'Default'; if (!class_exists($classname)) { throw new KFactoryAdapterException("Class [{$classname}] not found in file [" . basename($filepath) . "]"); } } } return $classname; }
/** * Create an instance of a class based on a class identifier * * @param mixed $string The class identifier * @param array $options An optional associative array of configuration settings. * @return object */ public function createInstance($identifier, array $options) { $instance = false; $parts = explode('.', $identifier); if ($parts[0] == 'lib' && $parts[1] == 'koowa') { unset($parts[0]); unset($parts[1]); $classname = 'K' . KInflector::implode($parts); if (!class_exists($classname)) { $suffix = array_pop($parts); $options['name'] = array('prefix' => 'k', 'base' => KInflector::implode($parts), 'suffix' => $suffix); $classname = 'K' . KInflector::implode($parts) . 'Default'; } $instance = new $classname($options); } return $instance; }
/** * Command handler * * @param string The command name * @param object The command context * @return boolean Can return both true or false. */ public function execute($name, KCommandContext $context) { $type = ''; if ($context->caller) { $identifier = clone $context->caller->getIdentifier(); if ($identifier->path) { $type = array_shift($identifier->path); } else { $type = $identifier->name; } } $parts = explode('.', $name); $method = !empty($type) ? '_' . $type . ucfirst(KInflector::implode($parts)) : '_' . lcfirst(KInflector::implode($parts)); if (in_array($method, $this->getMethods())) { return $this->{$method}($context); } return true; }
/** * Implements magic method. Dynamically mixes a mixin * * @param string $method Method name * @param array $args Array of arugments * * @return mixed */ public function __call($method, $args) { //If the method hasn't been mixed yet, load all the behaviors if (!isset($this->_mixed_methods[$method])) { $key = 'behavior.' . $method; if (!self::_cache($this)->offsetExists($key)) { self::_cache($this)->offsetSet($key, false); $behaviors = $this->getRepository()->getBehaviors(); foreach ($behaviors as $behavior) { if (in_array($method, $behavior->getMixableMethods())) { //only mix the mixin that has $method self::_cache($this)->offsetSet($key, $behavior); break; } } } if ($behavior = self::_cache($this)->offsetGet($key)) { $this->mixin($behavior); } } $parts = KInflector::explode($method); if ($parts[0] == 'is') { if (isset($this->_mixed_methods[$method])) { return true; } else { return false; } } if (!isset($this->_mixed_methods[$method])) { if ($parts[0] == 'get' || $parts[0] == 'set') { $property = lcfirst(KInflector::implode(array_slice($parts, 1))); $property = $this->getEntityDescription()->getProperty($property); if ($property) { if ($parts[0] == 'get') { return $this->getData($property->getName()); } else { return $this->setData($property->getName(), array_shift($args)); } } } } return parent::__call($method, $args); }
/** * Finds the default class for an identifier or return null. * * @param KServiceIdentifier $identifier The identifier of the class * * @return string|bool Return the class name or false if not found */ public static function findDefaultClass($identifier) { $strIdentifier = (string) $identifier; if (isset(self::$_defaults[$strIdentifier])) { $classname = self::$_defaults[$strIdentifier]; if ($classname === false || class_exists($classname)) { return $classname; } } $classbase = 'Lib' . ucfirst($identifier->package) . KInflector::implode($identifier->path); $loader = KService::get('koowa:loader'); $classname = $classbase . ucfirst($identifier->name); if (!class_exists($classname)) { $classname = $classbase . 'Default'; if (!class_exists($classname)) { $classname = false; } } if ($classname === false) { if (isset(self::$_identifiers[$strIdentifier])) { $config = self::$_identifiers[$strIdentifier]; if (isset($config['default'])) { $classes = array_unique($config['default']); } else { $classes = get_prefix($config['prefix'], $config['name']); if (isset($config['fallback'])) { $classes[] = $config['fallback']; } } foreach ($classes as $class) { //make sure to find path first //then try to load it if ($loader->findPath($class, $identifier->basepath) && $loader->loadClass($class, $identifier->basepath)) { $classname = $class; break; } } } } self::setDefaultClass($strIdentifier, $classname); return $classname; }